I. Introduction of the Strategy model
Policy mode: It defines the algorithm family, individually encapsulated, so that they can be replaced with each other, this mode allows the algorithm changes will not affect the use of the algorithm customers.
For example:
The cashier software in the mall, how to make different discounts for different products? Or, how to make different discount according to different membership level?
In fact, the internal definition of an algorithm family, to each member or commodity to specify a specific algorithm, according to different algorithms, to play a different discount.
Policy Mode UML diagram:
strategy (Abstract Algorithm Interface): Defines a common interface for all supported algorithms
Concretestrategya,concretestrategyb, and concretestrategyc: Inherited from strategy, encapsulating specific algorithms
context: Different algorithm implementations for maintaining different objects
Second, the Strategy mode code implementation
Here are the examples of different users buying merchandise discounts
First define an abstract algorithm interface:
12345 |
//抽象算法接口:定义了所有支持算法的公共接口 public interface Strategy { //算法方法:打印商品的价格(不同的实现打不同的折扣) public double getPrice( double price); } |
And then define the specific individual algorithms
1234567891011121314151617181920212223242526272829303132 |
//具体的算法:普通用户,不打折
public class GeneralUser
implements Strategy {
@Override
public double getPrice(
double price) {
System.out.println(
"普通用户,不打折"
);
return price;
}
}
//具体的算法:注册用户:打9折
public class RegisterUser
implements Strategy{
@Override
public double getPrice(
double price) {
System.out.println(
"注册用户:打9折"
);
return price*
0.9
;
}
}
//具体的算法:普通会员:打8折
public class RegisterVip
implements Strategy{
@Override
public double getPrice(
double price) {
System.out.println(
"普通会员:打8折"
);
return price*
0.8
;
}
}
//具体的算法:老会员:打5折
public class OldVip
implements Strategy{
@Override
public double getPrice(
double price) {
System.out.println(
"老会员:打5折"
);
return price*
0.5
;
}
}
|
The next step is to define a context that maintains different discounts for different users.
123456789101112 |
//上下文:用来维护不同对象的不同折扣
public class Context {
private Strategy strategy;
//持有算法族的引用
public Context(Strategy strategy) {
super
();
this
.strategy = strategy;
}
//打印价钱
public void printPrice(
double price){
System.out.println(
"应付金额:"
+Math.round(strategy.getPrice(price)));
}
}
|
Client Test Code:
1234567891011121314151617 |
public static void main(String[] args) {
double price =
998
;
//商品价格
Strategy generalUser =
new GeneralUser();
//普通用户
Strategy registerUser =
new RegisterUser();
//注册用户
Strategy registerVip =
new RegisterVip();
//普通会员
Strategy oldVip =
new OldVip();
//老会员
//根据不同的用户打不同的折扣
Context c1 =
new Context(generalUser);
c1.printPrice(price);
Context c2 =
new Context(registerUser);
c2.printPrice(price);
Context c3 =
new Context(registerVip);
c3.printPrice(price);
Context c4 =
new Context(oldVip);
c4.printPrice(price);
}
|
The printing results are as follows:
Normal user, no discount
Amount Due: 998
Registered users: Call 90 percent
Amount Due: 898
Regular members: 80 percent off
Amount Due: 798
Old member: Call 50 percent
Amount Due: 499
Third, the application scenario
The essence of policy mode: separating algorithm, choosing different implementation.
Application Scenarios:
Layout management of Javase
Spring Framework, resource interface, resource access policy
Javax.servlet.http.httpservlet#service ();
From for notes (Wiz)
Policy mode (strategy)