You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: BehavioralPatterns/TemplateMethod/README.md
+31-9Lines changed: 31 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -11,13 +11,41 @@ Usually, subclasses control how the behavior of a parent class is redefined, and
11
11
12
12
## Solution
13
13
14
+
The Template Method pattern suggests to break down an alogirthm into a series of methods and call them one by one inside a single `template` method.
15
+
14
16
* Define abstract operations (primitives) for the variant parts of a behavior.
15
17
* Define a **template method** that
16
18
* implements the invariant parts of a behavior.
17
19
* calls abstract operations (primitives) that subclasses implement.
18
20
19
21
The **template method** controls how subclasses redefine a behavior. This is also referred to as *inversion of control* because subclasses do no longer control how the behavior of a parent class redefined.
20
22
23
+
## Benefits
24
+
25
+
* Helps to eliminate code duplication
26
+
27
+
## Drawbacks
28
+
29
+
* You are limited with a skeleton of an existing algorithm
30
+
* Template methods tend to be harder to maintain the more steps they have.
31
+
32
+
## Known uses
33
+
34
+
* When subclasses should be able to extend the base algorithm without altering its structure.
35
+
* When you have several classes that do similar things with only minor differences. When you alter one of the classes, you have to change others as well (e.g. HTML/PDF/CSV Generator follows the same data processing steps before generating the specific file format).
36
+
37
+
## Common Structure
38
+
39
+

40
+
41
+
* AbstractClass (CaffeineBeverage)
42
+
* defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm
43
+
* implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
44
+
* ConcreteClass (Tea, Coffee)
45
+
* implements the primitive operations ot carry out subclass-specific steps of the algorithm
@@ -83,14 +111,8 @@ public class Tea : CaffeineBeverage
83
111
}
84
112
```
85
113
86
-
## Common Structure
87
-
88
-

114
+
## Comparison with other patterns
89
115
90
-
* AbstractClass (CaffeineBeverage)
91
-
* defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm
92
-
* implements a template method defining the skeleton of an algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.
93
-
* ConcreteClass (Tea, Coffee)
94
-
* implements the primitive operations ot carry out subclass-specific steps of the algorithm
0 commit comments