Similarities and differences between Template Method modes and Policy Modes

Source: Internet
Author: User

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.

Related Article

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.