File tree Expand file tree Collapse file tree 7 files changed +111
-0
lines changed
src/main/java/com/iluwatar Expand file tree Collapse file tree 7 files changed +111
-0
lines changed Original file line number Diff line number Diff line change 3939 <module >observer</module >
4040 <module >state</module >
4141 <module >strategy</module >
42+ <module >template-method</module >
4243 </modules >
4344
4445<build >
Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments