added template method sample
This commit is contained in:
parent
6157f22ea4
commit
23e2faeca2
1
pom.xml
1
pom.xml
@ -39,6 +39,7 @@
|
|||||||
<module>observer</module>
|
<module>observer</module>
|
||||||
<module>state</module>
|
<module>state</module>
|
||||||
<module>strategy</module>
|
<module>strategy</module>
|
||||||
|
<module>template-method</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
23
template-method/pom.xml
Normal file
23
template-method/pom.xml
Normal 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>template-method</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>template-method</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>
|
12
template-method/src/main/java/com/iluwatar/App.java
Normal file
12
template-method/src/main/java/com/iluwatar/App.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class App
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
HalflingThief thief = new HalflingThief(new HitAndRunMethod());
|
||||||
|
thief.steal();
|
||||||
|
thief.changeMethod(new SubtleMethod());
|
||||||
|
thief.steal();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,18 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class HalflingThief {
|
||||||
|
|
||||||
|
private StealingMethod method;
|
||||||
|
|
||||||
|
public HalflingThief(StealingMethod method) {
|
||||||
|
this.method = method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void steal() {
|
||||||
|
method.steal();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void changeMethod(StealingMethod method) {
|
||||||
|
this.method = method;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,20 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class HitAndRunMethod extends StealingMethod {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String pickTarget() {
|
||||||
|
return "old goblin woman";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void confuseTarget(String target) {
|
||||||
|
System.out.println("Approach the " + target + " from behind.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void stealTheItem(String target) {
|
||||||
|
System.out.println("Grab the handbag and run away fast!");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public abstract class StealingMethod {
|
||||||
|
|
||||||
|
protected abstract String pickTarget();
|
||||||
|
|
||||||
|
protected abstract void confuseTarget(String target);
|
||||||
|
|
||||||
|
protected abstract void stealTheItem(String target);
|
||||||
|
|
||||||
|
public void steal() {
|
||||||
|
String target = pickTarget();
|
||||||
|
System.out.println("The target has been chosen as " + target + ".");
|
||||||
|
confuseTarget(target);
|
||||||
|
stealTheItem(target);
|
||||||
|
}
|
||||||
|
}
|
20
template-method/src/main/java/com/iluwatar/SubtleMethod.java
Normal file
20
template-method/src/main/java/com/iluwatar/SubtleMethod.java
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package com.iluwatar;
|
||||||
|
|
||||||
|
public class SubtleMethod extends StealingMethod {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String pickTarget() {
|
||||||
|
return "shop keeper";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void confuseTarget(String target) {
|
||||||
|
System.out.println("Approach the " + target + " with tears running and hug him!");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void stealTheItem(String target) {
|
||||||
|
System.out.println("While in close contact grab the " + target + "'s wallet.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user