Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you use Template Method Pattern in programming languages with closures/delegates/function pointers?

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?

like image 728
krosenvold Avatar asked Nov 21 '08 11:11

krosenvold


1 Answers

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.

like image 172
Robert Gould Avatar answered Nov 15 '22 07:11

Robert Gould