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
Changes after feedback. Wrote a little about polymorphism being used in the pattern. Described in greater detail the concepts of the algorithm structure and the specification of algorithm step behaviour.
Copy file name to clipboardExpand all lines: chapters/design_patterns/template_method.md
+8-9Lines changed: 8 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,13 +5,13 @@ chapter: Design Patterns
5
5
---
6
6
## Problem
7
7
8
-
You need to execute a series of steps according to some algorithm or recipe and wish to provide the implementation for any of the steps.
8
+
Define the structure of an algorithm as a series of high-level steps, making it possible to specify the behaviour of each step, giving rise to a family of algorithms that have the same structure but different behaviours.
9
9
10
10
## Solution
11
11
12
-
Use the Template Method to describe each step in a superclass, delegating the implementation of each step to a subclass.
12
+
Use the Template Method to describe the algorithm structure in a superclass, delegating the implementation of some steps to one or more concrete subclasses.
13
13
14
-
For example, imagine you wish to model various types of document and each one may contain a header and a body.
14
+
For example, imagine you wish to model the production of various types of document and each one may contain a header and a body.
15
15
16
16
{% highlight coffeescript %}
17
17
class Document
@@ -33,13 +33,12 @@ class DocWithoutHeader extends Document
33
33
produceBody: ->
34
34
console.log "Producing body for DocWithoutHeader"
35
35
36
-
doc1 = new DocWithHeader
37
-
doc1.produceDocument()
38
-
39
-
doc2 = new DocWithoutHeader
40
-
doc2.produceDocument()
36
+
docs = [new DocWithHeader, new DocWithoutHeader]
37
+
doc.produceDocument() for doc in docs
41
38
{% endhighlight %}
42
39
43
40
## Discussion
44
41
45
-
In this example, there are two steps, one for producing a document header and the second for producing the document body; these two steps are given empty implementations in the superclass. The DocWithHeader implements both the body and header steps, whereas the DocWithoutHeader only impements the body step.
42
+
In this example, the algorithm consists of two steps describing document production: one for producing a document header and the second for producing the document body. An empty method implementation for each step is present in the superclass and polymorphism is exploited such that each concrete subclass can provide a different implementation for a step by overriding a step method. In this example,the DocWithHeader implements both the body and header steps, whereas the DocWithoutHeader only implements the body step.
43
+
44
+
The production of different types of document is then straightforward when document objects are stored in an array, and it is then a simple of matter of iterating over each document object and calling its produceDocument method.
0 commit comments