added command sample

This commit is contained in:
Ilkka Seppala 2014-08-17 14:42:10 +03:00
parent f0e7f22266
commit 0785bccffb
11 changed files with 250 additions and 0 deletions

23
command/pom.xml Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>com.iluwatar</groupId>
<artifactId>command</artifactId>
<version>1.0-SNAPSHOT</version>
<name>command</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,20 @@
package com.iluwatar;
public class App
{
public static void main( String[] args )
{
Wizard wizard = new Wizard();
Goblin goblin = new Goblin();
goblin.printStatus();
wizard.castSpell(new ShrinkSpell(), goblin);
goblin.printStatus();
wizard.castSpell(new InvisibilitySpell(), goblin);
goblin.printStatus();
wizard.undoLastSpell();
goblin.printStatus();
}
}

View File

@ -0,0 +1,12 @@
package com.iluwatar;
public abstract class Command {
public abstract void execute(Target target);
public abstract void undo();
@Override
public abstract String toString();
}

View File

@ -0,0 +1,15 @@
package com.iluwatar;
public class Goblin extends Target {
public Goblin() {
this.setSize(Size.NORMAL);
this.setVisibility(Visibility.VISIBLE);
}
@Override
public String toString() {
return "Goblin";
}
}

View File

@ -0,0 +1,28 @@
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);
this.target = target;
}
@Override
public void undo() {
if (target != null) {
target.setVisibility(Visibility.VISIBLE);
}
}
@Override
public String toString() {
return "Invisibility spell";
}
}

View File

@ -0,0 +1,32 @@
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();
target.setSize(Size.SMALL);
this.target = target;
}
@Override
public void undo() {
if (oldSize != null && target != null) {
target.setSize(oldSize);
}
}
@Override
public String toString() {
return "Shrink spell";
}
}

View File

@ -0,0 +1,31 @@
package com.iluwatar;
public enum Size {
SMALL,
NORMAL,
LARGE;
@Override
public String toString() {
String s = "";
switch (this) {
case LARGE:
s = "large";
break;
case NORMAL:
s = "normal";
break;
case SMALL:
s = "small";
break;
default:
break;
}
return s;
}
}

View File

@ -0,0 +1,31 @@
package com.iluwatar;
public abstract class Target {
private Size size;
private Visibility visibility;
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;
}
@Override
public abstract String toString();
public void printStatus() {
System.out.println(String.format("%s, size=%s visibility=%s", this, getSize(), getVisibility()));
}
}

View File

@ -0,0 +1,26 @@
package com.iluwatar;
public enum Visibility {
VISIBLE,
INVISIBLE;
@Override
public String toString() {
String s = "";
switch (this) {
case INVISIBLE:
s = "invisible";
break;
case VISIBLE:
s = "visible";
break;
default:
break;
}
return s;
}
}

View File

@ -0,0 +1,31 @@
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);
previousSpell = command;
}
public void undoLastSpell() {
if (previousSpell != null) {
System.out.println(this + " undoes " + previousSpell);
previousSpell.undo();
}
}
@Override
public String toString() {
return "Wizard";
}
}

View File

@ -31,6 +31,7 @@
<module>flyweight</module> <module>flyweight</module>
<module>proxy</module> <module>proxy</module>
<module>chain</module> <module>chain</module>
<module>command</module>
</modules> </modules>
<build> <build>