Template method pattern, linkage, difference and application scenario of policy mode

Source: Internet
Author: User

Introduction and implementation:
    1. The structure of the strategy mode is very simple, more simple than the template method mode, it is essentially a principle of embodiment, to the low-said is the principle of the "in-the-way", the high-said is the dependency inversion principle, the concrete implementation process is this:
      1. There is an interface (abstract class) in a that has an abstract algorithm method a
      2. There is a set of interfaces (abstract class) for an implementation class (subclass) A-? The method of abstract algorithm is implemented by different concrete algorithm a
      3. Then there is a business Class B with a business method B and an object member of type a C, where method B calls C's algorithm method A to do some work
      4. Finally, when using, instantiate the business Class B when you pass in the subclass a of the different A-? To instantiate an object of business Class B using different algorithms
    2. and the template method pattern
      1. There is an abstract class A in two methods (temporarily ignoring the hook method), the abstract algorithm method A-abstruct, template business method A-mode (template method used in the abstract algorithm method, so in fact, this template method does not work properly now)
      2. And then write a group of subclass a-? Inherits the abstract class A, implements the abstract algorithm method with the different algorithm in the subclass a (the template method can work correctly)
      3. Finally, when used, the different subclasses a-? are instantiated directly. To instantiate an object using different algorithms, invoking the object's template method allows the business
Common:
    1. The purpose is the same, so that different algorithms implement the business Reuse code
Difference:
    1. The form appears to be "policy mode" to separate the template method in the template method pattern into another class, and then, when used, to stitch together the implemented algorithm subclasses. (When using the stitching, users can know what mode they are using)
    2. "Template method Mode" in use, without splicing, because at the time of inheritance is already separate inheritance, two methods are originally together, you want to use which group directly call which group on the line. (Call the full method directly when using, the user does not know what mode is implemented)
A confusion:
    1. Sometimes in policy mode, implement Class A-? In the use of different specific algorithms to implement the abstract algorithm method A may have a lot of duplicate code, this time in order to reuse, the duplicate code stripping out, use the time to call, then peel to the interface A, then this time interface a must be abstract class A, at this time you will find that, There are also two methods in the abstract class A of the policy pattern, an abstract algorithm method and another concrete method, which is not a bit like the template method pattern, but it is completely different:
      1. The specific method here is not a template method, but another tool method
      2. There is also a business Class B and Business method B, this b is the meaning of the template method
    2. On the surface of the strategy model is complex, but in fact, the strategy mode is the most simple interface-oriented programming example, the most simple polymorphic one embodiment, from this point of view, is actually very simple
Advantages and disadvantages or two ways to adapt to the scene:
    1. Read a lot, found a lot of biased or a group of paste understanding, even the work of the programmer, so I specifically disputed
      1. Most people tend to be about the same, too lazy to spit out the groove
      2. There are even the two models of the definition are not clear, use not rigidly adhere to the model to stall, and then finally said that the innovation of a pattern in between the two, in fact, is one of these, more is actually using the abstract class instead of the interface strategy mode
      3. "They are like the test paper, template mode is the blank question, the strategy mode is the choice problem", confused the development stage, in the creation phase, both are the blank question, in the use stage, both are the choice question
      4. Then some people think the biggest difference between the two is the outside of the method to modify the access to the difference, this can not say no, but this point is worthless
    2. Personal experience
      1. The template method is suitable for small projects, because it is the simplest form and convenient to use, but it is not flexible for the reuse of the algorithm, and a large number of inheritance will produce a lot of redundant code, also aggravate the system burden
      2. The strategy model is suitable for large projects, although the form is complex, but the management specification, the reuse of the algorithm can be very flexible (especially when using abstract classes instead of interfaces), and even can be nested with multiple algorithms to further reduce redundant code, and by the combination of classes, redundant code can also reduce a lot of

Template method Patterns, contact, differences, and scenarios for policy mode

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.