Cited
GOF "design mode: Reusable object-oriented software design in the basis of the model, should be regarded as the originator of the pattern induction (not their earliest discovery/use of the model, but the existing patterns are integrated induction), and we are now on the mode of learning is basically based on GOF design patterns. Today, we are talking about one of the behavioral patterns: the Strategy model.
Pattern definition
Intent: Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other. This mode allows the algorithm to be independent of the customers who use it.
Motivation:
- The client program will become complex if it contains the policy algorithm code directly. (The strategy algorithm itself is complex enough)
- Different times require different algorithms (If....else ...). )
Pattern class Diagram
Elaboration and discussion
Strategy mode is one of the patterns we use in our development. Online about the strategy model also has a lot of introduction, here again, is expected to give a "normal" mode, in my beginner design mode, also often be made unintelligible, the opinions of the different to the code, but almost not the same, I was also confused by the strategy mode (mainly the context), So again find out that Gof was originally analyzed--
The most common question of doubt: the strategy model is compared with the simple factory model. The difference between the two is still relatively large, whether from the semantic or the implementation.
The following shows the class diagram for a simple factory (there is no simple factory in GOF mode---only abstract factory and factory methods):
Comparing simple factory and policy pattern class diagrams It is easy to see the difference: The Factory (Factory) is dependent on the product interface, while relying on the specific products (producta and PRODUCTB), whereas in the policy mode, the context is the Stategy interface that contains the It does not depend on the specific policy class at the same time.
It's easy to see the difference by code:
Policy Mode code:
1
1 PackageStrategydemo;2 /**3 * @author: Xiaolu February 6, 20184 * @todo: Policy context5 */6 Public classContext {7 //include relationship8 Privatestrategy strategy;9 //This class contains additional parameters that are required by the specific policy and can be passed to the policy class if necessary (GOF)Ten PublicContext (Strategy strategy) { One This. Strategy =strategy; A } - - Public intget () { the return This. Strategy.algorithm (); - } -}
1 PackageStrategydemo;2 3 /**4 * @author: Xiaolu February 6, 20185 * @todo: Policy interface6 */7 Public classStrategybImplementsStrategy {8 @Override9 Public intalgorithm () {Ten //the logic here should be more complex, otherwise it is not necessary to use the policy class One //for illustration only A return0; - } - the}
1 PackageStrategydemo;2 3 Public classStrategyaImplementsStrategy {4 @Override5 Public intalgorithm () {6 //the logic here should be more complex, otherwise it is not necessary to use the policy class7 //for illustration only8 return1;9 }Ten One}
1 PackageStrategydemo;2 3 /**4 * @author: Xiaolu February 6, 20185 * @todo: Policy interface6 */7 Public classStrategybImplementsStrategy {8 @Override9 Public intalgorithm () {Ten //the logic here should be more complex, otherwise it is not necessary to use the policy class One //for illustration only A return0; - } - the}
1 PackageStrategydemo;2 /**3 * @author: Xiaolu February 6, 20184 * @todo: Client5 */6 Public classMain {7 Public Static voidMain (string[] args) {8 //Specific policy Classes9Strategy A =NewStrategya ();Ten //Context OneContext CTX =NewContext (a); A - System.out.println (Ctx.get ()); - } the}
The code structure and name are generally given in the policy class diagram. You can see that the client is "aware" (or dependent on) the specific policy class. The context does not have any dependency (ignorance) on the specific policy class, but only "contains" (attributes in the Class) the policy interface.
A simple factory implementation is given below:
1 package simplefactory; 2 3 /** 4 * @ Author 5 * @todo: Product interface 6 */ 7 public interface Product { 8 ... Factory mode, the factory is mainly used to produce products, does not involve the operation of products, so here omit the operation of the specific product 9 }
1 package simplefactory; 2 3 /** 4 * @ Author 5 * @todo: Product a 6 */
7 public class ProductA implements Product { 8 9 }
Package simplefactory; /** @author : Xiaolu February 6, 2018 * @todo ** publicclass Implements Product {}
1 Packagesimplefactory;2 3 /**4 * @author: Xiaolu February 6, 20185 * @todo: Simple Factory6 */7 Public classFactory {8 //The return value is the product interface9 PublicProduct Get (inti) {Ten if(i = = 0) { One //return to a specific product (dependent) A return Newproducta (); -}Else if(i = = 1) { - //return to a specific product (dependent) the return NewPRODUCTB (); -}Else { - Throw NewRuntimeException ("Not supported"); - } + } -}
1 Packagesimplefactory;2 3 /**4 * @author: Xiaolu February 6, 20185 * @todo: Client6 */7 Public classMain {8 Public Static voidMain (string[] args) {9Factory f =NewFactory ();Ten //The client does not know (does not depend on) the specific product OneProduct p = f.get (1); A //perform the operation of P - } -}
Comparing class diagrams and code implementations should be structurally easy to distinguish between the two. More importantly: The strategy mode is the behavioral mode, and the Factory mode is the creation mode! In layman's terms, the factory model is only responsible for producing "as you Want" products, as for how you use the factory is ignorant (which is why I did not define the method in the product interface, because it is not fixed). In the policy model, the specific policy class is the client "knows" (which is also a choice to use the policy mode directly "can also circumvent it-strategy + factory"), while the context is responsible for "forwarding" (just forwarding it?). Think about why you need the context, what if there is no context? If not:
1 New Strategya ();
2 strategy.algorithm ();
Seems like it's a little easier? The context is an indirect layer between strategy and main (client) that can be used to encapsulate the data structures and parameters that need to be passed to strategy.
Compared with the common polymorphic structure, the strategy model emphasizes the alternative of "algorithm". Again, if the specific policy class is not complex, do not use strategy mode (asking for trouble). A disadvantage of the Strategy class: The client class needs to "know" the specific policy class------can be solved by adding a factory. What is the meaning of a simple factory? In line with SRP, single function, reduced coupling, there is also a role---------conversion. Most of the If....else code we can not circumvent, just moved from "here" to "there", the benefits are more clear code, clearer, easier to expand, and simple factory to make this conversion is very appropriate. - -
Policy Mode (Java)