From 23e2faeca228ad165f3e70a3f5153c1e1845a929 Mon Sep 17 00:00:00 2001 From: Ilkka Seppala Date: Sat, 23 Aug 2014 13:37:42 +0300 Subject: [PATCH] added template method sample --- pom.xml | 1 + template-method/pom.xml | 23 +++++++++++++++++++ .../src/main/java/com/iluwatar/App.java | 12 ++++++++++ .../main/java/com/iluwatar/HalflingThief.java | 18 +++++++++++++++ .../java/com/iluwatar/HitAndRunMethod.java | 20 ++++++++++++++++ .../java/com/iluwatar/StealingMethod.java | 17 ++++++++++++++ .../main/java/com/iluwatar/SubtleMethod.java | 20 ++++++++++++++++ 7 files changed, 111 insertions(+) create mode 100644 template-method/pom.xml create mode 100644 template-method/src/main/java/com/iluwatar/App.java create mode 100644 template-method/src/main/java/com/iluwatar/HalflingThief.java create mode 100644 template-method/src/main/java/com/iluwatar/HitAndRunMethod.java create mode 100644 template-method/src/main/java/com/iluwatar/StealingMethod.java create mode 100644 template-method/src/main/java/com/iluwatar/SubtleMethod.java diff --git a/pom.xml b/pom.xml index de4bd0ed8..298732fd5 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,7 @@ observer state strategy + template-method diff --git a/template-method/pom.xml b/template-method/pom.xml new file mode 100644 index 000000000..90fde692d --- /dev/null +++ b/template-method/pom.xml @@ -0,0 +1,23 @@ + + + 4.0.0 + + com.iluwatar + java-design-patterns + 1.0-SNAPSHOT + + com.iluwatar + template-method + 1.0-SNAPSHOT + template-method + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + diff --git a/template-method/src/main/java/com/iluwatar/App.java b/template-method/src/main/java/com/iluwatar/App.java new file mode 100644 index 000000000..7360eddf6 --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/App.java @@ -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(); + } +} diff --git a/template-method/src/main/java/com/iluwatar/HalflingThief.java b/template-method/src/main/java/com/iluwatar/HalflingThief.java new file mode 100644 index 000000000..64604a8b2 --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/HalflingThief.java @@ -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; + } +} diff --git a/template-method/src/main/java/com/iluwatar/HitAndRunMethod.java b/template-method/src/main/java/com/iluwatar/HitAndRunMethod.java new file mode 100644 index 000000000..99c0d61d1 --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/HitAndRunMethod.java @@ -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!"); + } + +} diff --git a/template-method/src/main/java/com/iluwatar/StealingMethod.java b/template-method/src/main/java/com/iluwatar/StealingMethod.java new file mode 100644 index 000000000..f24de7588 --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/StealingMethod.java @@ -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); + } +} diff --git a/template-method/src/main/java/com/iluwatar/SubtleMethod.java b/template-method/src/main/java/com/iluwatar/SubtleMethod.java new file mode 100644 index 000000000..8967a92d6 --- /dev/null +++ b/template-method/src/main/java/com/iluwatar/SubtleMethod.java @@ -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."); + } + +}