如何通过策略模式简化 if-else?

开发 前端
假设我们要处理一个office文件,分为三种类型 docx、xlsx、pptx,分别表示Word文件、Excel文件、PPT文件,根据文件后缀分别解析。

哈喽,大家好,我是指北君。

相信大家日常开发中会经常写各种分支判断语句,比如 if-else ,当分支较多时,代码看着会比较臃肿,那么如何优化呢?

1.什么是策略模式?

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

策略模式(Strategy Pattern):定义一族算法类,将每个算法分别封装起来,让它们可以互相替换。

2.策略模式定义

图片

①Context封装角色

它也叫做上下文角色, 起承上启下封装作用, 屏蔽高层模块对策略、 算法的直接访问,封装可能存在的变化。

②Strategy 抽象策略角色

策略、 算法家族的抽象, 通常为接口, 定义每个策略或算法必须具有的方法和属性。

③ConcreteStrategy 具体策略角色

实现抽象策略中的操作, 该类含有具体的算法。

3.策略模式通用代码

public class Context {
    // 抽象策略
    private Strategy strategy = null;
    // 构造函数设置具体策略
    public Context(Strategy strategy) {
        this.strategy = strategy;
    }
    // 封装后的策略方法
    public void doAnything(){
        this.strategy.doSomething();
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
public interface Strategy {
    // 策略模式的运算法则
    public void doSomething();
}
  • 1.
  • 2.
  • 3.
  • 4.
public class ConcreteStrategy1 implements Strategy{
    @Override
    public void doSomething() {
        System.out.println("ConcreteStrategy1");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
public class ConcreteStrategy2 implements Strategy{
    @Override
    public void doSomething() {
        System.out.println("ConcreteStrategy2");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

测试:

public class StrategyClient {
    public static void main(String[] args) {
        // 声明一个具体的策略
        Strategy strategy = new ConcreteStrategy1();
        // 声明上下文对象
        Context context = new Context(strategy);
        // 执行封装后的方法
        context.doAnything();
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

4.用策略模式改写if-else

假设我们要处理一个office文件,分为三种类型 docx、xlsx、pptx,分别表示Word文件、Excel文件、PPT文件,根据文件后缀分别解析。

4.1 常规写法

public class OfficeHandler {

    public void handleFile(String filePath){
        if(filePath == null){
            return;
        }
        String fileExtension = getFileExtension(filePath);
        if(("docx").equals(fileExtension)){
            handlerDocx(filePath);
        }else if(("xlsx").equals(fileExtension)){
            handlerXlsx(filePath);
        }else if(("pptx").equals(fileExtension)){
            handlerPptx(filePath);
        }
    }

    public void handlerDocx(String filePath){
        System.out.println("处理docx文件");
    }
    public void handlerXlsx(String filePath){
        System.out.println("处理xlsx文件");
    }
    public void handlerPptx(String filePath){
        System.out.println("处理pptx文件");
    }
    private static String getFileExtension(String filePath){
        // 解析文件名获取文件扩展名,比如 文档.docx,返回 docx
        String fileExtension = filePath.substring(filePath.lastIndexOf(".")+1);
        return fileExtension;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

处理逻辑全部放在一个类中,会导致整个类特别庞大,假设我们要新增一种类型处理,比如对于2007版之前的office文件,后缀分别是 doc/xls/ppt,那我们得增加 else if 逻辑,违反了开闭原则,如何解决这种问题呢,答案就是通过策略模式。

4.2 策略模式改写

public interface OfficeHandlerStrategy {
    void handlerOffice(String filePath);
}
  • 1.
  • 2.
  • 3.
public class OfficeHandlerDocxStrategy implements OfficeHandlerStrategy {
    @Override
    public void handlerOffice(String filePath) {
        System.out.println("处理docx");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

// 省略 OfficeHandlerXlsxStrategy/OfficeHandlerPptxStrategy 类

public class OfficeHandlerStrategyFactory {
    private static final Map<String,OfficeHandlerStrategy> map = new HashMap<>();
    static {
        map.put("docx",new OfficeHandlerDocxStrategy());
        map.put("xlsx",new OfficeHandlerXlsxStrategy());
        map.put("pptx",new OfficeHandlerPptxStrategy());
    }
    public static OfficeHandlerStrategy getStrategy(String type){
        return map.get(type);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

测试:

public class OfficeHandlerStrategyClient {
    public static void main(String[] args) {
        String filePath = "C://file/123.xlsx";
        String type = getFileExtension(filePath);
        OfficeHandlerStrategy strategy = OfficeHandlerStrategyFactory.getStrategy(type);
        strategy.handlerOffice(filePath);
    }

    private static String getFileExtension(String filePath){
        // 解析文件名获取文件扩展名,比如 文档.docx,返回 docx
        String fileExtension = filePath.substring(filePath.lastIndexOf(".")+1);
        return fileExtension;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

4.策略模式优点

①算法可以自由切换

这是策略模式本身定义的, 只要实现抽象策略, 它就成为策略家族的一个成员, 通过封装角色对其进行封装, 保证对外提供“可自由切换”的策略。

②避免使用多重条件判断

简化多重if-else,或多个switch-case分支。

③扩展性良好

增加一个策略,只需要实现一个接口即可。

5.策略模式应用场景

①多个类只有在算法或行为上稍有不同的场景。

②算法需要自由切换的场景。

③需要屏蔽算法规则的场景。

责任编辑:武晓燕 来源: Java技术指北
相关推荐

2023-06-02 07:30:24

If-else结构流程控制

2020-09-27 14:24:58

if-else cod业务

2013-03-06 10:28:57

ifJava

2022-04-12 07:32:40

引擎模式Spring策略模式

2020-10-22 09:20:22

SQLNoSQL 数据库

2021-04-13 06:39:13

代码重构code

2021-03-10 07:20:43

if-else静态代码

2020-12-15 09:31:58

CTOif-else代码

2020-05-13 14:15:25

if-else代码前端

2021-11-04 08:53:00

if-else代码Java

2020-07-17 13:01:44

If-Else代码编程

2020-12-29 09:16:36

程序员对象开发

2020-04-09 08:29:50

编程语言事件驱动

2020-04-05 10:27:04

设计模式结构

2024-06-18 18:36:03

2021-01-29 07:45:27

if-else代码数据

2021-12-07 11:31:47

Python代码if…elif…els

2021-05-17 14:57:23

策略模式代码

2011-08-03 09:55:30

WindowsPowe组策略

2022-01-13 10:45:59

if-else代码Java
点赞
收藏

51CTO技术栈公众号