策略模式


package strategy;

public class Example {
public Double calRecharge(Double charge, RechargeTypeEnum type) {
if (type.equals(RechargeTypeEnum.E_BANK)) {
return charge * 0.85;
} else if (type.equals(RechargeTypeEnum.BUSI_ACCOUNTS)) {
return charge * 0.90;
} else if (type.equals(RechargeTypeEnum.MOBILE)) {
return charge;
} else if (type.equals(RechargeTypeEnum.CARD_RECHARGE)) {
return charge + charge * 0.01;
} else {
return null;
}
}
}

package strategy;

public enum RechargeTypeEnum {
E_BANK(1, '网银'),
BUSI_ACCOUNTS(2, '商户账号'),
MOBILE(3,'手机卡充值'),
CARD_RECHARGE(4,'充值卡')
;
private int value;
private String description;
private RechargeTypeEnum(int value, String description) {
this.value = value;
this.description = description;
}
public int value() {
return value;
}
public String description() {
return description;
}
public static RechargeTypeEnum valueOf(int value) {
for(RechargeTypeEnum type : RechargeTypeEnum.values()) {
if(type.value() == value) {
return type;
}
}
return null;
}
}可以看出上面四种不同的计算方式在一个方法内部,不利于扩展和维护,当然也不符合面向对象设计原则。对以上的代码利用策略模式进行修改,类图如下:http:// dl2.iteye.com/upload/attachment/0087/8886/3ca5e83f-f6ad-3c77-a320-e716f5c3b639.png
实例代码如下:Java代码 package strategy.strategy;

import strategy.RechargeTypeEnum;

public interface Strategy {
public Double calRecharge(Double charge, RechargeTypeEnum type);
}

package strategy.strategy;
import strategy.RechargeTypeEnum;

public class EBankStrategy implements Strategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge * 0.85;
}
}

package strategy.strategy;
import strategy.RechargeTypeEnum;

public class BusiAcctStrategy implements Strategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
// TODO Auto-generated method stub
return charge * 0.90;
}
}

package strategy.strategy;
import strategy.RechargeTypeEnum;

public class MobileStrategy implements Strategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
// TODO Auto-generated method stub
return charge;
}
}

package strategy.strategy;
import strategy.RechargeTypeEnum;

public class CardStrategy implements Strategy {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
return charge + charge * 0.01;
}
}

package strategy.strategy;
import strategy.RechargeTypeEnum;

public class Context {
private Strategy strategy;

public Double calRecharge(Double charge, Integer type) {
strategy = StrategyFactory.getInstance().creator(type);
return strategy.calRecharge(charge, RechargeTypeEnum.valueOf(type));
}

public Strategy getStrategy() {
return strategy;
}

public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
}

package strategy.strategy;
import java.util.HashMap;
import java.util.Map;
import strategy.RechargeTypeEnum;

public class StrategyFactory {
private static StrategyFactory factory = new StrategyFactory();

private StrategyFactory() {
}

private static Map strategyMap = new HashMap<>();
static {
strategyMap.put(RechargeTypeEnum.E_BANK.value(), new EBankStrategy());
strategyMap.put(RechargeTypeEnum.BUSI_ACCOUNTS.value(), new BusiAcctStrategy());
strategyMap.put(RechargeTypeEnum.MOBILE.value(), new MobileStrategy());
strategyMap.put(RechargeTypeEnum.CARD_RECHARGE.value(), new CardStrategy());
}

public Strategy creator(Integer type) {
return strategyMap.get(type);
}

public static StrategyFactory getInstance() {
return factory;
}
}

package strategy.strategy;

import strategy.RechargeTypeEnum;

public class Client {
public static void main(String[] args) {
Context context = new Context();
// 网银充值100 需要付多少
Double money = context.calRecharge(100D,
RechargeTypeEnum.E_BANK.value());
System.out.println(money);
// 商户账户充值100 需要付多少
Double money2 = context.calRecharge(100D,
RechargeTypeEnum.BUSI_ACCOUNTS.value());
System.out.println(money2);
// 手机充值100 需要付多少
Double money3 = context.calRecharge(100D,
RechargeTypeEnum.MOBILE.value());
System.out.println(money3);
// 充值卡充值100 需要付多少
Double money4 = context.calRecharge(100D,
RechargeTypeEnum.CARD_RECHARGE.value());
System.out.println(money4);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值