File tree Expand file tree Collapse file tree 5 files changed +118
-109
lines changed
main/java/com/iluwatar/templatemethod
test/java/com/iluwatar/templatemethod Expand file tree Collapse file tree 5 files changed +118
-109
lines changed Original file line number Diff line number Diff line change 1- package com .iluwatar .templatemethod ;
2-
3- /**
4- *
5- * Template Method defines a skeleton for an algorithm. The algorithm subclasses
6- * provide implementation for the blank parts.
7- *
8- * In this example HalflingThief contains StealingMethod that can be changed.
9- * First the thief hits with HitAndRunMethod and then with SubtleMethod.
10- *
11- */
12- public class App {
13-
14- public static void main (String [] args ) {
15- HalflingThief thief = new HalflingThief (new HitAndRunMethod ());
16- thief .steal ();
17- thief .changeMethod (new SubtleMethod ());
18- thief .steal ();
19- }
20- }
1+ package com .iluwatar .templatemethod ;
2+
3+ /**
4+ *
5+ * Template Method defines a skeleton for an algorithm. The algorithm subclasses
6+ * provide implementation for the blank parts.
7+ * <p>
8+ * In this example {@link HalflingThief} contains {@link StealingMethod} that can be changed.
9+ * First the thief hits with {@link HitAndRunMethod} and then with {@link SubtleMethod}.
10+ *
11+ */
12+ public class App {
13+
14+ /**
15+ * Program entry point
16+ * @param args command line args
17+ */
18+ public static void main (String [] args ) {
19+ HalflingThief thief = new HalflingThief (new HitAndRunMethod ());
20+ thief .steal ();
21+ thief .changeMethod (new SubtleMethod ());
22+ thief .steal ();
23+ }
24+ }
Original file line number Diff line number Diff line change 1- package com .iluwatar .templatemethod ;
2-
3- /**
4- *
5- * Halfling thief uses StealingMethod to steal.
6- *
7- */
8- public class HalflingThief {
9-
10- private StealingMethod method ;
11-
12- public HalflingThief (StealingMethod method ) {
13- this .method = method ;
14- }
15-
16- public void steal () {
17- method .steal ();
18- }
19-
20- public void changeMethod (StealingMethod method ) {
21- this .method = method ;
22- }
23- }
1+ package com .iluwatar .templatemethod ;
2+
3+ /**
4+ *
5+ * Halfling thief uses {@link StealingMethod} to steal.
6+ *
7+ */
8+ public class HalflingThief {
9+
10+ private StealingMethod method ;
11+
12+ public HalflingThief (StealingMethod method ) {
13+ this .method = method ;
14+ }
15+
16+ public void steal () {
17+ method .steal ();
18+ }
19+
20+ public void changeMethod (StealingMethod method ) {
21+ this .method = method ;
22+ }
23+ }
Original file line number Diff line number Diff line change 1- package com .iluwatar .templatemethod ;
2-
3- /**
4- *
5- * HitAndRunMethod implementation of StealingMethod.
6- *
7- */
8- public class HitAndRunMethod extends StealingMethod {
9-
10- @ Override
11- protected String pickTarget () {
12- return "old goblin woman" ;
13- }
14-
15- @ Override
16- protected void confuseTarget (String target ) {
17- System .out .println ("Approach the " + target + " from behind." );
18- }
19-
20- @ Override
21- protected void stealTheItem (String target ) {
22- System .out .println ("Grab the handbag and run away fast!" );
23- }
24-
25- }
1+ package com .iluwatar .templatemethod ;
2+
3+ /**
4+ *
5+ * HitAndRunMethod implementation of {@link StealingMethod} .
6+ *
7+ */
8+ public class HitAndRunMethod extends StealingMethod {
9+
10+ @ Override
11+ protected String pickTarget () {
12+ return "old goblin woman" ;
13+ }
14+
15+ @ Override
16+ protected void confuseTarget (String target ) {
17+ System .out .println ("Approach the " + target + " from behind." );
18+ }
19+
20+ @ Override
21+ protected void stealTheItem (String target ) {
22+ System .out .println ("Grab the handbag and run away fast!" );
23+ }
24+
25+ }
Original file line number Diff line number Diff line change 1- package com .iluwatar .templatemethod ;
2-
3- /**
4- *
5- * SubtleMethod implementation of StealingMethod.
6- *
7- */
8- public class SubtleMethod extends StealingMethod {
9-
10- @ Override
11- protected String pickTarget () {
12- return "shop keeper" ;
13- }
14-
15- @ Override
16- protected void confuseTarget (String target ) {
17- System .out .println ("Approach the " + target
18- + " with tears running and hug him!" );
19- }
20-
21- @ Override
22- protected void stealTheItem (String target ) {
23- System .out .println ("While in close contact grab the " + target
24- + "'s wallet." );
25- }
26-
27- }
1+ package com .iluwatar .templatemethod ;
2+
3+ /**
4+ *
5+ * SubtleMethod implementation of {@link StealingMethod} .
6+ *
7+ */
8+ public class SubtleMethod extends StealingMethod {
9+
10+ @ Override
11+ protected String pickTarget () {
12+ return "shop keeper" ;
13+ }
14+
15+ @ Override
16+ protected void confuseTarget (String target ) {
17+ System .out .println ("Approach the " + target
18+ + " with tears running and hug him!" );
19+ }
20+
21+ @ Override
22+ protected void stealTheItem (String target ) {
23+ System .out .println ("While in close contact grab the " + target
24+ + "'s wallet." );
25+ }
26+
27+ }
Original file line number Diff line number Diff line change 1- package com .iluwatar .templatemethod ;
2-
3- import org .junit .Test ;
4-
5- import com .iluwatar .templatemethod .App ;
6-
7- public class AppTest {
8-
9- @ Test
10- public void test () {
11- String [] args = {};
12- App .main (args );
13- }
14- }
1+ package com .iluwatar .templatemethod ;
2+
3+ import org .junit .Test ;
4+
5+ import com .iluwatar .templatemethod .App ;
6+
7+ /**
8+ *
9+ * Application test
10+ *
11+ */
12+ public class AppTest {
13+
14+ @ Test
15+ public void test () {
16+ String [] args = {};
17+ App .main (args );
18+ }
19+ }
You can’t perform that action at this time.
0 commit comments