X Tutup
Skip to content

Commit 23e2fae

Browse files
committed
added template method sample
1 parent 6157f22 commit 23e2fae

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-0
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
<module>observer</module>
4040
<module>state</module>
4141
<module>strategy</module>
42+
<module>template-method</module>
4243
</modules>
4344

4445
<build>

template-method/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>template-method</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>template-method</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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iluwatar;
2+
3+
public class App
4+
{
5+
public static void main( String[] args )
6+
{
7+
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
8+
thief.steal();
9+
thief.changeMethod(new SubtleMethod());
10+
thief.steal();
11+
}
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.iluwatar;
2+
3+
public class HalflingThief {
4+
5+
private StealingMethod method;
6+
7+
public HalflingThief(StealingMethod method) {
8+
this.method = method;
9+
}
10+
11+
public void steal() {
12+
method.steal();
13+
}
14+
15+
public void changeMethod(StealingMethod method) {
16+
this.method = method;
17+
}
18+
}
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 HitAndRunMethod extends StealingMethod {
4+
5+
@Override
6+
protected String pickTarget() {
7+
return "old goblin woman";
8+
}
9+
10+
@Override
11+
protected void confuseTarget(String target) {
12+
System.out.println("Approach the " + target + " from behind.");
13+
}
14+
15+
@Override
16+
protected void stealTheItem(String target) {
17+
System.out.println("Grab the handbag and run away fast!");
18+
}
19+
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.iluwatar;
2+
3+
public abstract class StealingMethod {
4+
5+
protected abstract String pickTarget();
6+
7+
protected abstract void confuseTarget(String target);
8+
9+
protected abstract void stealTheItem(String target);
10+
11+
public void steal() {
12+
String target = pickTarget();
13+
System.out.println("The target has been chosen as " + target + ".");
14+
confuseTarget(target);
15+
stealTheItem(target);
16+
}
17+
}
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 SubtleMethod extends StealingMethod {
4+
5+
@Override
6+
protected String pickTarget() {
7+
return "shop keeper";
8+
}
9+
10+
@Override
11+
protected void confuseTarget(String target) {
12+
System.out.println("Approach the " + target + " with tears running and hug him!");
13+
}
14+
15+
@Override
16+
protected void stealTheItem(String target) {
17+
System.out.println("While in close contact grab the " + target + "'s wallet.");
18+
}
19+
20+
}

0 commit comments

Comments
 (0)
X Tutup