Scene
A market personnel received a single post-quote strategy (FAQ in CRM system) The quotation strategy is very complex, can be easily categorized as follows:
- Low-volume quotation for ordinary customers
- General customer high-volume quotation
- Low-volume quotation for old customers
- High-volume quotation for old customers
The specific choice of which bidding strategy, which needs to be determined according to the actual situation. At this point, we can use the strategy model.
We first use conditional statements to deal with
public class Test {public double getprice (String type,double price) {if (Type.equals ("Normal customer small batch") {System.out.println (" No discount, the original price "); else if (type.equals ("Normal customer bulk")) {System.out.println ("90 percent"); return price*0.9;} else if (type.equals ("Old Customer small batch")) {System.out.println ("hit 85 percent"); return price*0.85;} else if (type.equals ("Old Customer Bulk")) {System.out.println ("80 percent"); return price*0.8;} return price;}}
If, the type is very many, the algorithm is more complex, the whole condition control code will become very long, difficult to maintain.
The policy pattern corresponds to the algorithm family that solves a problem, allowing the user to solve a problem by selecting an algorithm from the algorithm family.
At the same time can easily change the algorithm or add a new algorithm. And the client decides which algorithm to call.
Advantages
- Can dynamically change the behavior of an object
Disadvantages
- The client must know all of the policy classes and decide for itself which policy class to use
- Policy mode will result in many policy classes
composition
- Environment Class (context): Configured with a Concretestrategy object. Maintains a reference to the Strategy object. You can define an interface to let strategy access its data.
- Abstract policy Class (strategy): Defines the public interface for all supported algorithms. The context uses this interface to invoke an algorithm defined by a concretestrategy.
- Specific policy class (Concretestrategy): Implement a specific algorithm with strategy interface.
Public interface Discountstratery {public double getdiscount (double originprice);} public class Vipdicount implements Discountstratery {public double getdiscount (double originprice) {//override Getdiscount () method , provide VIP discount algorithm System.out.println ("Use VIP discount ..."); return originprice * 0.5;}} public class Olddicount implements Discountstratery {public double getdiscount (double originprice) {//override Getdiscount () method , provide VIP discount algorithm System.out.println ("Use old books discount ..."); return originprice * 0.7;}} public class Discountcontext {//combine a Discountstratery object private discountstratery strategy;public Discountcontext ( Discountstratery strategy) {this.strategy = strategy;} Get a discounted price based on the actual Discountstratery object used public double Getdiscountprice (double prices) {//if strategy is NULL The system automatically selects the Olddicount class if (strategy = = NULL) {strategy = new Olddicount ();} return This.strategy.getDiscount (price);} Method of providing a switching algorithm public void Changediscount (Discountstratery strategy) {this.strategy = strategy;}} public class Strategyclient {public static void main (string[] Args) {//client did not select the discount policy class Discountcontext dc = new Discountcontext (null);d ouble price = 79;//Use the default discount policy System.out.println (" The price of the 79 Yuan book after the default discount is: "+ dc.getdiscountprice";//Client Select the appropriate VIP discount policy dc.changediscount (new Vipdicount ());d ouble Price2 = 89; System.out.println ("89 yuan book price for VIP users is" + Dc.getdiscountprice (Price2));}}
Essence
Separation algorithm, select the implementation.
common scenarios in development :
- In GUI programming in Java, Layout management
- Spring Framework, resource interface, resource access policy
- Java.servlet.http.httpservlet#service ()
Java design mode (16): Behavioral-policy mode (strategy)