Skip to content

Commit 172964e

Browse files
committed
iluwatar#590 explanation for Template Method
1 parent ab4e53a commit 172964e

1 file changed

Lines changed: 108 additions & 4 deletions

File tree

‎template-method/README.md‎

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,115 @@ tags:
99
---
1010

1111
## Intent
12-
Define the skeleton of an algorithm in an operation, deferring some
13-
steps to subclasses. Template method lets subclasses redefine certain steps of
14-
an algorithm without changing the algorithm's structure.
12+
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template method lets
13+
subclasses redefine certain steps of an algorithm without changing the algorithm's structure.
1514

16-
To make sure that subclasses don’t override the template method, the template method should be declared `final`.
15+
## Explanation
16+
Real world example
17+
18+
> The general steps in stealing an item are the same. First you pick the target, next you confuse him somehow and finally you steal the item. However there are many ways to implement these steps.
19+
20+
In plain words
21+
22+
> Template Method pattern outlines the general steps in the parent class and lets the concrete child implementations define the details.
23+
24+
Wikipedia says
25+
26+
> In object-oriented programming, the template method is one of the behavioral design patterns identified by Gamma et al. in the book Design Patterns. The template method is a method in a superclass, usually an abstract superclass, and defines the skeleton of an operation in terms of a number of high-level steps. These steps are themselves implemented by additional helper methods in the same class as the template method.
27+
28+
**Programmatic Example**
29+
30+
Let's first introduce the template method class along with its concrete implementations.
31+
32+
```java
33+
public abstract class StealingMethod {
34+
35+
private static final Logger LOGGER = LoggerFactory.getLogger(StealingMethod.class);
36+
37+
protected abstract String pickTarget();
38+
39+
protected abstract void confuseTarget(String target);
40+
41+
protected abstract void stealTheItem(String target);
42+
43+
public void steal() {
44+
var target = pickTarget();
45+
LOGGER.info("The target has been chosen as {}.", target);
46+
confuseTarget(target);
47+
stealTheItem(target);
48+
}
49+
}
50+
51+
public class SubtleMethod extends StealingMethod {
52+
53+
private static final Logger LOGGER = LoggerFactory.getLogger(SubtleMethod.class);
54+
55+
@Override
56+
protected String pickTarget() {
57+
return "shop keeper";
58+
}
59+
60+
@Override
61+
protected void confuseTarget(String target) {
62+
LOGGER.info("Approach the {} with tears running and hug him!", target);
63+
}
64+
65+
@Override
66+
protected void stealTheItem(String target) {
67+
LOGGER.info("While in close contact grab the {}'s wallet.", target);
68+
}
69+
}
70+
71+
public class HitAndRunMethod extends StealingMethod {
72+
73+
private static final Logger LOGGER = LoggerFactory.getLogger(HitAndRunMethod.class);
74+
75+
@Override
76+
protected String pickTarget() {
77+
return "old goblin woman";
78+
}
79+
80+
@Override
81+
protected void confuseTarget(String target) {
82+
LOGGER.info("Approach the {} from behind.", target);
83+
}
84+
85+
@Override
86+
protected void stealTheItem(String target) {
87+
LOGGER.info("Grab the handbag and run away fast!");
88+
}
89+
}
90+
```
91+
92+
Here's the halfling thief class containing the template method.
93+
94+
```java
95+
public class HalflingThief {
96+
97+
private StealingMethod method;
98+
99+
public HalflingThief(StealingMethod method) {
100+
this.method = method;
101+
}
102+
103+
public void steal() {
104+
method.steal();
105+
}
106+
107+
public void changeMethod(StealingMethod method) {
108+
this.method = method;
109+
}
110+
}
111+
```
112+
113+
And finally we show how the halfling thief utilizes the different stealing methods.
114+
115+
```java
116+
var thief = new HalflingThief(new HitAndRunMethod());
117+
thief.steal();
118+
thief.changeMethod(new SubtleMethod());
119+
thief.steal();
120+
```
17121

18122
## Class diagram
19123
![alt text](./etc/template-method_1.png "Template Method")

0 commit comments

Comments
 (0)