Intent: Define a series of algorithms, encapsulate them one by one, and make them interchangeable with each other. The policy pattern allows the algorithm to change independently of the customers who use it.
1. Need to use the algorithm provided by Concretestrategy (specific policy role);
2. Maintain an instance of strategy (abstract policy Class) internally;
3. Responsible for the dynamic setting of the runtime strategy specific implementation algorithm;
4. Responsible for the interaction and data transfer with strategy.
Main Solution: the use of if...else is complicated and difficult to maintain in the case of many similar algorithms.
when to use: a system has many classes, and it is only their direct behavior that distinguishes them.
How to solve: encapsulate these algorithms into a single class and replace them arbitrarily.
Key code: implement the same interface.
Application Example: 1, Zhuge Liang's ace, every one of the Jin sac is a strategy. 2, Travel way, choose to ride a bike, ride a car, every way of travel is a strategy.
Advantages: 1, the algorithm can be switched freely. 2, avoid the use of multiple conditions to judge. 3, good Extensibility.
Cons: 1, each strategy will produce a new class, the policy class will increase. 2. All policy classes need to be exposed externally. Choosing which class requires the client to create the object increases the coupling.
usage Scenario: 1, if there are many classes in a system, the difference between them is only their behavior, then the use of policy mode can dynamically let an object in many behaviors to choose a behavior. 2, a system needs to dynamically select one of several algorithms. 3, if an object has a lot of behavior, if not the appropriate pattern, these behaviors have to use multiple conditional selection statements to achieve.
Note: If a system has more than four policies, you need to consider using mixed mode to solve the problem of policy class bloat.
1 Public Interface pricestrategy {2 double pricestrategyinterface (double price ); 3 }
1 public class Supervipstrategt implements Pricestrategy { 2 @Override 3 public double Pricestrategyinterface (double price) { 4 ; 5 6 7 }
1 public class Goldvipstrategt implements Pricestrategy { 2 3 @Override 4 public double pricestrategyinterface (double " Span style= "color: #000000;" > price) { 5 return Price*0.9; 6 7 }
1 public class Slivervipstrategy implements Pricestrategy { 2 3 @Override 4 public double pricestrategyinterface (double " Span style= "color: #000000;" > price) { 5 return Price*0.95
;
6
7 }
Public class Price { private pricestrategy pricestrategy; Public Price (Pricestrategy pricestrategy) { this.pricestrategy=pricestrategy; } publicdouble getvipprice (double price ) { return pricestrategy.pricestrategyinterface (price);} }
Public classTest { Public Static voidMain (string[] args) {supervipstrategt supervipstrategt=NewSupervipstrategt (); Goldvipstrategt Goldvipstrategt=NewGoldvipstrategt (); Slivervipstrategy Slivervipstrategy=NewSlivervipstrategy (); System.out.println ("Super member Price =" +NewPrice (SUPERVIPSTRATEGT). Getvipprice (100)); System.out.println ("Gold member Price =" +NewPrice (GOLDVIPSTRATEGT). Getvipprice (100)); System.out.println ("Silver member Price =" +NewPrice (Slivervipstrategy). Getvipprice (100)); }}
Super member Price =80.0
Gold member Price =90.0
Silver Member Price =95.0
Strategy Mode of design pattern