X Tutup
Skip to content

Commit 0785bcc

Browse files
committed
added command sample
1 parent f0e7f22 commit 0785bcc

File tree

11 files changed

+250
-0
lines changed

11 files changed

+250
-0
lines changed

command/pom.xml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<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"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.iluwatar</groupId>
7+
<artifactId>java-design-patterns</artifactId>
8+
<version>1.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.iluwatar</groupId>
11+
<artifactId>command</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>command</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
</dependencies>
23+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
Wizard wizard = new Wizard();
8+
Goblin goblin = new Goblin();
9+
10+
goblin.printStatus();
11+
12+
wizard.castSpell(new ShrinkSpell(), goblin);
13+
goblin.printStatus();
14+
15+
wizard.castSpell(new InvisibilitySpell(), goblin);
16+
goblin.printStatus();
17+
wizard.undoLastSpell();
18+
goblin.printStatus();
19+
}
20+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar;
2+
3+
public abstract class Command {
4+
5+
public abstract void execute(Target target);
6+
7+
public abstract void undo();
8+
9+
@Override
10+
public abstract String toString();
11+
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar;
2+
3+
public class Goblin extends Target {
4+
5+
public Goblin() {
6+
this.setSize(Size.NORMAL);
7+
this.setVisibility(Visibility.VISIBLE);
8+
}
9+
10+
@Override
11+
public String toString() {
12+
return "Goblin";
13+
}
14+
15+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.iluwatar;
2+
3+
public class InvisibilitySpell extends Command {
4+
5+
private Target target;
6+
7+
public InvisibilitySpell() {
8+
target = null;
9+
}
10+
11+
@Override
12+
public void execute(Target target) {
13+
target.setVisibility(Visibility.INVISIBLE);
14+
this.target = target;
15+
}
16+
17+
@Override
18+
public void undo() {
19+
if (target != null) {
20+
target.setVisibility(Visibility.VISIBLE);
21+
}
22+
}
23+
24+
@Override
25+
public String toString() {
26+
return "Invisibility spell";
27+
}
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.iluwatar;
2+
3+
public class ShrinkSpell extends Command {
4+
5+
private Size oldSize;
6+
7+
private Target target;
8+
9+
public ShrinkSpell() {
10+
oldSize = null;
11+
target = null;
12+
}
13+
14+
@Override
15+
public void execute(Target target) {
16+
oldSize = target.getSize();
17+
target.setSize(Size.SMALL);
18+
this.target = target;
19+
}
20+
21+
@Override
22+
public void undo() {
23+
if (oldSize != null && target != null) {
24+
target.setSize(oldSize);
25+
}
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return "Shrink spell";
31+
}
32+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.iluwatar;
2+
3+
public enum Size {
4+
5+
SMALL,
6+
NORMAL,
7+
LARGE;
8+
9+
@Override
10+
public String toString() {
11+
12+
String s = "";
13+
14+
switch (this) {
15+
case LARGE:
16+
s = "large";
17+
break;
18+
case NORMAL:
19+
s = "normal";
20+
break;
21+
case SMALL:
22+
s = "small";
23+
break;
24+
default:
25+
break;
26+
}
27+
return s;
28+
}
29+
30+
31+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.iluwatar;
2+
3+
public abstract class Target {
4+
5+
private Size size;
6+
7+
private Visibility visibility;
8+
9+
public Size getSize() {
10+
return size;
11+
}
12+
13+
public void setSize(Size size) {
14+
this.size = size;
15+
}
16+
17+
public Visibility getVisibility() {
18+
return visibility;
19+
}
20+
21+
public void setVisibility(Visibility visibility) {
22+
this.visibility = visibility;
23+
}
24+
25+
@Override
26+
public abstract String toString();
27+
28+
public void printStatus() {
29+
System.out.println(String.format("%s, size=%s visibility=%s", this, getSize(), getVisibility()));
30+
}
31+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.iluwatar;
2+
3+
public enum Visibility {
4+
5+
VISIBLE,
6+
INVISIBLE;
7+
8+
@Override
9+
public String toString() {
10+
11+
String s = "";
12+
13+
switch (this) {
14+
case INVISIBLE:
15+
s = "invisible";
16+
break;
17+
case VISIBLE:
18+
s = "visible";
19+
break;
20+
default:
21+
break;
22+
23+
}
24+
return s;
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.iluwatar;
2+
3+
public class Wizard extends Target {
4+
5+
private Command previousSpell;
6+
7+
public Wizard() {
8+
this.setSize(Size.NORMAL);
9+
this.setVisibility(Visibility.VISIBLE);
10+
previousSpell = null;
11+
}
12+
13+
public void castSpell(Command command, Target target) {
14+
System.out.println(this + " casts " + command + " at " + target);
15+
command.execute(target);
16+
previousSpell = command;
17+
}
18+
19+
public void undoLastSpell() {
20+
if (previousSpell != null) {
21+
System.out.println(this + " undoes " + previousSpell);
22+
previousSpell.undo();
23+
}
24+
}
25+
26+
@Override
27+
public String toString() {
28+
return "Wizard";
29+
}
30+
31+
}

0 commit comments

Comments
 (0)
X Tutup