Skip to content

Commit

Permalink
[Command] Added spells example, rename device folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
luisburgos committed Aug 14, 2015
1 parent 4200062 commit 4f545ce
Show file tree
Hide file tree
Showing 23 changed files with 379 additions and 27 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command.examples.tv.commands;
package command.examples.devices.commands;

/**
* Created by luisburgos on 13/08/15.
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command.examples.tv.devices;
package command.examples.devices.devices;

/**
* Created by luisburgos on 13/08/15.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command.examples.tv.devices;
package command.examples.devices.devices;

/**
* Created by luisburgos on 13/08/15.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package command.examples.tv.devices;
package command.examples.devices.devices;

/**
* Created by luisburgos on 13/08/15.
Expand Down
24 changes: 24 additions & 0 deletions src/command/examples/spells/Assistant.java
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";
}

}
42 changes: 42 additions & 0 deletions src/command/examples/spells/MagicAct.java
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();
}

}
48 changes: 48 additions & 0 deletions src/command/examples/spells/Target.java
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()));
}

}
49 changes: 49 additions & 0 deletions src/command/examples/spells/Wizard.java
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";
}

}
19 changes: 19 additions & 0 deletions src/command/examples/spells/commands/Age.java
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;
}
}
39 changes: 39 additions & 0 deletions src/command/examples/spells/commands/AgeSpell.java
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";
}
}
17 changes: 17 additions & 0 deletions src/command/examples/spells/commands/Command.java
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 src/command/examples/spells/commands/InvisibilitySpell.java
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";
}
}
Loading

0 comments on commit 4f545ce

Please sign in to comment.