一、什么是策略模式?
- 应用场景:定义一系列的算法,把它们一个个封装起来,并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。(官方解释)
- 特点:最终执行的结果是固定的,但是执行的过程和逻辑不一致;
- 归类:行为型模式
二、实战演练
- 事例: 商城里面购买东西不同的优惠算法,商城有满80减20,满100减30的活动
- 创建结果类,PayState.java
package com.demo.strategy;
public class PayState {
private String name;
private String activeName;
private double totalAmount;
private double discount;
private double giveAmount;
public PayState(String name,double totalAmount,double giveAmount,String activeName,double discount){
this.name = name;
this.totalAmount=totalAmount;
this.giveAmount=giveAmount;
this.activeName=activeName;
this.discount=discount;
}
public String toString(){
StringBuffer stringBuffer=new StringBuffer();
stringBuffer.append("用户:"+this.name+"\n");
stringBuffer.append("商品总金额:"+this.totalAmount+"\n");
stringBuffer.append("收到金额:"+this.giveAmount+"\n");
stringBuffer.append("满足活动:"+this.activeName+"\n");
stringBuffer.append("优惠:"+ this.discount +"\n");
stringBuffer.append("找零:"+ (this.giveAmount-this.totalAmount + this.discount) +"\n");
return stringBuffer.toString();
}
}
- 创建策略接口 Pay.java
package com.demo.strategy.active;
import com.demo.strategy.PayState;
public interface Pay {
/**
*
* @param name 用户
* @param totalAmount 商品总金额
* @param giveAmount 收到的金额
* @return
*/
public PayState payAmount(String name, double totalAmount, double giveAmount);
}
- 创建不同的活动策略实现<br />
Active0.java
package com.demo.strategy.active;
import com.demo.strategy.PayState;
/**
* 无活动
*/
public class Active0 implements Pay{
@Override
public PayState payAmount(String name, double totalAmount, double giveAmount) {
return new PayState(name,totalAmount,giveAmount,"无",0);
}
}
Active80.java
package com.demo.strategy.active;
import com.demo.strategy.PayState;
/**
* 活动满80减20
*/
public class Active80 implements Pay{
@Override
public PayState payAmount(String name, double totalAmount, double giveAmount) {
return new PayState(name,totalAmount,giveAmount,"活动满80减20",20);
}
}
Active100.java
package com.demo.strategy.active;
import com.demo.strategy.PayState;
/**
* 活动满100减30
*/
public class Active100 implements Pay{
@Override
public PayState payAmount(String name, double totalAmount, double giveAmount) {
return new PayState(name,totalAmount,giveAmount,"活动满100减30",30);
}
}
- 定义活动枚举类管理 ActiveEnum.java
package com.demo.strategy.active;
/**
* 活动枚举类
*/
public enum ActiveEnum {
ACTIVE_0(new Active0()),
ACTIVE_80(new Active80()),
ACTIVE_100(new Active100());
private Pay pay;
ActiveEnum(Pay pay){
this.pay = pay;
}
public Pay get(){ return this.pay;}
}
- 创建订单类 Order.java
package com.demo.strategy;
import com.demo.strategy.active.ActiveEnum;
public class Order {
private String name;
private double totalAmount;
private double giveAmount;
public Order(String name,double totalAmount,double giveAmount){
this.name=name;
this.totalAmount = totalAmount;
this.giveAmount=giveAmount;
}
public PayState pay(ActiveEnum ActiveEnum){
return ActiveEnum.get().payAmount(this.name,this.totalAmount,this.giveAmount);
}
}
- 最后创建测试类 PayTest.java
package com.demo.strategy;
import com.demo.strategy.active.ActiveEnum;
public class PayTest {
public static void main(String[] args) {
String name="zhangsan";
double totalAmount=120;
double giveAmount=130;
Order order=new Order(name,totalAmount,giveAmount);
//金额的逻辑判断没写,活动按照满足
System.out.println(order.pay(ActiveEnum.ACTIVE_100));
}
}
- 结果
用户:zhangsan
商品总金额:120.0
收到金额:130.0
满足活动:活动满100减30
优惠:30.0
找零:40.0