Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
Real-world example
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.
In plain words
Template Method pattern outlines the general steps in the parent class and lets the concrete child implementations define the details.
Wikipedia says
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.
Programmatic Example
Let’s first introduce the template method class along with its concrete implementations.
To make sure that subclasses don’t override the template method, the template method (in our case
method steal
) should be declared final
, otherwise the skeleton defined in the base class could
be overridden in subclasses.
1@Slf4j
2public abstract class StealingMethod {
3
4 protected abstract String pickTarget();
5
6 protected abstract void confuseTarget(String target);
7
8 protected abstract void stealTheItem(String target);
9
10 public final void steal() {
11 var target = pickTarget();
12 LOGGER.info("The target has been chosen as {}.", target);
13 confuseTarget(target);
14 stealTheItem(target);
15 }
16}
17
18@Slf4j
19public class SubtleMethod extends StealingMethod {
20
21 @Override
22 protected String pickTarget() {
23 return "shop keeper";
24 }
25
26 @Override
27 protected void confuseTarget(String target) {
28 LOGGER.info("Approach the {} with tears running and hug him!", target);
29 }
30
31 @Override
32 protected void stealTheItem(String target) {
33 LOGGER.info("While in close contact grab the {}'s wallet.", target);
34 }
35}
36
37@Slf4j
38public class HitAndRunMethod extends StealingMethod {
39
40 @Override
41 protected String pickTarget() {
42 return "old goblin woman";
43 }
44
45 @Override
46 protected void confuseTarget(String target) {
47 LOGGER.info("Approach the {} from behind.", target);
48 }
49
50 @Override
51 protected void stealTheItem(String target) {
52 LOGGER.info("Grab the handbag and run away fast!");
53 }
54}
Here’s the halfling thief class containing the template method.
1public class HalflingThief {
2
3 private StealingMethod method;
4
5 public HalflingThief(StealingMethod method) {
6 this.method = method;
7 }
8
9 public void steal() {
10 method.steal();
11 }
12
13 public void changeMethod(StealingMethod method) {
14 this.method = method;
15 }
16}
And finally, we show how the halfling thief utilizes the different stealing methods.
1 var thief = new HalflingThief(new HitAndRunMethod());
2 thief.steal();
3 thief.changeMethod(new SubtleMethod());
4 thief.steal();
The Template Method pattern should be used
GenericServlet.init(ServletConfig config)
calls the parameterless method GenericServlet.init()
which is intended to be overridden in subclasses.
Method GenericServlet.init(ServletConfig config)
is the template method in this example.