This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
1) An interface, say I 2) A class, say A, that implements the interface I 3) Another class, say B, that implements I and extends A 4) Another class, say C, that implements I and extends A
Now, A has a couple of abstract methods for separate functional implementations in B and C. B and C also share some of the common methods of A. These commmon methods in A can call the abstract methods which will be called dynamically based on whether B or C, as a concrete implementation of I, is in operation.
This seems to me an efficient way to delegate reponsibilities at runtime.
Patterns are solutions to common problems, and this isn't what you've described. Instead, you've captured a common design strategy for generalization structures. It's a good strategy, but it's not a pattern.
Originally posted by steve souza: Not sure I understand your posting, but it sounds like the "Template Method" design pattern.
Yes, that was my thought, too...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Can we say "Abstract Class" instead of "Template Method Pattern" ?
Joined: Dec 02, 2003
One more comment: if B,C extends A then they do automatically ands always implement I.
Joined: Jun 26, 2002
What you described in your original post is more than an abstract class. The way your various classes interact in their inheritance structure makes it the 'template method'. One of the biggest benefits of using design patterns is that they enable you to communicate a whole design with one phrase. By calling it an abstract class you would be forced to explain in words your design. That is not true if you use the proper name: Template method [ September 02, 2005: Message edited by: steve souza ]
Joined: Jul 11, 2001
Originally posted by Vladas Razas: Can we say "Abstract Class" instead of "Template Method Pattern" ?
No. Template Method is a pattern that makes use of an abstract class to define a high level algorithm, and delegate implementation of low level details to subclasses by calling abstract methods.
With other words, not every use of an abstract class is an implementation of the Template Method pattern.