设计模式-行为型模式-策略模式

387 阅读2分钟

背景

最近项目接了一个新需求,在一个 app 页面上需要有四种情况的数据查看(实时、当日、当月、上月),每种情况下有数据汇总块、echart图表块、详细信息展示块。
一般的开发思路当然是 if-else 区分数据查看情况、然后再对每种情况进行细分,是该怎样展示的。
类似于:

if{//实时
    if{//头部数据汇总

    }else if{//图表

    }
    ……
}else if{//当日
……
}

定义

大话设计模式——策略模式:定义了算法家族,分别封装起来,让它们之间可以互相替换,此模式让算法的变化,不会影响到使用算法的客户
上面是非常术语的表达,基本上日常开发中使用策略模式的情况就是减少 if-else 分支判断,使得代码解耦,易扩展、易读。

代码实战

原始开发:

直接就一个类,一个方法,if-else 堆叠完事。其实就现在来说,代码就已经不是那么可读了,而且如果一旦需要修改代码,那是很吓人的
意味着你全部场景都得测一遍,因为你所有逻辑都在这个类里面,保不齐就改的不对了。

public class GetData {
    public static void main(String[] args) {
        String dataType = "real";
        String showType = "head";
        System.out.println(getDatafromDiff(dataType, showType));
    }
    public static Object getDatafromDiff(String dataType, String showType) {
        Object result = null;

        if ("real".equals(dataType)) { // 实时数据
            if ("head".equals(showType)) {  // 头部数据汇总
                result = "head Data";
            } else if ("chart".equals(showType)) { //echart 图表
                result = "chart Data";
            } else if ("table".equals(showType)) {//详细内容数据
                result = "table Data";
            }
        } else if ("day".equals(dataType)) { //日数据
            if ("head".equals(showType)) {

            } else if ("chart".equals(showType)) {

            } else if ("table".equals(showType)) {

            }
        } else if ("month".equals(dataType)) { //月数据
            if ("head".equals(showType)) {

            } else if ("chart".equals(showType)) {

            } else if ("table".equals(showType)) {

            }
        }
        //……
        return result;
    }
}

策略模式

主要就是一个接口类,里面有算法方法。然后就是具体实现类去实现具体的算法,
并使用 ContextStrategy 提供具体的实现类,放在一个 Map 中,使用的时候再去获取

ContextStrategy

public class ContextStrategy {
    /**
* 容器 存放策略对象ID
*/
    private Map<String, DevelopService> strategys = new HashMap<>();

    /**
* 使用无参构造方法初始化 策略配置
*/
    public ContextStrategy() {
        strategys.put("real", new DevelopRealServiceImpl());
        strategys.put("day", new DevelopDayServiceImpl());
        strategys.put("month", new DevelopMonthServiceImpl());
    }

    /**
* 获取策略
* @param typeCode
* @return
*/
    public DevelopService getDevelopMenuHeadStrategy(String typeCode) {
        return strategys.get(typeCode);
    }
}

接口-实现类

public interface DevelopService {
    String getDevelopMenuTableData(Map<String, Object> jsonMap);
    String getDevelopMenuChartData(Map<String, Object> jsonMap);
    String getDevelopMenuHeadData(Map<String, Object> jsonMap);
}
public class DevelopDayServiceImpl implements DevelopService {
    @Override
    public String getDevelopMenuTableData(Map<String, Object> jsonMap) {
        return "日数据-详细数据";
    }

    @Override
    public String getDevelopMenuChartData(Map<String, Object> jsonMap) {
        return "日数据-图标数据";
    }

    @Override
    public String getDevelopMenuHeadData(Map<String, Object> jsonMap) {
        return "日数据-汇总数据";
    }
}

总结

  • 通过策略略设计模式的使用可以把我们方法中的if语句优化掉,⼤量的if语句使用会让代码难以扩展,也不不好维护,同时在后期遇到各种问题也很难维护。在使⽤用这样的设计模式后可以很好的满⾜。
    隔离性与和扩展性,对于不断新增的需求也非常⽅便承接
  • 很多模式都是省去 if-else 判断,但是根本的实现逻辑和应用场景还是有区别的,还需要深入学习。