Image example:
Have you read the classic article "how to persuade girls to go to bed? From the process of realizing that a girl goes to bed, there are eight steps (Template Method): encounter, break the deadlock, pursue, kiss, foreplay, start-up, caress, and enter ), however, each step has a different approach for different situations. It depends on your response (specific implementation );
Template Method mode:
The template method mode prepares an abstract class, implements some logic in the form of a specific method and a specific constructor, and then declares some abstract methods to force the subclass to implement the remaining logic. Different sub-classes can implement these abstract methods in different ways to implement the rest logic differently. First, create a top-level logic framework, and leave the logic details to specific sub-classes for implementation.
Purpose:
Defines the skeleton of an algorithm in an operation, and delays some steps to the subclass. Templatemethod allows the subclass to redefine certain steps of an algorithm without changing the structure of an algorithm.
UML structure diagram:
Abstract base class:
1) abstractclass: abstract base class, defining the outline of an algorithm
Resolution:
The key to templatemethod is to define an algorithm profile in the base class, but the specific implementation of each step of the algorithm is left to the derived class. however, this will also cause the design to be less flexible, because it is difficult to change the outline, which is also the reason for the priority of aggregation rather than inheritance.
Design Pattern _ Template Method