这是判断条件取反的做法,代码在逻辑表达上很清楚,如下:
if(condition) {
//do something
}else {
return xxx;
}
但是,我们完全可以先判断!condition,去掉else:
if(!condition) {
return xxx;
}
//do something
编写需要的枚举类
package cn.cheng.test;
public enum RechargeTypeEnum {
E_BANK(1,"网银"),
ALI_PAY(2,"支付宝"),
WECHAT(3,"微信"),
CREDIT_CARD(4,"信用卡");
private int code;
private String desc;
private RechargeTypeEnum(int code , String desc) {
this.code=code;
this.desc=desc;
}
public int getCode() {
return code;
}
public String getDesc() {
return desc;
}
public static RechargeTypeEnum valueOf(Integer code) {
for(RechargeTypeEnum type:RechargeTypeEnum.values()) {
if(type.getCode()==code) {
return type;
}
}
return null;
}
}
编写策略接口、接口实现类
package cn.cheng.test;
/**
* 策略接口
* @author Administrator
*
*/
public interface Strategy {
public Double calRecharge(Double charge, RechargeTypeEnum type);
}
/**
* 策略实现类
* @author Administrator
*
*/
class EbankStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
//业务逻辑
return charge*0.1;
}
}
/**
* 策略实现类
* @author Administrator
*
*/
class AlipayStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
//业务逻辑
return charge *0.2;
}
}
/**
* 策略实现类
* @author Administrator
*
*/
class WechatStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
//业务逻辑
return charge *0.3;
}
}
/**
* 策略实现类
* @author Administrator
*
*/
class CreditCardStrategy implements Strategy{
@Override
public Double calRecharge(Double charge, RechargeTypeEnum type) {
//业务逻辑
return charge *0.4;
}
}
编写策略工厂类
package cn.cheng.test;
import java.util.HashMap;
import java.util.Map;
/**
* 策略工厂类
* @author Administrator
*
*/
public class StrategyFactory {
private static StrategyFactory factory=new StrategyFactory();
private StrategyFactory(){
}
private static Map<Integer, Strategy> strategyMap=new HashMap<Integer, Strategy>();
static {
strategyMap.put(RechargeTypeEnum.E_BANK.getCode(), new EbankStrategy());
strategyMap.put(RechargeTypeEnum.ALI_PAY.getCode(), new AlipayStrategy());
strategyMap.put(RechargeTypeEnum.WECHAT.getCode(), new WechatStrategy());
strategyMap.put(RechargeTypeEnum.CREDIT_CARD.getCode(), new CreditCardStrategy());
}
public static StrategyFactory getInstance() {
return factory;
}
public Strategy getStrategyMapByKey(Integer code) {
return strategyMap.get(code);
}
}
编写策略上下文
package cn.cheng.test;
/**
* 策略上下文
* @author Administrator
*
*/
public class Context {
private Strategy strategy;
public Strategy getStrategy() {
return strategy;
}
public void setStrategy(Strategy strategy) {
this.strategy = strategy;
}
public Double calRecharge(Double charge, Integer code) {
strategy = StrategyFactory.getInstance().getStrategyMapByKey(code);
return strategy.calRecharge(charge, RechargeTypeEnum.valueOf(code));
};
}
编写测试代码
package cn.cheng.test;
/**
* 策略模式把具体的算法封装到了具体策略角色内部,增强了可扩展性,隐藏了实现细节,同时避免了多个if-else不好维护的条件语句;
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
//1.策略模式+工厂模式
Context context=new Context();
Double money1 = context.calRecharge(100D, RechargeTypeEnum.ALI_PAY.getCode());
Double money2 = context.calRecharge(100d, RechargeTypeEnum.WECHAT.getCode());
Double money3 = context.calRecharge(100D, RechargeTypeEnum.E_BANK.getCode());
Double money4 = context.calRecharge(100D, RechargeTypeEnum.CREDIT_CARD.getCode());
System.out.println(money1);
System.out.println(money2);
System.out.println(money3);
System.out.println(money4);
System.out.println("===============================");
}
}
运行结果:
20.0
30.0
10.0
40.0
总结:这种策略优化if/else的方案把具体的算法封装到了具体策略角色内部,增强了可扩展性,隐藏了实现细节,同时避免了多个if-else不好维护的条件语句,但是也有一个弊端,为了能够快速拿到对应的策略实现,需要map对象来保存策略,当添加一个新的策略的时候,还需要手动添加到map中,容易被忽视;
因为枚举中可以定义抽象方法,并使每个枚举实例来实现该方法,同样是上面的例子,使用枚举来优化if/else;
编写枚举类:
package cn.cheng.test;
public enum RechargeTypeEnum2 {
E_BANK(1,"网银") {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum2 type) {
return charge*0.1;
}
},
ALI_PAY(2,"支付宝") {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum2 type) {
return charge*0.2;
}
},
WECHAT(3,"微信") {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum2 type) {
return charge*0.3;
}
},
CREDIT_CARD(4,"信用卡") {
@Override
public Double calRecharge(Double charge, RechargeTypeEnum2 type) {
return charge*0.4;
}
};
private int code;
private String desc;
private RechargeTypeEnum2(int code , String desc) {
this.code=code;
this.desc=desc;
}
public int getCode() {
return code;
}
public String getDesc() {
return desc;
}
public static RechargeTypeEnum2 valueOf(Integer code) {
for(RechargeTypeEnum2 type:RechargeTypeEnum2.values()) {
if(type.getCode()==code) {
return type;
}
}
return null;
}
public abstract Double calRecharge(Double charge, RechargeTypeEnum2 type);
}
编写测试代码:
package cn.cheng.test;
/**
* 枚举方式;
* @author Administrator
*
*/
public class Test {
public static void main(String[] args) {
//枚举方式
RechargeTypeEnum2 typeEnum1 = RechargeTypeEnum2.valueOf(1);
Double m1 = typeEnum1.calRecharge(100D,typeEnum1 );
RechargeTypeEnum2 typeEnum2 = RechargeTypeEnum2.valueOf(2);
Double m2 = typeEnum2.calRecharge(100D,typeEnum2 );
RechargeTypeEnum2 typeEnum3 = RechargeTypeEnum2.valueOf(3);
Double m3 = typeEnum3.calRecharge(100D,typeEnum3 );
RechargeTypeEnum2 typeEnum4 = RechargeTypeEnum2.valueOf(4);
Double m4 = typeEnum4.calRecharge(100D,typeEnum4 );
System.out.println(m1);
System.out.println(m2);
System.out.println(m3);
System.out.println(m4);
System.out.println("-----------------------------------------");
}
}
运行结果:
10.0
20.0
30.0
40.0
总结:通过枚举优化之后发现比策略模式简单的多,所以推荐使用枚举的方式优化if/else代码;