The difference between template method patterns and policy patterns

Source: Internet
Author: User

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







Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.