This is the 8th day of my participation in the August More Text Challenge. For details, see:August is more challenging
Template method pattern
- Template method pattern: An abstract class is used to define the algorithm template structure, deferring the implementation of certain specific algorithm steps to the subclasses, allowing the subclasses to redefine the specific implementation of some algorithms without changing the algorithm structure. The algorithm is called in the abstract class
- The template method pattern is a behavioral pattern based on inherited code reuse
- Key to the template approach:A child class can replace a mutable part of a parent class, but it cannot change the top-level logic implemented in a template method
- When new subclasses are defined in the template method pattern, they are defined not in terms of the control process, but in terms of the idea of responsibility
- Consider which methods in a subclass must be transposed for the parent class, which are transposable, and which are not. The template approach makes these responsibilities clear
- Application scenarios of template methods:
- Important, complex methods can be used as template methods
- Use a class to implement the immutable logic of an algorithm and defer the mutable logic to subclasses for implementation
- The common behavior extracted from each subclass is grouped into a single parent class to avoid code duplication
- Control the extension of subclasses
Example template method pattern
- The template method pattern contains two roles:
- AbstractTemplate:
- An algorithm template structure that defines abstract operations to be implemented in subclasses
- Abstract operations, also known as basic operations, are the building blocks of top-level logic
- Define and implement a template method
- A template method is a concrete method
- A top-level logical structure is given, and the steps of the logical structure are deferred to the subclasses in the corresponding abstract operations
- Some concrete methods may also be called from the top-level logical structure
- An algorithm template structure that defines abstract operations to be implemented in subclasses
- ConcreteTemplate: ConcreteTemplate:
- Implement abstract methods defined in AbstractTemplate AbstractTemplate. Is a top-level logical component step
- Every AbstractTemplate, AbstractTemplate, can have any number of concrete templates corresponding to it. Each ConcreteTemplate can implement different steps of the top-level logic in the AbstractTemplate AbstractTemplate, making the implementation of top-level logic different
- AbstractTemplate:
- Methods in the template method pattern:Template methods and basic methods
- Template method:
- Template methods are defined in abstract classes that combine multiple basic method operations into a total algorithm or behavior
- An abstract class can contain any number of template methods, and each template method can call any number of concrete methods
- Basic methods:
- AbstractMethod: AbstractMethod is declared by abstract class and implemented by concrete subclasses
- ConcreteMethod: Concrete methods are declared and implemented by abstract classes. Subclasses do not implement or replace them
- HookMethod:Hook methods are declared and implemented by abstract classes that can be extended by subclasses.
- In general, abstract classes will give an empty implementation of the hook method as the default
- The hook method eliminates the need for a subclass to implement all the methods of its parent class, since a concrete subclass usually does not need to implement all the methods of its parent class
- Template method:
- Template mode pattern implementation:
- Analyze the target algorithm to determine if it can be decomposed into multiple steps. From the perspective of each subclass, consider which steps are universal and which steps are different
- Create an abstract class and declare a template method and a series of abstract methods that represent the steps of the algorithm. In the template method, the algorithm steps are called in turn according to the structure of the algorithm. The final modifier is used to modify the template method to prevent subclasses from overwriting the template method
- By putting methods shared by a subclass into a superclass, the subclass simply overrides the mutable algorithm logic
- Consider adding hook methods to the superclass, which default to an empty implementation, for subclasses to extend the algorithm logic
- Each algorithm subclass needs to create a concrete subclass, which must implement all the abstract steps and can override some optional extension steps
- Example Template Method pattern
Template method pattern summary
Note the template mode
- Typically, template methods use the final keyword
Template method pattern advantages
- Realize reverse control: the parent class calls the operation of the subclass, through the specific implementation of the subclass to expand into different behaviors, realize reverse control, in line with the open closed principle
- Improve code reusability: Put common code in abstract parent classes and different code implementations in subclasses
- Improved system extensibility: different code is put into different subclasses, and new behaviors are added by extending the subclasses
- Behavior is controlled by the parent class and implemented by the child class
Template method pattern shortcomings
- Increased system complexity: Introduction of abstract classes that require a subclass for each different implementation, resulting in an increase in the number of classes in the system
- Changing the default step implementation of the parent class by a subclass may violate the principle of Richter substitution
The difference between template method pattern and factory pattern
Template method pattern
- The template method pattern is based on the inheritance mechanism and allows the algorithm related step implementation to be extended and modified in subclasses
- The template method pattern is static and works at the class level
Factory method pattern
- The factory method pattern is a special template method pattern
- A factory method can be used as a step in a larger template method pattern
The strategy pattern
- The policy pattern is based on the combination mechanism, which can change part of the behavior of an object by providing different policies for the corresponding behavior
- The policy mode is dynamic and can be switched at run time. The policy pattern works at the object level