• 干掉ifelse的方法


    策略模式+工厂方法消除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();
    

      

  • 相关阅读:
    DataTable转换成List
    gitbash如何修改可恶的蓝色字体
    nvm use exit status 1
    搭建CNPM私有库
    Angular2项目,刷新后页面显示404错误的?
    基于webpack模块加载,ts里对系统对象prototype的扩展
    Angular2 primeNG的p-dropdown的选中值未初始化
    移动端开发常见问题
    weinre的使用
    利用百度地图API进行GPS坐标转换成百度地图坐标,创建点,标签,多边形
  • 原文地址:https://www.cnblogs.com/q1359720840/p/15555968.html
Copyright © 2020-2023  润新知