Original: Http://www.tuicool.com/articles/6JBN7z3
If you are not yet aware of template method patterns and policy patterns, read the policy mode (strategy) and template method patterns first.
The main idea of the template method pattern is to define an algorithm flow, implement the specific steps, and defer to subclasses. This allows specific steps in a "custom" process to be implemented through different subclasses without changing the algorithm flow. The main idea of the policy pattern is that different algorithms can be replaced by each other without affecting the client's use.
In view of thought and intention, the template method emphasizes more:
1 Define a line (algorithm flow), multiple points on the line can be changed (specific implementation in subclasses), multiple points on the line must be executed, and must be carried out according to a specific process.
2 The algorithm flow has only a unique entry, for the point of access is limited "usually with a protected virtual function to define the variable point."
The policy model focuses more on: a "policy" is a whole (complete) algorithm, which can be replaced by the whole. The template method can only be replaced by a specific point in which the algorithm flow is fixed.
In such a detail, the template method and a set of policy patterns cannot be equated.
In front of this board method, it seems that the strategy pattern of "prioritizing the use of object combinations rather than inheritance" is very high. Personally, I still feel that there are advantages and disadvantages, but they adapt to different scenes. When encountering "Multiple change point independent change", at this time need the strategy mode to rescue, if otherwise, the original architecture can be well maintained and extended, it is necessary to go to the trouble, to find the temporary most perfect answer. No, it seems.
With reference to the design pattern of fine solution
https://blog.csdn.net/hudashi/article/details/50978969