Template Method Design Pattern
Template Method Design Pattern
The above example deals with order processing flow. The OrderProcessTemplate class
is an abstract class containing the algorithm skeleton. As shown on note,
processOrder() is the method that contains the process steps. We have two
subclasses NetOrder and StoreOrder which has the same order processing steps.
So the overall algorithm used to process an order is defined in the base class and
used by the subclasses. But the way individual operations are performed vary
depending on the subclass.
When to use template method
The template method is used in frameworks, where each implements the invariant
parts of a domain's architecture, leaving "placeholders" for customization options.
The template method is used for the following reasons :
Avoid duplication in the code , the general workflow structure is implemented once
in the abstract class's algorithm, and necessary variations are implemented in the
subclasses.