Skip to content

Commit 198eed3

Browse files
committed
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.
1 parent 0776427 commit 198eed3

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

chapters/design_patterns/template_method.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ chapter: Design Patterns
55
---
66
## Problem
77

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.
99

1010
## Solution
1111

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.
1313

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.
1515

1616
{% highlight coffeescript %}
1717
class Document
@@ -33,13 +33,12 @@ class DocWithoutHeader extends Document
3333
produceBody: ->
3434
console.log "Producing body for DocWithoutHeader"
3535

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
4138
{% endhighlight %}
4239

4340
## Discussion
4441

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

Comments
 (0)