Select Page

Template method

Template method

Defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses refine certain steps of an algorithm without changing the algorithm’s structure.

Source Code

* This class defines abstract primitive operations that concrete
* subclasses define to implement steps of an algorithm. Implements a template
* method defining the skeleton of an algorithm.
* @role __TemplateContext */ public abstract class Context { /**
* Primitive operation.
*/ public abstract void doPrimitiveOperation(); /**
* Defines the skeleton of an algorithm. Calls primitive operations as well
* as operations defined in AbstractClass or those in other objects.
*/ public void templateMethod() { // put your code here doPrimitiveOperation(); // put your code here } } /**
* Implements the primitive operations to carry out subclass-specific steps of
* the algorithm.
*/ public class ConcreteClass extends Context { /**
@see patterns.gof.templatemethod.Context#doPrimitiveOperation()
*/ public void doPrimitiveOperation() { // provide implementation here } }


0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments


Would love your thoughts, please comment.x