行为型模式-策略模式的概述与实践

1 概述

1.1 定义

策略模式是针对对象行为的编程模式。下面是我对策略模式的理解。

(1)定义:在某个处理流程中,存在多个类似或平级的处理逻辑(或算法),并且执行哪个或哪

几个逻辑由具体条件来确定。此时可以将这些处理逻辑抽象为策略接口和具体策略类,由策略管理

类来管理这些策略。通过策略管理器来选择和执行具体的策略。

(2)策略模式的关键点在于"策略接口"、"具体策略类"和"策略管理类"。

(3)使用策略模式的关键步骤为:

  • 创建表示各种策略的具体策略类,且实现同一个策略接口;
  • 创建一个根据行为类型动态选择和执行对应策略的策略管理类。

1.2 解决的问题

在有多种相似算法的情况下,使用策略模式可以解决使用 if...else 所带来的复杂性和可维护性差的

问题。

2 实践

下面是使用策略模式来管理不同类型的数学运算逻辑的案例。

2.1 策略接口

下面是数学运算策略接口类。

java 复制代码
/**
 * 数据操作策略父类
 */
public interface MathOperationStrategy {

    /**
     * 判断是否执行此策略
     * @param operateType
     * @return
     */
    boolean check(Integer operateType);

    /**
     * 执行策略
     *
     * @param num1
     * @param num2
     * @return
     */
    int doOperation(int num1, int num2);
}


/**
 * 数学运算策略类型
 */
public enum MathOperationStrategyTypeEnum {
    /**
     * 常用类型
     */
    add(1, "加"),
    subtract(2, "减"),
    multiply(3, "乘"),
    ;

    private Integer code;
    private String name;

    MathOperationStrategyTypeEnum(Integer code, String name) {
        this.code = code;
        this.name = name;
    }

    public Integer getCode() {
        return code;
    }

    public String getName() {
        return name;
    }
}

2.2 具体策略类

下面是不同类型的数学运算策略类。

java 复制代码
@Service
public class OperationAdd implements MathOperationStrategy {

    @Override
    public boolean check(Integer operateType) {
        return Objects.equal(MathOperationStrategyTypeEnum.add.getCode(), operateType);
    }

    @Override
    public int doOperation(int num1, int num2) {
        return num1 + num2;
    }

}


@Service
public class OperationSubtract implements MathOperationStrategy {


    @Override
    public boolean check(Integer operateType) {
        return Objects.equal(MathOperationStrategyTypeEnum.subtract.getCode(), operateType);
    }

    @Override
    public int doOperation(int num1, int num2) {
        return num1 - num2;
    }

}


@Service
public class OperationMultiply implements MathOperationStrategy {

    @Override
    public boolean check(Integer operateType) {
        return Objects.equal(MathOperationStrategyTypeEnum.multiply.getCode(), operateType);
    }

    @Override
    public int doOperation(int num1, int num2) {
        return num1 * num2;
    }

}

2.3 策略管理类

下面是策略管理类。可以用于管理不同的策略类。

java 复制代码
/**
 * 策略管理类
 */
@Service
public class StrategyContext {
    @Resource
    private List<MathOperationStrategy> mathOperationStrategyList;


    /**
     * 选择和执行数学运算策略
     *
     * @param operateType 操作类型
     * @param num1
     * @param num2
     * @return
     * @see MathOperationStrategyTypeEnum 操作类型
     */
    public int executeMathOperationStrategy(int operateType, int num1, int num2) {
        for (MathOperationStrategy strategy : mathOperationStrategyList) {
            if (strategy.check(operateType)) {
                return strategy.doOperation(num1, num2);
            }
        }
        throw new UnsupportedOperationException("该操作类型:[" + operateType + "]暂不支持");
    }
    
    
}

2.4 测试

下面是通过策略管理类选择和执行具体的策略。

java 复制代码
    /**
     * 策略
     */
    @Test
    public void strategy() {
        int result = strategyContext.executeMathOperationStrategy
                (MathOperationStrategyTypeEnum.multiply.getCode(), 2, 7);
        System.out.println("executeStrategy result: " + result);
    }
相关推荐
sg_knight3 小时前
设计模式实战:代理模式(Proxy)
python·设计模式·代理模式·proxy
Anurmy4 小时前
设计模式之命令模式
设计模式·命令模式
喵叔哟5 小时前
2.【.NET10 实战--孢子记账--产品智能化】--升级前的准备工作:项目依赖梳理与升级计划制定
.net·策略模式
五点六六六11 小时前
基于 AST 与 Proxy沙箱 的局部代码热验证
前端·设计模式·架构
qq_2320455720 小时前
精积微半导体面试(部分)
netty·策略模式·nio·内存抖动·threadlocal·bitmap·复用
wwdoffice01101 天前
304和316不锈钢有什么区别?哪个更好?
设计模式
网小鱼的学习笔记1 天前
创建型设计模式(工厂、builder、原型、单例)
java·后端·设计模式
逆境不可逃1 天前
【从零入门23种设计模式21】行为型之空对象模式
java·开发语言·数据库·算法·设计模式·职场和发展
蜜獾云2 天前
设计模式之命令模式:给其他模块下达命令
设计模式·命令模式
小湘西2 天前
拓扑排序(Topological Sort)
python·设计模式