The template mode uses the inheritance method to realize the heterogeneous algorithm. The key point is to encapsulate the algorithm in the abstract base class and put the implementation details of different algorithms in the sub-class. The template mode complies with the object-oriented system analysis and design principles, and relies on the inverted principle. The parent class calls subclass operations, and the underlying module implements interfaces declared by high-level modules. In this way, the control is in the parent class, and the underlying module depends on the high-level module.
The inherited mandatory constraint relationships also make the template mode inadequate. We can see that for the concreteclass
The primitive method primitive1 () implemented in the class cannot be reused by other classes. Template
The problems exposed by models are inherent in inheritance.
The policy mode achieves algorithm heterogeneity through combination, while the template mode adopts the inheritance mode.
To implement an abstract interface, inheritance is a way: we declare the abstract interface in the base class and place the specific implementation in the specific subclass. Combination (Delegation) is another method: We put the implementation of interfaces in the composite object and abstract interfaces in the composite class. Both methods have their own advantages and disadvantages.