ArticleDirectory
- 2.1 Policy mode to solve
- 2.2 schema structure and description
- 2.3 sample code of policy Mode
- 2.4 example of policy mode Rewriting
2 solution 2.1 Solution
A reasonable solution for solving the above problems is the policy mode. So what is a policy model?
(1) Policy mode definition
Define a seriesAlgorithm, Encapsulate them one by one, and make them replace each other. This mode allows algorithms to change independently of customers who use it.
(2) Application policy mode to solve the problem
after carefully analyzing the problem above, let's abstract it first, the Calculation Method of various quote calculation methods is like a specific algorithm, and the Program that uses these calculation methods to calculate the quotation, this is equivalent to the customers who use algorithms.
the root cause of the above implementation is that the algorithms are coupled with the customers who use the algorithms. In the above implementation, the specific algorithm and the customer using the algorithm are different methods in the same class.
to solve these problems, we should first separate all calculation methods according to the policy mode. Each calculation method should be made into a separate algorithm class, thus, a series of algorithms are formed and a public interface is defined for these algorithms. These algorithms implement different implementations of the same interface, and their status is equal and can be replaced with each other. In this way, to expand a new algorithm, it becomes a new algorithm implementation class. To maintain an algorithm, you only need to modify a specific algorithm implementation, it does not affect other codes . That is to say, this solves the maintenance and scalability problems.
in order to enable the algorithm to be independent of customers who use it, the policy mode introduces a context object that holds the algorithm but does not decide which algorithm to choose, the algorithm selection function is handed over to the customer. After the customer selects a specific algorithm, it is set to the context object so that the context object can hold the algorithm selected by the customer, when the customer notifies the context object to execute the function, the context object will transfer the specific algorithm. In this way, the specific algorithm is separated from the customer who directly uses the algorithm.
after the specific algorithm is separated from the customer who uses it, the algorithm can change independently of the customer who uses it, and can dynamically switch the algorithm to be used, as long as the client dynamically chooses to use different algorithms and then sets them to the context object, different algorithms can be called in actual calls.
2.2 schema structure and description
The structure 1 of the Rule mode is shown in:
Figure 1 rule mode structure
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.
2.3 sample code of policy Mode
(1) first, let's look at the policy, that is, the interface that defines the algorithm. The sample code is as follows:
/** * Policy: defines the algorithm interface */ Public interface Strategy { /** * An algorithm interface can contain input parameters or return values. */ Public void algorithminterface (); } |
(2) Let's take a look at the specific Algorithm Implementation and define three, namely concretestrategya, concretestrategyb, and concretestrategyc. The example is very simple, because there is no specific algorithm implementation, the three are named differently. The sample code is as follows:
/** * Implement specific algorithms */ Public class concretestrategya implements Strategy { Public void algorithminterface (){ // Specific Algorithm Implementation } } |
/** * Implement specific algorithms */ Public class concretestrategyb implements Strategy { Public void algorithminterface (){ // Specific Algorithm Implementation } } |
/** * Implement specific algorithms */ Public class concretestrategyc implements Strategy { Public void algorithminterface (){ // Specific Algorithm Implementation } } |
(3) let's take a look at the context implementation. The sample code is as follows:
/** * Context object, usually holding a specific policy object */ Public class context { /** * Holding a specific policy object */ Private strategy Strategy; /** * Constructor to pass in a specific policy object * @ Param astrategy specific policy object */ Public context (Strategy astrategy ){ This. Strategy = astrategy; } /** * The operation interface provided by the context to the client can have parameters and return values */ Public void contextinterface (){ // It usually calls a specific policy object for algorithm operations. Strategy. algorithminterface (); } } |
2.4 example of policy mode Rewriting
The example of the previous quotation to be rewritten using the Policy mode is roughly changed as follows:
- First, you must define the algorithm interface.
- Then, separate various quote calculation methods to form an algorithm class.
- For the price class, it is used as the context. When calculating the quotation, you no longer need to judge it. You can directly use the specific algorithm you hold for calculation. Select which algorithm to use and place it on the external client.
At this time, the program structure 2 is shown as follows:
Figure 2 Structure of sample implementation using policy Mode
(1) Check the policy interface first. The sample code is as follows:
/** * Policy: defines the quote calculation algorithm interface */ Public interface Strategy { /** * Calculate the quoted price * @ Param goodsprice original price * @ Return: the price that should be quoted to the customer */ 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 customers 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 quoted price for old customers */ 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 quoted price for major customers */ Public class largecustomerstrategy implements Strategy { Public double calcprice (double goodsprice ){ System.Out. Println ("10% discount for major customers "); 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 to pass in a specific policy object * @ Param astrategy specific policy object */ Public price (Strategy astrategy ){ This. Strategy = astrategy; } /** * Quote: calculates the quotation for the customer * @ Param goodsprice original price * @ Return: the price that should be quoted to the customer */ PublicDouble 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 expected policy object strategy Strategy = new largecustomerstrategy (); // 2: create a context price CTX = new price (Strategy ); // 3: calculate the quote double quote = CTX. quote (1000); system. out . println ("quoted to the customer:" + quote ); } } |
Run the command to check the effect.
You can modify and use different policy algorithms for specific implementation. Now you are using largecustomerstrategy. You can try to change it to the other two implementations and try to see the ease of switching algorithms.
To be continued ......