Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Template Method design pattern and Inheritance

 
Teja Saab
Rancher
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How is the Template Method design pattern different from plain old class inheritance? I know that the base case in this case will have some placeholders that are implemented in various ways by different sub classes.

Isn't this plain old inheritance in its purest form? To me, it looks like a very basic OO concept has been rehashed as a design pattern. Am I missing something here?

 
JigaR Parekh
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simplest form of Template method is almost same like plain class inheritance, but

one of applicable scenario for template method is

To control subclasses extensions. You can define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points.


So by using template pattern you can control what can be changed and what can not be changed. of course you will need inheritance characteristics of OOP to implement it.

 
Teja Saab
Rancher
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JigaR Parekh wrote:Simplest form of Template method is almost same like plain class inheritance, but

one of applicable scenario for template method is

To control subclasses extensions. You can define a template method that calls "hook" operations at specific points, thereby permitting extensions only at those points.


So by using template pattern you can control what can be changed and what can not be changed. of course you will need inheritance characteristics of OOP to implement it.



Thanks JigaR. However, the only way you can allow extensions at specific points is by using a method call at the specific point and then having the sub class over ride that called method. This is still just overriding a method in the sub class. I still don't see how this is different from plain old OO inheritance.

Here is what I mean.



Apart from using a method, I can't think of a way to allow a sub class to plug custom functionality at specific points.
Maybe I need to read the pattern again.

Thanks for your help.

 
Ranganathan Kaliyur Mannar
Bartender
Posts: 1103
10
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In template method, the "myMethod()" in the super class will be final.

The concept is that, the template can't be changed (i.e call order),
whereas parts of the template can be...
 
Teja Saab
Rancher
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranganathan Kaliyur Mannar wrote:In template method, the "myMethod()" in the super class will be final.

The concept is that, the template can't be changed (i.e call order),
whereas parts of the template can be...


Thank you for the clarification.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic