This article is from: Cao shenghuan blog column. Reprinted please indicate the source:Http://blog.csdn.net/csh624366188
Strategy is an object behavior pattern in design patterns. It mainly defines a series of algorithms and encapsulates these algorithms into separate classes one by one.
Definition:Policy patterns define a series of algorithms, encapsulate each algorithm, and make them replaceable. The rule mode allows algorithms to change independently of customers who use it. (Original article: The Strategy Pattern Defines a family ofalgorithms, encapsulates each one, and makes them interchangeable. Strategy
Letsthe algorithm vary independently from clients that use it .)
Role:
Strategy:Policy Interface, used to constrain a series of specific policy algorithms. Context uses this interface to call the algorithm defined by a specific policy.
Concretestrategy:Specific policy implementation, that is, specific algorithm implementation.
Context:The context is responsible for interacting with specific policy classes. Generally, the context holds a real policy implementation. The context also allows specific policy classes to obtain context data, it even allows specific strategies to adjust the context back and forth.
The structure 1 of the Rule mode is shown in:
Application scenarios:
1. Multiple classes only differ in performance behavior. You can use the Strategy Mode to dynamically select the behavior to be executed during running.
2. Different policies (algorithms) need to be used in different situations, or they may be implemented in other ways in the future.
3. Hiding implementation details of specific policies (algorithms) from customers is completely independent of each other.
Advantages:
1. unit testing is simplified because each algorithm has its own class and can be tested independently through its own interface.
2. Avoid using multiple conditional transfer statements in the program to make the system more flexible and easy to expand.
3. comply with most grasp principles and common design principles, with high cohesion and low coupling.
4. It provides an alternative to inheritance, and maintains the advantages of inheritance (code reuse) and is more flexible than inheritance (algorithms are independent and can be expanded at will ).
Disadvantages:
1. Because each specific policy class generates a new class, the number of classes to be maintained by the system is increased.
2. In the basic policy mode, the responsibility for selecting the specific implementation is borne by the client object and transferred to the context object in the Policy mode. (This does not relieve the pressure on the client to select judgment. After the policy mode is combined with the simple factory mode, the responsibility for selecting specific implementations can also be borne by context, this minimizes the pressure on the client .)
A classic example of a policy model I have seen: quote Management
It is very complicated to quote different prices for different customers. Therefore, in some CRM (Customer Relationship Management) systems, there will be a separate quotation management module, to process complex quote functions.
To simplify the demo, we assume that a simplified quote management is required to implement the following functions:
(1) report full price to common customers or new customers
(2) a 5% discount for the prices quoted by old customers
(3) A unified discount of 10% for prices quoted by major customers
(1) Check the policy interface first. The sample code is as follows:
/*** Policy, define the quote calculation algorithm interface */public interface Strategy {/*** calculate the quoted price * @ Param goodsprice original price * @ return calculated, price to be quoted */Public double calcprice (double goodsprice );}
(2) Next, let's look at the specific Algorithm Implementation. Different algorithms have different implementations. First, let's look at the implementation of the Price Report calculated for new customers or common customers. The sample code is as follows:
/*** Specific Algorithm Implementation: Calculate the expected price for new or common customers */public class normalcustomerstrategy implements Strategy {public double calcprice (double goodsprice) {system. out. println ("No discounts for new or common customers"); Return goodsprice ;}}
Let's take a look at the implementation of calculating the quoted price for old customers. The sample code is as follows:
/*** Specific Algorithm Implementation: Calculate the expected price for the old customer */public class oldcustomerstrategy implements Strategy {public double calcprice (double goodsprice) {system. out. println ("5% discount for old customers"); Return goodsprice * (1-0.05 );}}
Let's take a look at the implementation of the Price Report for major customers. The sample code is as follows:
/*** Specific Algorithm Implementation: Calculate the expected price for major customers */public class largecustomerstrategy implements Strategy {public double calcprice (double goodsprice) {system. out. println ("10% discount for major accounts"); Return goodsprice * (1-0.1 );}}
(3) let's take a look at the implementation of the context, that is, the original price class. The changes are large, mainly including:
- The original private methods used for different computations have been removed and independently made into algorithm classes.
- In the original quotation method, the judgment on the specific calculation method is removed, allowing the client to complete the function of selecting a specific algorithm.
- Add a new algorithm implementation and pass it through the constructor.
- The implementation of the original quote method has changed to a specific algorithm for tuning.
The sample code is as follows:
/*** Price management, mainly used to calculate the price quoted to the customer */public class price {/*** holding a specific policy object */private strategy Strategy = NULL; /*** constructor: input a specific policy object * @ Param astrategy specific policy object */Public price (Strategy astrategy) {This. strategy = astrategy;}/*** quotation, calculated for the customer's quote * @ Param goodsprice product sales price * @ return calculated, price to be quoted */Public double quote (double goodsprice) {return this. strategy. calcprice (goodsprice );}}
(4) Write a client to test and run it. The sample code is as follows:
Public class client {public static void main (string [] ARGs) {// 1: select and create the policy object strategy Strategy = new largecustomerstrategy (); // 2: create context price CTX = new price (Strategy); // 3: Calculate the quote double quote = CTX. quote (1, 1000); system. out. println ("quoted to the customer:" + quote );}}