Strategy and template method

Source: Internet
Author: User

Strategy and template method

Strategy encapsulates differentAlgorithmThese algorithms can be replaced with each other to achieve different purposes.
Template Method defines the skeleton of an algorithm in an operation and delays the execution of some steps to its subclass.

Strategy is implemented through object combination.
The template method is implemented through Object Inheritance.
According to the principle of "Combination prevails over inheritance", in most cases, we choose strategy. But it's not the beginning
You know you want to use strategy.
Sometimes it is through micro-refactoring (the combination method is often used) to obtain the template method. when the problem is complicated, the subclass needs to be rewritten.
When many methods of the parent class are inherited from the method that is not required by the subclass, the template method should be
Refactored into strategy. It is like the flying duck mentioned in Header first deign patterns.
Strategy solves the coupling problem caused by inheritance.

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.