From 4200062b73f18957eeb1616964254e73aeb2f04b Mon Sep 17 00:00:00 2001 From: Luis Burgos Date: Thu, 13 Aug 2015 23:05:05 -0500 Subject: [PATCH] [Command] Added tv example and pattern notes --- README.md | 23 ++++++++ src/command/examples/tv/Client.java | 57 +++++++++++++++++++ src/command/examples/tv/DeviceButton.java | 24 ++++++++ src/command/examples/tv/commands/Command.java | 9 +++ .../tv/commands/TurnOffAllDevices.java | 29 ++++++++++ .../tv/commands/TurnOffTelevision.java | 25 ++++++++ .../tv/commands/TurnOnTelevision.java | 25 ++++++++ .../tv/commands/VolumeDownTelevision.java | 27 +++++++++ .../tv/commands/VolumeUpTelevision.java | 27 +++++++++ .../examples/tv/devices/ElectronicDevice.java | 11 ++++ src/command/examples/tv/devices/Radio.java | 36 ++++++++++++ .../examples/tv/devices/Television.java | 36 ++++++++++++ 12 files changed, 329 insertions(+) create mode 100644 src/command/examples/tv/Client.java create mode 100644 src/command/examples/tv/DeviceButton.java create mode 100644 src/command/examples/tv/commands/Command.java create mode 100644 src/command/examples/tv/commands/TurnOffAllDevices.java create mode 100644 src/command/examples/tv/commands/TurnOffTelevision.java create mode 100644 src/command/examples/tv/commands/TurnOnTelevision.java create mode 100644 src/command/examples/tv/commands/VolumeDownTelevision.java create mode 100644 src/command/examples/tv/commands/VolumeUpTelevision.java create mode 100644 src/command/examples/tv/devices/ElectronicDevice.java create mode 100644 src/command/examples/tv/devices/Radio.java create mode 100644 src/command/examples/tv/devices/Television.java diff --git a/README.md b/README.md index 61da94f..b6d411d 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,7 @@ Composición de clases u objetos. Forma en que clases las u objetos interaccionan y distribuyen funcionalidades. +* [Command](#command) * [Observer](#observer) * [Strategy](#strategy) @@ -215,3 +216,25 @@ Extender la funcionalidad de los objetos se puede hacer de forma estática en nu **Ejemplos:** * [Banco](https://github.com/LuisBurgos/design-patterns/tree/master/src/facade/examples/bank) * [Computadora](https://github.com/LuisBurgos/design-patterns/tree/master/src/facade/examples/computer) + +## Command [↑](#lista-de-patrones) + + El patrón *Command* encapsula comandos( llamados a métodos) en objetos, permitiéndonos realizar peticiones sin conocer exactamente la petición que se realiza o el objeto al cuál se le hace la petición. Este patrón nos provee las opciones para hacer listas de comandos, hacer/deshacer acciones y otras manipulaciones. + + Este patrón desacopla al *objeto que invoca* la operación del *objeto que sabe cómo* llevar a cabo la misma. Un objeto llamado *Invoker* transfiere el *comando* a otro objeto llamado *Receiver* el cual ejecuta el código correcto para el *comando* recibido. + +**Propósito:** Encapsular una petición en forma de objeto, permitiendo de ese modo que parametrizar clientes con diferentes peticiones, "colas" o registros de solicitudes, y apoyar las operaciones de deshacer. + +**Aplicación:** Usamos el patrón [Command...](https://github.com/LuisBurgos/design-patterns/tree/master/src/command/pattern) +* Cuando queremos realizar peticiones en diferentes tiempos. Se puede hacer a través de la especificación de una "cola". +* Para implementar la función de deshacer (*undo*), ya que se puede almacenar el estado de la ejecución del comando para revertir sus efectos. +* Cuando necesitemos mantener un registro (*log*) de los cambios y acciones. + +**Usos típicos:** +* Mantener un historial de peticiones. (*requests*) +* Implementar la funcionalidad de *callbacks*. +* Implementar la funcionalidad de *undo*. + +**Ejemplos:** +* [Televisión](https://github.com/LuisBurgos/design-patterns/tree/master/src/command/examples/tv) +* [Hechizos](https://github.com/LuisBurgos/design-patterns/tree/master/src/command/examples/spells) (No implementado aún) diff --git a/src/command/examples/tv/Client.java b/src/command/examples/tv/Client.java new file mode 100644 index 0000000..6d03680 --- /dev/null +++ b/src/command/examples/tv/Client.java @@ -0,0 +1,57 @@ +package command.examples.tv; + +import command.examples.tv.commands.TurnOffAllDevices; +import command.examples.tv.commands.TurnOffTelevision; +import command.examples.tv.commands.TurnOnTelevision; +import command.examples.tv.commands.VolumeUpTelevision; +import command.examples.tv.devices.ElectronicDevice; +import command.examples.tv.devices.Radio; +import command.examples.tv.devices.Television; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by luisburgos on 13/08/15. + */ +public class Client { + + public static void main(String[] args){ + + ElectronicDevice televisionOne = new Television("SAMSUMG"); + + TurnOnTelevision onCommand = new TurnOnTelevision(televisionOne); + + DeviceButton onPressed; + onPressed = new DeviceButton(onCommand); + onPressed.press(); + + + TurnOffTelevision offCommand = new TurnOffTelevision(televisionOne); + onPressed = new DeviceButton(offCommand); + onPressed.press(); + + VolumeUpTelevision volUpCommand = new VolumeUpTelevision(televisionOne); + onPressed = new DeviceButton(volUpCommand); + onPressed.press(); + onPressed.press(); + onPressed.press(); + + Television televisionTwo = new Television("SONY"); + Radio radioOne = new Radio("PIONEER"); + + List allDevices = new ArrayList<>(); + + allDevices.add(televisionTwo); + allDevices.add(radioOne); + + TurnOffAllDevices turnOffDevices = new TurnOffAllDevices(allDevices); + + DeviceButton turnThemOff = new DeviceButton(turnOffDevices); + + turnThemOff.press(); + turnThemOff.pressUndo(); + + } + +} diff --git a/src/command/examples/tv/DeviceButton.java b/src/command/examples/tv/DeviceButton.java new file mode 100644 index 0000000..736a546 --- /dev/null +++ b/src/command/examples/tv/DeviceButton.java @@ -0,0 +1,24 @@ +package command.examples.tv; + +import command.examples.tv.commands.Command; + +/** + * The INVOKER + * Created by luisburgos on 13/08/15. + */ +public class DeviceButton { + + private Command command; + + public DeviceButton(Command command){ + this.command = command; + } + + public void press(){ + command.execute(); + } + + public void pressUndo(){ + command.undo(); + } +} diff --git a/src/command/examples/tv/commands/Command.java b/src/command/examples/tv/commands/Command.java new file mode 100644 index 0000000..9448801 --- /dev/null +++ b/src/command/examples/tv/commands/Command.java @@ -0,0 +1,9 @@ +package command.examples.tv.commands; + +/** + * Created by luisburgos on 13/08/15. + */ +public interface Command { + public void execute(); + public void undo(); +} diff --git a/src/command/examples/tv/commands/TurnOffAllDevices.java b/src/command/examples/tv/commands/TurnOffAllDevices.java new file mode 100644 index 0000000..09a5098 --- /dev/null +++ b/src/command/examples/tv/commands/TurnOffAllDevices.java @@ -0,0 +1,29 @@ +package command.examples.tv.commands; + +import command.examples.tv.devices.ElectronicDevice; + +import java.util.List; + +/** + * Created by luisburgos on 13/08/15. + */ +public class TurnOffAllDevices implements Command { + + List allDevices; + + public TurnOffAllDevices(List newDevices) { + allDevices = newDevices; + } + + public void execute() { + for (ElectronicDevice device : allDevices) { + device.off(); + } + } + + public void undo() { + for (ElectronicDevice device : allDevices) { + device.on(); + } + } +} \ No newline at end of file diff --git a/src/command/examples/tv/commands/TurnOffTelevision.java b/src/command/examples/tv/commands/TurnOffTelevision.java new file mode 100644 index 0000000..f182f7f --- /dev/null +++ b/src/command/examples/tv/commands/TurnOffTelevision.java @@ -0,0 +1,25 @@ +package command.examples.tv.commands; + +import command.examples.tv.devices.ElectronicDevice; + +/** + * Created by luisburgos on 13/08/15. + */ +public class TurnOffTelevision implements Command { + + private ElectronicDevice device; + + public TurnOffTelevision(ElectronicDevice device){ + this.device = device; + } + + @Override + public void execute() { + device.off(); + } + + @Override + public void undo() { + device.on(); + } +} diff --git a/src/command/examples/tv/commands/TurnOnTelevision.java b/src/command/examples/tv/commands/TurnOnTelevision.java new file mode 100644 index 0000000..f088663 --- /dev/null +++ b/src/command/examples/tv/commands/TurnOnTelevision.java @@ -0,0 +1,25 @@ +package command.examples.tv.commands; + +import command.examples.tv.devices.ElectronicDevice; + +/** + * Created by luisburgos on 13/08/15. + */ +public class TurnOnTelevision implements Command { + + private ElectronicDevice device; + + public TurnOnTelevision(ElectronicDevice device){ + this.device = device; + } + + @Override + public void execute() { + device.on(); + } + + @Override + public void undo() { + device.off(); + } +} diff --git a/src/command/examples/tv/commands/VolumeDownTelevision.java b/src/command/examples/tv/commands/VolumeDownTelevision.java new file mode 100644 index 0000000..c884180 --- /dev/null +++ b/src/command/examples/tv/commands/VolumeDownTelevision.java @@ -0,0 +1,27 @@ +package command.examples.tv.commands; + +import command.examples.tv.devices.ElectronicDevice; + +/** + * Created by luisburgos on 13/08/15. + */ +public class VolumeDownTelevision implements Command { + + private ElectronicDevice device; + + public VolumeDownTelevision(ElectronicDevice device){ + this.device = device; + } + + @Override + public void execute() { + device.volumenDown(); + } + + @Override + public void undo() { + device.volumeUp(); + } + + +} diff --git a/src/command/examples/tv/commands/VolumeUpTelevision.java b/src/command/examples/tv/commands/VolumeUpTelevision.java new file mode 100644 index 0000000..888beb4 --- /dev/null +++ b/src/command/examples/tv/commands/VolumeUpTelevision.java @@ -0,0 +1,27 @@ +package command.examples.tv.commands; + +import command.examples.tv.devices.ElectronicDevice; + +/** + * Created by luisburgos on 13/08/15. + */ +public class VolumeUpTelevision implements Command { + + private ElectronicDevice device; + + public VolumeUpTelevision(ElectronicDevice device){ + this.device = device; + } + + @Override + public void execute() { + device.volumeUp(); + } + + @Override + public void undo() { + device.volumenDown(); + } + + +} diff --git a/src/command/examples/tv/devices/ElectronicDevice.java b/src/command/examples/tv/devices/ElectronicDevice.java new file mode 100644 index 0000000..d5007b7 --- /dev/null +++ b/src/command/examples/tv/devices/ElectronicDevice.java @@ -0,0 +1,11 @@ +package command.examples.tv.devices; + +/** + * Created by luisburgos on 13/08/15. + */ +public interface ElectronicDevice { + public void on(); + public void off(); + public void volumeUp(); + public void volumenDown(); +} diff --git a/src/command/examples/tv/devices/Radio.java b/src/command/examples/tv/devices/Radio.java new file mode 100644 index 0000000..457ff68 --- /dev/null +++ b/src/command/examples/tv/devices/Radio.java @@ -0,0 +1,36 @@ +package command.examples.tv.devices; + +/** + * Created by luisburgos on 13/08/15. + */ +public class Radio implements ElectronicDevice { + + private int volume = 0; + private String name; + + public Radio(String name) { + this.name = name; + } + + @Override + public void on() { + System.out.println(name + "RADIO is on"); + } + + @Override + public void off() { + System.out.println(name + "RADIO is off"); + } + + @Override + public void volumeUp() { + volume++; + System.out.println(name + "RADIO Volume at: " + volume); + } + + @Override + public void volumenDown() { + volume--; + System.out.println(name + "RADIO Volume at: " + volume); + } +} diff --git a/src/command/examples/tv/devices/Television.java b/src/command/examples/tv/devices/Television.java new file mode 100644 index 0000000..b91a3dd --- /dev/null +++ b/src/command/examples/tv/devices/Television.java @@ -0,0 +1,36 @@ +package command.examples.tv.devices; + +/** + * Created by luisburgos on 13/08/15. + */ +public class Television implements ElectronicDevice { + + private int volume = 0; + private String name; + + public Television(String name) { + this.name = name; + } + + @Override + public void on() { + System.out.println(name + "TV is on"); + } + + @Override + public void off() { + System.out.println(name + "TV is off"); + } + + @Override + public void volumeUp() { + volume++; + System.out.println(name + "TV Volume at: " + volume); + } + + @Override + public void volumenDown() { + volume--; + System.out.println(name + "TV Volume at: " + volume); + } +}