Skip to content

Commit d469b26

Browse files
committed
update TemplateMethod
1 parent e7f4931 commit d469b26

File tree

1 file changed

+31
-9
lines changed

1 file changed

+31
-9
lines changed

BehavioralPatterns/TemplateMethod/README.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,41 @@ Usually, subclasses control how the behavior of a parent class is redefined, and
1111

1212
## Solution
1313

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+
1416
* Define abstract operations (primitives) for the variant parts of a behavior.
1517
* Define a **template method** that
1618
* implements the invariant parts of a behavior.
1719
* calls abstract operations (primitives) that subclasses implement.
1820

1921
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.
2022

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+
![Common structure of template method pattern](https://upload.wikimedia.org/wikipedia/commons/2/2a/W3sDesign_Template_Method_Design_Pattern_UML.jpg)
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
46+
47+
_[Source: http://www.dofactory.com/net/template-method-design-pattern]_
48+
2149
**Definition**
2250

2351
![Template Method Example](/Diagrams/TemplateMethod.png)
@@ -83,14 +111,8 @@ public class Tea : CaffeineBeverage
83111
}
84112
```
85113

86-
## Common Structure
87-
88-
![Common structure of template method pattern](https://upload.wikimedia.org/wikipedia/commons/2/2a/W3sDesign_Template_Method_Design_Pattern_UML.jpg)
114+
## Comparison with other patterns
89115

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
116+
* **Factory Method**
95117

96-
_[Source: http://www.dofactory.com/net/template-method-design-pattern]_
118+
* **Strategy**

0 commit comments

Comments
 (0)