Skip to content

Conversation

@randusr836
Copy link
Contributor

I've added the Template Method Pattern to the Design Patterns chapter.

@sukima
Copy link
Contributor

sukima commented Sep 20, 2013

This is a perfect opportunity to talk about polymorphism in CoffeeScript.

@sukima
Copy link
Contributor

sukima commented Sep 20, 2013

I haven't ever heard of this called the "template pattern" before. I always figured this was a standard OOP concept. Maybe that name is confusing for me.

I think this is ok to be included. Maybe If you wanted to expand on the discussion about it being an example of polymorphism that would be cool too.

:shipit:

@peterhellberg
Copy link
Member

Yes :shipit:

@randusr836
Copy link
Contributor Author

sukima: Thanks for the comments. I suppose it is a pattern that exploits polymorphism to achieve its aim. It is documented here: http://en.wikipedia.org/wiki/Template_method_pattern and there is also a good example in the Head First Design Patterns book (in Java). I think the discussion regarding polymorphism belongs in perhaps a new section and this could consist of the ubiquitous Shape class hierarchy example with a draw method; I would be willing to add this. Then to demonstrate polymorphism more clearly in my code, I could put the two objects in an array, iterate over it calling the produceDocument method.

@dbrady
Copy link
Member

dbrady commented Sep 20, 2013

I think this is patternworthy. I generally prefer "favor composition over inheritance" but the template method pattern is the one exception to the rule--when you have shared high-level behavior for a parent class that child classes can implement the specifics of differently.

My only nitpick is that I think there's a typo in the first sentence: "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." <-- I believe the "and wish" should be "but do not wish", or similar verbiage to that effect, correct? I would even be tempted to reword it to reflect the goal of the pattern explicitly, e.g. that you have multiple classes that should all execute the same algorithm but in their own way.

…in the pattern. Described in greater detail the concepts of the algorithm structure and the specification of algorithm step behaviour.
@randusr836
Copy link
Contributor Author

Thanks for the comments. My last commit has included a bit about polymorphism and I've rewritten some parts of the text in response to dbrady's comments. I've also tidied up the usage example by putting the objects in an array and iterating over it when calling the algorithm method.

sukima added a commit that referenced this pull request Sep 26, 2013
Added the Template Method Pattern.
@sukima sukima merged commit 67f38ca into coffeescript-cookbook:master Sep 26, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants