Definition
Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure..
UML Class Diagram
Template Method: when and where use it
- The intent of the Template Method design pattern is to provide an outline of a series of steps for an algorithm. Derived classes retain the original structure of the algorithm but have the option to redefine or adjust certain steps of the algorithm. This pattern is designed to offer extensibility to the client programmer. Template Methods are frequently used when building a class library (for example an application framework) that is going to be used by other client programmers.
- There are strong similarities between the Template Method and the Strategy pattern. Both are designed for extensibility and customization as they allow the client to alter the way an algorithm or process is executed. The difference is that with Strategy the entire algorithm is changed, whereas the Template method allows individual steps to be redefined. However, their object-oriented implementations are quite different: Strategy uses delegation and Template Method is based on object inheritance.
Sample
- Template class
- Concrete class
Ref: Gang of Four tutorial
No comments:
Post a Comment