Skip to content

Commit 67f38ca

Browse files
committed
Merge pull request coffeescript-cookbook#93 from randusr836/master
Added the Template Method Pattern.
2 parents 5b2ae1d + 198eed3 commit 67f38ca

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

authors.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The following people are totally rad and awesome because they have contributed r
2121
* Frederic Hemberger
2222
* Mike Hatfield *[email protected]*
2323
* [Anton Rissanen](http://github.com/antris) *[email protected]*
24+
* Calum Robertson *http://github.com/randusr836*
2425
* ...You! What are you waiting for? Check out the [contributing](/contributing) section and get cracking!
2526

2627
# Developers
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
layout: recipe
3+
title: Template Method Pattern
4+
chapter: Design Patterns
5+
---
6+
## Problem
7+
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+
10+
## Solution
11+
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+
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+
16+
{% highlight coffeescript %}
17+
class Document
18+
produceDocument: ->
19+
@produceHeader()
20+
@produceBody()
21+
22+
produceHeader: ->
23+
produceBody: ->
24+
25+
class DocWithHeader extends Document
26+
produceHeader: ->
27+
console.log "Producing header for DocWithHeader"
28+
29+
produceBody: ->
30+
console.log "Producing body for DocWithHeader"
31+
32+
class DocWithoutHeader extends Document
33+
produceBody: ->
34+
console.log "Producing body for DocWithoutHeader"
35+
36+
docs = [new DocWithHeader, new DocWithoutHeader]
37+
doc.produceDocument() for doc in docs
38+
{% endhighlight %}
39+
40+
## Discussion
41+
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)