Formatted all files to the same standard
This commit is contained in:
@ -1,25 +1,25 @@
|
||||
package com.iluwatar;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* In Command pattern actions are objects that can be executed and undone. The
|
||||
* commands in this example are spells cast by the wizard on the goblin.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public class App {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Wizard wizard = new Wizard();
|
||||
Goblin goblin = new Goblin();
|
||||
public static void main(String[] args) {
|
||||
Wizard wizard = new Wizard();
|
||||
Goblin goblin = new Goblin();
|
||||
|
||||
goblin.printStatus();
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||
goblin.printStatus();
|
||||
wizard.castSpell(new ShrinkSpell(), goblin);
|
||||
goblin.printStatus();
|
||||
|
||||
wizard.castSpell(new InvisibilitySpell(), goblin);
|
||||
goblin.printStatus();
|
||||
wizard.undoLastSpell();
|
||||
goblin.printStatus();
|
||||
}
|
||||
wizard.castSpell(new InvisibilitySpell(), goblin);
|
||||
goblin.printStatus();
|
||||
wizard.undoLastSpell();
|
||||
goblin.printStatus();
|
||||
}
|
||||
}
|
||||
|
@ -3,15 +3,15 @@ package com.iluwatar;
|
||||
/**
|
||||
*
|
||||
* Interface for spells.
|
||||
*
|
||||
*
|
||||
*/
|
||||
public abstract class Command {
|
||||
|
||||
public abstract void execute(Target target);
|
||||
|
||||
|
||||
public abstract void undo();
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
|
||||
public abstract String toString();
|
||||
|
||||
}
|
||||
|
@ -6,7 +6,7 @@ public class Goblin extends Target {
|
||||
this.setSize(Size.NORMAL);
|
||||
this.setVisibility(Visibility.VISIBLE);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Goblin";
|
||||
|
@ -3,11 +3,11 @@ package com.iluwatar;
|
||||
public class InvisibilitySpell extends Command {
|
||||
|
||||
private Target target;
|
||||
|
||||
|
||||
public InvisibilitySpell() {
|
||||
target = null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(Target target) {
|
||||
target.setVisibility(Visibility.INVISIBLE);
|
||||
|
@ -3,14 +3,14 @@ package com.iluwatar;
|
||||
public class ShrinkSpell extends Command {
|
||||
|
||||
private Size oldSize;
|
||||
|
||||
|
||||
private Target target;
|
||||
|
||||
|
||||
public ShrinkSpell() {
|
||||
oldSize = null;
|
||||
target = null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void execute(Target target) {
|
||||
oldSize = target.getSize();
|
||||
|
@ -2,15 +2,13 @@ package com.iluwatar;
|
||||
|
||||
public enum Size {
|
||||
|
||||
SMALL,
|
||||
NORMAL,
|
||||
LARGE;
|
||||
SMALL, NORMAL, LARGE;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
|
||||
String s = "";
|
||||
|
||||
|
||||
switch (this) {
|
||||
case LARGE:
|
||||
s = "large";
|
||||
@ -27,5 +25,4 @@ public enum Size {
|
||||
return s;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ public abstract class Target {
|
||||
private Size size;
|
||||
|
||||
private Visibility visibility;
|
||||
|
||||
|
||||
public Size getSize() {
|
||||
return size;
|
||||
}
|
||||
@ -24,8 +24,9 @@ public abstract class Target {
|
||||
|
||||
@Override
|
||||
public abstract String toString();
|
||||
|
||||
|
||||
public void printStatus() {
|
||||
System.out.println(String.format("%s, size=%s visibility=%s", this, getSize(), getVisibility()));
|
||||
System.out.println(String.format("%s, size=%s visibility=%s", this,
|
||||
getSize(), getVisibility()));
|
||||
}
|
||||
}
|
||||
|
@ -2,14 +2,13 @@ package com.iluwatar;
|
||||
|
||||
public enum Visibility {
|
||||
|
||||
VISIBLE,
|
||||
INVISIBLE;
|
||||
VISIBLE, INVISIBLE;
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
|
||||
|
||||
String s = "";
|
||||
|
||||
|
||||
switch (this) {
|
||||
case INVISIBLE:
|
||||
s = "invisible";
|
||||
@ -19,7 +18,7 @@ public enum Visibility {
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
@ -3,13 +3,13 @@ package com.iluwatar;
|
||||
public class Wizard extends Target {
|
||||
|
||||
private Command previousSpell;
|
||||
|
||||
|
||||
public Wizard() {
|
||||
this.setSize(Size.NORMAL);
|
||||
this.setVisibility(Visibility.VISIBLE);
|
||||
previousSpell = null;
|
||||
}
|
||||
|
||||
|
||||
public void castSpell(Command command, Target target) {
|
||||
System.out.println(this + " casts " + command + " at " + target);
|
||||
command.execute(target);
|
||||
@ -22,10 +22,10 @@ public class Wizard extends Target {
|
||||
previousSpell.undo();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Wizard";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user