java策略模式
程序员文章站
2022-04-18 11:41:38
...
简介
策略模式(Strategy Pattern)属于对象的行为模式。其用意是针对一组算法,将每一个算法封装到具有共同接口的独立的类中,从而使得它们可以相互替换。策略模式使得算法可以在不影响到客户端的情况下发生变化。
其主要目的是通过定义相似的算法,替换if else 语句写法,并且可以随时相互替换。
策略模式主要由这三个角色组成,环境角色(Context)、抽象策略角色(Strategy)和具体策略角色(ConcreteStrategy)。
- 环境角色(Context):持有一个策略类的引用,提供给客户端使用。
- 抽象策略角色(Strategy):这是一个抽象角色,通常由一个接口或抽象类实现。此角色给出所有的具体策略类所需的接口。
- 具体策略角色(ConcreteStrategy):包装了相关的算法或行为。
代码实现:
充值方式枚举:
public enum RechargeTypeEnum {
E_BANK(1, "网银"),
BUSI_ACCOUNTS(2, "商户账号"),
MOBILE(3,"手机卡充值"),
CARD_RECHARGE(4,"充值卡")
;
private Integer value;
private String description;
private RechargeTypeEnum(Integer value, String description) {
this.value = value;
this.description = description;
}
public Integer value() {
return value;
}
public String description() {
return description;
}
public static RechargeTypeEnum valueOf(Integer value) {
for(RechargeTypeEnum type : RechargeTypeEnum.values()) {
//if(type.value() == value) {
if(type.value().equals(value)) {
return type;
}
}
return null;
}
}
策略接口定义:
public interface RechargeStrategy {
public Double calRecharge(Double charge, RechargeTypeEnum type);
public String getRechargeName(String name, RechargeTypeEnum type);
}
不同策略的具体实现类:
public class EBankRechargeStrategy implements RechargeStrategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge * 0.85;
}
@Override
public String getRechargeName(String name, RechargeTypeEnum type) {
return name + "调用了策略实现类:网银";
}
}
public class BusiAcctRechargeStrategy implements RechargeStrategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge * 0.90;
}
@Override
public String getRechargeName(String name, RechargeTypeEnum type) {
return name + "调用了策略实现类:商户账号";
}
}
public class MobileRechargeStrategy implements RechargeStrategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge;
}
@Override
public String getRechargeName(String name, RechargeTypeEnum type) {
return name + "调用了策略实现类:手机卡充值";
}
}
public class CardRechargeStrategy implements RechargeStrategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge + charge * 0.1;
}
@Override
public String getRechargeName(String name, RechargeTypeEnum type) {
return name + "调用了策略实现类:充值卡";
}
}
上下文Context定义:
public class StrategyFactory {
private static StrategyFactory factory = new StrategyFactory();
private StrategyFactory() {
}
private static Map<Integer, RechargeStrategy> strategyMap = new HashMap<>();
static {
strategyMap.put(RechargeTypeEnum.E_BANK.value(), new EBankRechargeStrategy());
strategyMap.put(RechargeTypeEnum.BUSI_ACCOUNTS.value(), new BusiAcctRechargeStrategy());
strategyMap.put(RechargeTypeEnum.MOBILE.value(), new MobileRechargeStrategy());
strategyMap.put(RechargeTypeEnum.CARD_RECHARGE.value(), new CardRechargeStrategy());
}
public RechargeStrategy creator(Integer type) {
return strategyMap.get(type);
}
public static StrategyFactory getInstance() {
return factory;
}
}
public class RechargeContext {
private RechargeStrategy strategy;
public Double calRecharge(Double charge, Integer type) {
strategy = StrategyFactory.getInstance().creator(type);
return strategy.calRecharge(charge, RechargeTypeEnum.valueOf(type));
}
public String getRechargeName(String name, Integer type) {
strategy = StrategyFactory.getInstance().creator(type);
return strategy.getRechargeName(name, RechargeTypeEnum.valueOf(type));
}
public RechargeStrategy getStrategy() {
return strategy;
}
public void setStrategy(RechargeStrategy strategy) {
this.strategy = strategy;
}
}
测试类:
@SpringBootTest
public class StrategyTest {
@Test
public void test02(){
RechargeContext recharge = new RechargeContext();
String msg = "";
msg = recharge.getRechargeName("E_BANK", RechargeTypeEnum.E_BANK.value());
System.out.println(msg);
msg = recharge.getRechargeName("BUSI_ACCOUNTS", RechargeTypeEnum.BUSI_ACCOUNTS.value());
System.out.println(msg);
msg = recharge.getRechargeName("MOBILE", RechargeTypeEnum.MOBILE.value());
System.out.println(msg);
msg = recharge.getRechargeName("CARD_RECHARGE", RechargeTypeEnum.CARD_RECHARGE.value());
System.out.println(msg);
}
@Test
public void test01(){
RechargeContext recharge = new RechargeContext();
Double money = null;
//网银
money = recharge.calRecharge(100D, RechargeTypeEnum.E_BANK.value());
System.out.println(RechargeTypeEnum.E_BANK.description() + ": " + money);
//商户账号
money = recharge.calRecharge(100D, RechargeTypeEnum.BUSI_ACCOUNTS.value());
System.out.println(RechargeTypeEnum.BUSI_ACCOUNTS.description() + ": " + money);
//手机卡充值
money = recharge.calRecharge(100D, RechargeTypeEnum.MOBILE.value());
System.out.println(RechargeTypeEnum.MOBILE.description() + ": " + money);
//充值卡
money = recharge.calRecharge(100D, RechargeTypeEnum.CARD_RECHARGE.value());
System.out.println(RechargeTypeEnum.CARD_RECHARGE.description() + ": " + money);
}
}