Introduction and implementation:
- 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:
- There is an interface (abstract class) in a that has an abstract algorithm method a
- 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
- 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
- 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
- and the template method pattern
- 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)
- 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)
- 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:
- The purpose is the same, so that different algorithms implement the business Reuse code
Difference:
- 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)
- "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:
- 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:
- The specific method here is not a template method, but another tool method
- There is also a business Class B and Business method B, this b is the meaning of the template method
- 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:
- Read a lot, found a lot of biased or a group of paste understanding, even the work of the programmer, so I specifically disputed
- Most people tend to be about the same, too lazy to spit out the groove
- 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
- "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
- 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
- Personal experience
- 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
- 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