I have been going back and forth between C# and Java for the last 8 years.
One thing that strikes me is that I have completely stopped using the ‘Template Method’ design pattern in C#. Actually, in C# I Have come to think of this pattern as an anti-pattern.
http://en.wikipedia.org/wiki/Template_method_pattern
Coming back to Java, I find the pattern is alive and kicking. I still think it looks antique, but realise that there’s no other way to do this in java. Java looks antique too 😉
Since this is going to come up anyway, Why is it an antipattern ?
- A lot of times it uses up your inheritance hierarchy for ‘the wrong reasons’.
- Base classes have a tendency to become littered with all sorts of unrelated code.
- It forces you to lock down design, often quite early in the development process. (Premature lock down in a lot of cases)
- Changing this at a later stage becomes just harder and harder.
So with closures/delegates/function pointers, you normally pass around some function instead of subclassing.
So back to the question:
If your language has closures/delegates/function, do you use the Template Method, and when?
When I used Java, yes. But for languages with ‘closures/delegates/function’, Lua in my case, no I don’t anymore, instead I’ve been leaning more and more towards decoration pattern for most of my needs.