Skip to content

Commit

Permalink
[Command] Added tv example and pattern notes
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Aug 14, 2015
1 parent 6250b4b commit 4200062
Show file tree
Hide file tree
Showing 12 changed files with 329 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

## <a name="command">Command</a> [&#8593;](#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)
57 changes: 57 additions & 0 deletions src/command/examples/tv/Client.java
Original file line number Diff line number Diff line change
@@ -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<ElectronicDevice> allDevices = new ArrayList<>();

allDevices.add(televisionTwo);
allDevices.add(radioOne);

TurnOffAllDevices turnOffDevices = new TurnOffAllDevices(allDevices);

DeviceButton turnThemOff = new DeviceButton(turnOffDevices);

turnThemOff.press();
turnThemOff.pressUndo();

}

}
24 changes: 24 additions & 0 deletions src/command/examples/tv/DeviceButton.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
9 changes: 9 additions & 0 deletions src/command/examples/tv/commands/Command.java
Original file line number Diff line number Diff line change
@@ -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();
}
29 changes: 29 additions & 0 deletions src/command/examples/tv/commands/TurnOffAllDevices.java
Original file line number Diff line number Diff line change
@@ -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<ElectronicDevice> allDevices;

public TurnOffAllDevices(List<ElectronicDevice> newDevices) {
allDevices = newDevices;
}

public void execute() {
for (ElectronicDevice device : allDevices) {
device.off();
}
}

public void undo() {
for (ElectronicDevice device : allDevices) {
device.on();
}
}
}
25 changes: 25 additions & 0 deletions src/command/examples/tv/commands/TurnOffTelevision.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
25 changes: 25 additions & 0 deletions src/command/examples/tv/commands/TurnOnTelevision.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
27 changes: 27 additions & 0 deletions src/command/examples/tv/commands/VolumeDownTelevision.java
Original file line number Diff line number Diff line change
@@ -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();
}


}
27 changes: 27 additions & 0 deletions src/command/examples/tv/commands/VolumeUpTelevision.java
Original file line number Diff line number Diff line change
@@ -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();
}


}
11 changes: 11 additions & 0 deletions src/command/examples/tv/devices/ElectronicDevice.java
Original file line number Diff line number Diff line change
@@ -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();
}
36 changes: 36 additions & 0 deletions src/command/examples/tv/devices/Radio.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
36 changes: 36 additions & 0 deletions src/command/examples/tv/devices/Television.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 4200062

Please sign in to comment.