forked from luisburgos/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Command] Added spells example, rename device folder.
- Loading branch information
1 parent
4200062
commit 4f545ce
Showing
23 changed files
with
379 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 9 additions & 9 deletions
18
src/command/examples/tv/Client.java → src/command/examples/devices/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
src/command/examples/tv/DeviceButton.java → ...ommand/examples/devices/DeviceButton.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...command/examples/tv/commands/Command.java → ...nd/examples/devices/commands/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...amples/tv/commands/TurnOffAllDevices.java → ...s/devices/commands/TurnOffAllDevices.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...amples/tv/commands/TurnOffTelevision.java → ...s/devices/commands/TurnOffTelevision.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...xamples/tv/commands/TurnOnTelevision.java → ...es/devices/commands/TurnOnTelevision.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...les/tv/commands/VolumeDownTelevision.java → ...evices/commands/VolumeDownTelevision.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...mples/tv/commands/VolumeUpTelevision.java → .../devices/commands/VolumeUpTelevision.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...examples/tv/devices/ElectronicDevice.java → ...les/devices/devices/ElectronicDevice.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/command/examples/tv/devices/Radio.java → ...mmand/examples/devices/devices/Radio.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...mmand/examples/tv/devices/Television.java → .../examples/devices/devices/Television.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Command> undoStack = new LinkedList<>(); | ||
private Deque<Command> 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"; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
src/command/examples/spells/commands/InvisibilitySpell.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} |
Oops, something went wrong.