From 4f545ce28a8830c5068bbe4bc2290ccbda72ca36 Mon Sep 17 00:00:00 2001 From: Luis Burgos Date: Fri, 14 Aug 2015 13:29:11 -0500 Subject: [PATCH] [Command] Added spells example, rename device folder. --- README.md | 4 +- .../examples/{tv => devices}/Client.java | 18 +++---- .../{tv => devices}/DeviceButton.java | 4 +- .../{tv => devices}/commands/Command.java | 2 +- .../commands/TurnOffAllDevices.java | 4 +- .../commands/TurnOffTelevision.java | 4 +- .../commands/TurnOnTelevision.java | 4 +- .../commands/VolumeDownTelevision.java | 4 +- .../commands/VolumeUpTelevision.java | 4 +- .../devices/ElectronicDevice.java | 2 +- .../{tv => devices}/devices/Radio.java | 2 +- .../{tv => devices}/devices/Television.java | 2 +- src/command/examples/spells/Assistant.java | 24 +++++++++ src/command/examples/spells/MagicAct.java | 42 ++++++++++++++++ src/command/examples/spells/Target.java | 48 ++++++++++++++++++ src/command/examples/spells/Wizard.java | 49 +++++++++++++++++++ src/command/examples/spells/commands/Age.java | 19 +++++++ .../examples/spells/commands/AgeSpell.java | 39 +++++++++++++++ .../examples/spells/commands/Command.java | 17 +++++++ .../spells/commands/InvisibilitySpell.java | 36 ++++++++++++++ .../examples/spells/commands/ShrinkSpell.java | 38 ++++++++++++++ .../examples/spells/commands/Size.java | 20 ++++++++ .../examples/spells/commands/Visibility.java | 20 ++++++++ 23 files changed, 379 insertions(+), 27 deletions(-) rename src/command/examples/{tv => devices}/Client.java (74%) rename src/command/examples/{tv => devices}/DeviceButton.java (79%) rename src/command/examples/{tv => devices}/commands/Command.java (74%) rename src/command/examples/{tv => devices}/commands/TurnOffAllDevices.java (83%) rename src/command/examples/{tv => devices}/commands/TurnOffTelevision.java (78%) rename src/command/examples/{tv => devices}/commands/TurnOnTelevision.java (78%) rename src/command/examples/{tv => devices}/commands/VolumeDownTelevision.java (79%) rename src/command/examples/{tv => devices}/commands/VolumeUpTelevision.java (79%) rename src/command/examples/{tv => devices}/devices/ElectronicDevice.java (81%) rename src/command/examples/{tv => devices}/devices/Radio.java (94%) rename src/command/examples/{tv => devices}/devices/Television.java (94%) create mode 100644 src/command/examples/spells/Assistant.java create mode 100644 src/command/examples/spells/MagicAct.java create mode 100644 src/command/examples/spells/Target.java create mode 100644 src/command/examples/spells/Wizard.java create mode 100644 src/command/examples/spells/commands/Age.java create mode 100644 src/command/examples/spells/commands/AgeSpell.java create mode 100644 src/command/examples/spells/commands/Command.java create mode 100644 src/command/examples/spells/commands/InvisibilitySpell.java create mode 100644 src/command/examples/spells/commands/ShrinkSpell.java create mode 100644 src/command/examples/spells/commands/Size.java create mode 100644 src/command/examples/spells/commands/Visibility.java diff --git a/README.md b/README.md index b6d411d..ca81a88 100644 --- a/README.md +++ b/README.md @@ -236,5 +236,5 @@ Extender la funcionalidad de los objetos se puede hacer de forma estática en nu * 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) +* [Electrónicos](https://github.com/LuisBurgos/design-patterns/tree/master/src/command/examples/devices) +* [Hechizos](https://github.com/LuisBurgos/design-patterns/tree/master/src/command/examples/spells) diff --git a/src/command/examples/tv/Client.java b/src/command/examples/devices/Client.java similarity index 74% rename from src/command/examples/tv/Client.java rename to src/command/examples/devices/Client.java index 6d03680..eaaf19c 100644 --- a/src/command/examples/tv/Client.java +++ b/src/command/examples/devices/Client.java @@ -1,12 +1,12 @@ -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; +package command.examples.devices; + +import command.examples.devices.commands.TurnOffAllDevices; +import command.examples.devices.commands.TurnOffTelevision; +import command.examples.devices.commands.TurnOnTelevision; +import command.examples.devices.commands.VolumeUpTelevision; +import command.examples.devices.devices.ElectronicDevice; +import command.examples.devices.devices.Radio; +import command.examples.devices.devices.Television; import java.util.ArrayList; import java.util.List; diff --git a/src/command/examples/tv/DeviceButton.java b/src/command/examples/devices/DeviceButton.java similarity index 79% rename from src/command/examples/tv/DeviceButton.java rename to src/command/examples/devices/DeviceButton.java index 736a546..7e01204 100644 --- a/src/command/examples/tv/DeviceButton.java +++ b/src/command/examples/devices/DeviceButton.java @@ -1,6 +1,6 @@ -package command.examples.tv; +package command.examples.devices; -import command.examples.tv.commands.Command; +import command.examples.devices.commands.Command; /** * The INVOKER diff --git a/src/command/examples/tv/commands/Command.java b/src/command/examples/devices/commands/Command.java similarity index 74% rename from src/command/examples/tv/commands/Command.java rename to src/command/examples/devices/commands/Command.java index 9448801..1c07db6 100644 --- a/src/command/examples/tv/commands/Command.java +++ b/src/command/examples/devices/commands/Command.java @@ -1,4 +1,4 @@ -package command.examples.tv.commands; +package command.examples.devices.commands; /** * Created by luisburgos on 13/08/15. diff --git a/src/command/examples/tv/commands/TurnOffAllDevices.java b/src/command/examples/devices/commands/TurnOffAllDevices.java similarity index 83% rename from src/command/examples/tv/commands/TurnOffAllDevices.java rename to src/command/examples/devices/commands/TurnOffAllDevices.java index 09a5098..9604c15 100644 --- a/src/command/examples/tv/commands/TurnOffAllDevices.java +++ b/src/command/examples/devices/commands/TurnOffAllDevices.java @@ -1,6 +1,6 @@ -package command.examples.tv.commands; +package command.examples.devices.commands; -import command.examples.tv.devices.ElectronicDevice; +import command.examples.devices.devices.ElectronicDevice; import java.util.List; diff --git a/src/command/examples/tv/commands/TurnOffTelevision.java b/src/command/examples/devices/commands/TurnOffTelevision.java similarity index 78% rename from src/command/examples/tv/commands/TurnOffTelevision.java rename to src/command/examples/devices/commands/TurnOffTelevision.java index f182f7f..d7276ef 100644 --- a/src/command/examples/tv/commands/TurnOffTelevision.java +++ b/src/command/examples/devices/commands/TurnOffTelevision.java @@ -1,6 +1,6 @@ -package command.examples.tv.commands; +package command.examples.devices.commands; -import command.examples.tv.devices.ElectronicDevice; +import command.examples.devices.devices.ElectronicDevice; /** * Created by luisburgos on 13/08/15. diff --git a/src/command/examples/tv/commands/TurnOnTelevision.java b/src/command/examples/devices/commands/TurnOnTelevision.java similarity index 78% rename from src/command/examples/tv/commands/TurnOnTelevision.java rename to src/command/examples/devices/commands/TurnOnTelevision.java index f088663..9b8620f 100644 --- a/src/command/examples/tv/commands/TurnOnTelevision.java +++ b/src/command/examples/devices/commands/TurnOnTelevision.java @@ -1,6 +1,6 @@ -package command.examples.tv.commands; +package command.examples.devices.commands; -import command.examples.tv.devices.ElectronicDevice; +import command.examples.devices.devices.ElectronicDevice; /** * Created by luisburgos on 13/08/15. diff --git a/src/command/examples/tv/commands/VolumeDownTelevision.java b/src/command/examples/devices/commands/VolumeDownTelevision.java similarity index 79% rename from src/command/examples/tv/commands/VolumeDownTelevision.java rename to src/command/examples/devices/commands/VolumeDownTelevision.java index c884180..976b075 100644 --- a/src/command/examples/tv/commands/VolumeDownTelevision.java +++ b/src/command/examples/devices/commands/VolumeDownTelevision.java @@ -1,6 +1,6 @@ -package command.examples.tv.commands; +package command.examples.devices.commands; -import command.examples.tv.devices.ElectronicDevice; +import command.examples.devices.devices.ElectronicDevice; /** * Created by luisburgos on 13/08/15. diff --git a/src/command/examples/tv/commands/VolumeUpTelevision.java b/src/command/examples/devices/commands/VolumeUpTelevision.java similarity index 79% rename from src/command/examples/tv/commands/VolumeUpTelevision.java rename to src/command/examples/devices/commands/VolumeUpTelevision.java index 888beb4..898508a 100644 --- a/src/command/examples/tv/commands/VolumeUpTelevision.java +++ b/src/command/examples/devices/commands/VolumeUpTelevision.java @@ -1,6 +1,6 @@ -package command.examples.tv.commands; +package command.examples.devices.commands; -import command.examples.tv.devices.ElectronicDevice; +import command.examples.devices.devices.ElectronicDevice; /** * Created by luisburgos on 13/08/15. diff --git a/src/command/examples/tv/devices/ElectronicDevice.java b/src/command/examples/devices/devices/ElectronicDevice.java similarity index 81% rename from src/command/examples/tv/devices/ElectronicDevice.java rename to src/command/examples/devices/devices/ElectronicDevice.java index d5007b7..ad8d1f7 100644 --- a/src/command/examples/tv/devices/ElectronicDevice.java +++ b/src/command/examples/devices/devices/ElectronicDevice.java @@ -1,4 +1,4 @@ -package command.examples.tv.devices; +package command.examples.devices.devices; /** * Created by luisburgos on 13/08/15. diff --git a/src/command/examples/tv/devices/Radio.java b/src/command/examples/devices/devices/Radio.java similarity index 94% rename from src/command/examples/tv/devices/Radio.java rename to src/command/examples/devices/devices/Radio.java index 457ff68..5d5664d 100644 --- a/src/command/examples/tv/devices/Radio.java +++ b/src/command/examples/devices/devices/Radio.java @@ -1,4 +1,4 @@ -package command.examples.tv.devices; +package command.examples.devices.devices; /** * Created by luisburgos on 13/08/15. diff --git a/src/command/examples/tv/devices/Television.java b/src/command/examples/devices/devices/Television.java similarity index 94% rename from src/command/examples/tv/devices/Television.java rename to src/command/examples/devices/devices/Television.java index b91a3dd..5008034 100644 --- a/src/command/examples/tv/devices/Television.java +++ b/src/command/examples/devices/devices/Television.java @@ -1,4 +1,4 @@ -package command.examples.tv.devices; +package command.examples.devices.devices; /** * Created by luisburgos on 13/08/15. diff --git a/src/command/examples/spells/Assistant.java b/src/command/examples/spells/Assistant.java new file mode 100644 index 0000000..10c812e --- /dev/null +++ b/src/command/examples/spells/Assistant.java @@ -0,0 +1,24 @@ +package command.examples.spells; + +import command.examples.spells.commands.Age; +import command.examples.spells.commands.Size; +import command.examples.spells.commands.Visibility; + +/** + * The RECEIVER + * Created by luisburgos on 14/08/15. + */ +public class Assistant extends Target { + + public Assistant() { + setSize(Size.NORMAL); + setVisibility(Visibility.VISIBLE); + setAge(Age.ADULT); + } + + @Override + public String toString() { + return "Assistant"; + } + +} \ No newline at end of file diff --git a/src/command/examples/spells/MagicAct.java b/src/command/examples/spells/MagicAct.java new file mode 100644 index 0000000..82bc893 --- /dev/null +++ b/src/command/examples/spells/MagicAct.java @@ -0,0 +1,42 @@ +package command.examples.spells; + +import command.examples.spells.commands.AgeSpell; +import command.examples.spells.commands.InvisibilitySpell; +import command.examples.spells.commands.ShrinkSpell; + +/** + * Main Magic Act. + * Created by luisburgos on 14/08/15. + */ +public class MagicAct { + + public static void main(String[] args) { + Wizard wizard = new Wizard(); + Assistant assistant = new Assistant(); + + assistant.printStatus(); + + wizard.castSpell(new ShrinkSpell(), assistant); + assistant.printStatus(); + + wizard.castSpell(new InvisibilitySpell(), assistant); + assistant.printStatus(); + + wizard.undoLastSpell(); + assistant.printStatus(); + + wizard.undoLastSpell(); + assistant.printStatus(); + + wizard.redoLastSpell(); + assistant.printStatus(); + + wizard.redoLastSpell(); + assistant.printStatus(); + + ///Add a new spell + wizard.castSpell(new AgeSpell(), assistant); + assistant.printStatus(); + } + +} diff --git a/src/command/examples/spells/Target.java b/src/command/examples/spells/Target.java new file mode 100644 index 0000000..1e63333 --- /dev/null +++ b/src/command/examples/spells/Target.java @@ -0,0 +1,48 @@ +package command.examples.spells; + +import command.examples.spells.commands.Age; +import command.examples.spells.commands.Size; +import command.examples.spells.commands.Visibility; + +/** + * Created by luisburgos on 14/08/15. + */ +public abstract class Target { + + private Size size; + private Visibility visibility; + private Age age; + + public Size getSize() { + return size; + } + + public void setSize(Size size) { + this.size = size; + } + + public Visibility getVisibility() { + return visibility; + } + + public void setVisibility(Visibility visibility) { + this.visibility = visibility; + } + + public Age getAge() { + return age; + } + + public void setAge(Age age) { + this.age = age; + } + + @Override + public abstract String toString(); + + public void printStatus() { + System.out.println(String.format("%s, Size: %s | Visibility: %s | Age: %s \n", this, + getSize(), getVisibility(), getAge())); + } + +} diff --git a/src/command/examples/spells/Wizard.java b/src/command/examples/spells/Wizard.java new file mode 100644 index 0000000..35a4369 --- /dev/null +++ b/src/command/examples/spells/Wizard.java @@ -0,0 +1,49 @@ +package command.examples.spells; + +import command.examples.spells.commands.Command; + +import java.util.Deque; +import java.util.LinkedList; + +/** + * The INVOKER + * Created by luisburgos on 14/08/15. + */ +public class Wizard { + + private Deque undoStack = new LinkedList<>(); + private Deque redoStack = new LinkedList<>(); + + public Wizard() { + } + + public void castSpell(Command command, Target target) { + System.out.println(this + " casts " + command + " at " + target); + command.execute(target); + undoStack.offerLast(command); + } + + public void undoLastSpell() { + if (!undoStack.isEmpty()) { + Command previousSpell = undoStack.pollLast(); + redoStack.offerLast(previousSpell); + System.out.println(this + " undoes " + previousSpell); + previousSpell.undo(); + } + } + + public void redoLastSpell() { + if (!redoStack.isEmpty()) { + Command previousSpell = redoStack.pollLast(); + undoStack.offerLast(previousSpell); + System.out.println(this + " redoes " + previousSpell); + previousSpell.redo(); + } + } + + @Override + public String toString() { + return "Wizard"; + } + +} diff --git a/src/command/examples/spells/commands/Age.java b/src/command/examples/spells/commands/Age.java new file mode 100644 index 0000000..fee4416 --- /dev/null +++ b/src/command/examples/spells/commands/Age.java @@ -0,0 +1,19 @@ +package command.examples.spells.commands; + +/** + * Created by luisburgos on 14/08/15. + */ +public enum Age { + CHILD("small"), ADULT("adult"), ELDER("elder"), UNDEFINED(""); + + private String title; + + Age(String title) { + this.title = title; + } + + @Override + public String toString() { + return title; + } +} diff --git a/src/command/examples/spells/commands/AgeSpell.java b/src/command/examples/spells/commands/AgeSpell.java new file mode 100644 index 0000000..80cb22e --- /dev/null +++ b/src/command/examples/spells/commands/AgeSpell.java @@ -0,0 +1,39 @@ +package command.examples.spells.commands; + +import command.examples.spells.Target; + +/** + * Created by luisburgos on 14/08/15. + */ +public class AgeSpell extends Command { + + private Age previousAge; + private Target target; + + + @Override + public void execute(Target target) { + previousAge = target.getAge(); + target.setAge(Age.ELDER); + this.target = target; + } + + @Override + public void undo() { + if (previousAge != null && target != null) { + Age temp = target.getAge(); + target.setAge(previousAge); + previousAge = temp; + } + } + + @Override + public void redo() { + undo(); + } + + @Override + public String toString() { + return "Age Spell"; + } +} diff --git a/src/command/examples/spells/commands/Command.java b/src/command/examples/spells/commands/Command.java new file mode 100644 index 0000000..9dfb3ea --- /dev/null +++ b/src/command/examples/spells/commands/Command.java @@ -0,0 +1,17 @@ +package command.examples.spells.commands; + +import command.examples.spells.Target; + +/** + * Created by luisburgos on 14/08/15. + */ +public abstract class Command { + + public abstract void execute(Target target); + public abstract void undo(); + public abstract void redo(); + + @Override + public abstract String toString(); + +} diff --git a/src/command/examples/spells/commands/InvisibilitySpell.java b/src/command/examples/spells/commands/InvisibilitySpell.java new file mode 100644 index 0000000..32a068e --- /dev/null +++ b/src/command/examples/spells/commands/InvisibilitySpell.java @@ -0,0 +1,36 @@ +package command.examples.spells.commands; + +import command.examples.spells.Target; + +/** + * Created by luisburgos on 14/08/15. + */ +public class InvisibilitySpell extends Command { + + private Target target; + + @Override + public void execute(Target target) { + target.setVisibility(Visibility.INVISIBLE); + this.target = target; + } + + @Override + public void undo() { + if (target != null) { + target.setVisibility(Visibility.VISIBLE); + } + } + + @Override + public void redo() { + if (target != null) { + target.setVisibility(Visibility.INVISIBLE); + } + } + + @Override + public String toString() { + return "Invisibility spell"; + } +} diff --git a/src/command/examples/spells/commands/ShrinkSpell.java b/src/command/examples/spells/commands/ShrinkSpell.java new file mode 100644 index 0000000..28b8723 --- /dev/null +++ b/src/command/examples/spells/commands/ShrinkSpell.java @@ -0,0 +1,38 @@ +package command.examples.spells.commands; + +import command.examples.spells.Target; + +/** + * Created by luisburgos on 14/08/15. + */ +public class ShrinkSpell extends Command { + + private Size oldSize; + private Target target; + + @Override + public void execute(Target target) { + oldSize = target.getSize(); + target.setSize(Size.SMALL); + this.target = target; + } + + @Override + public void undo() { + if (oldSize != null && target != null) { + Size temp = target.getSize(); + target.setSize(oldSize); + oldSize = temp; + } + } + + @Override + public void redo() { + undo(); + } + + @Override + public String toString() { + return "Shrink spell"; + } +} diff --git a/src/command/examples/spells/commands/Size.java b/src/command/examples/spells/commands/Size.java new file mode 100644 index 0000000..4cc3179 --- /dev/null +++ b/src/command/examples/spells/commands/Size.java @@ -0,0 +1,20 @@ +package command.examples.spells.commands; + +/** + * Created by luisburgos on 14/08/15. + */ +public enum Size { + + SMALL("small"), NORMAL("normal"), LARGE("large"), UNDEFINED(""); + + private String title; + + Size(String title) { + this.title = title; + } + + @Override + public String toString() { + return title; + } +} \ No newline at end of file diff --git a/src/command/examples/spells/commands/Visibility.java b/src/command/examples/spells/commands/Visibility.java new file mode 100644 index 0000000..9cfc80f --- /dev/null +++ b/src/command/examples/spells/commands/Visibility.java @@ -0,0 +1,20 @@ +package command.examples.spells.commands; + +/** + * Created by luisburgos on 14/08/15. + */ +public enum Visibility { + + VISIBLE("visible"), INVISIBLE("invisible"), UNDEFINED(""); + + private String title; + + Visibility(String title) { + this.title = title; + } + + @Override + public String toString() { + return title; + } +} \ No newline at end of file