干掉if-else的方法

策略模式+工厂方法消除if else

假设需求为,根据不同勋章类型,处理相对应的勋章服务,优化前有以下代码:

 

  String medalType = "guest";
    if ("guest".equals(medalType)) {
        System.out.println("嘉宾勋章");
     } else if ("vip".equals(medalType)) {
        System.out.println("会员勋章");
    } else if ("guard".equals(medalType)) {
        System.out.println("展示守护勋章");
    }
    ...

  

首先,我们把每个条件逻辑代码块,抽象成一个公共的接口,可以得到以下代码:

 

//勋章接口
public interface IMedalService {
    void showMedal();
    String getMedalType();
}

  

我们根据每个逻辑条件,定义相对应的策略实现类,可得以下代码:

 

//守护勋章策略实现类
public class GuardMedalServiceImpl implements IMedalService {
    @Override
    public void showMedal() {
        System.out.println("展示守护勋章");
    }
    @Override
    public String getMedalType() {
        return "guard";
    }
}
//嘉宾勋章策略实现类
public class GuestMedalServiceImpl implements IMedalService {
    @Override
    public void showMedal() {
        System.out.println("嘉宾勋章");
    }
    @Override
    public String getMedalType() {
        return "guest";
    }
}
//VIP勋章策略实现类
public class VipMedalServiceImpl implements IMedalService {
    @Override
    public void showMedal() {
        System.out.println("会员勋章");
    }
    @Override
    public String getMedalType() {
        return "vip";
    }
}
接下来,我们再定义

  

接下来,我们再定义策略工厂类,用来管理这些勋章实现策略类,如下:

 

//勋章服务工产类
public class MedalServicesFactory {
    private static final Map<String, IMedalService> map = new HashMap<>();
    static {
        map.put("guard", new GuardMedalServiceImpl());
        map.put("vip", new VipMedalServiceImpl());
        map.put("guest", new GuestMedalServiceImpl());
    }
    public static IMedalService getMedalService(String medalType) {
        return map.get(medalType);
    }
}

  

使用了策略+工厂模式之后,代码变得简洁多了,如下:

 

public class Test {
    public static void main(String[] args) {
        String medalType = "guest";
        IMedalService medalService = MedalServicesFactory.getMedalService(medalType);
        medalService.showMedal();
    }
}

  

使用枚举

 

String OrderStatusDes;
if(orderStatus==0){
    OrderStatusDes ="订单未支付";
}else if(OrderStatus==1){
    OrderStatusDes ="订单已支付";
}else if(OrderStatus==2){
   OrderStatusDes ="已发货"; 
}
...

  

先定义一个枚举:

 

public enum OrderStatusEnum {
    UN_PAID(0,"订单未支付"),PAIDED(1,"订单已支付"),SENDED(2,"已发货"),;
    private int index;
    private String desc;
    public int getIndex() {
        return index;
    }
    public String getDesc() {
        return desc;
    }
    OrderStatusEnum(int index, String desc){
        this.index = index;
        this.desc =desc;
    }
    OrderStatusEnum of(int orderStatus) {
        for (OrderStatusEnum temp : OrderStatusEnum.values()) {
            if (temp.getIndex() == orderStatus) {
                return temp;
            }
        }
        return null;
    }
}

  

有了枚举之后,以上if-else逻辑分支,可以优化为一行代码:

 

StringOrderStatusDes= OrderStatusEnum.0f(orderStatus).getDesc();

  

posted @ 2021-11-15 13:51  三号小玩家  阅读(107)  评论(0编辑  收藏  举报
Title
三号小玩家的 Mail: 17612457115@163.com, 联系QQ: 1359720840 微信: QQ1359720840