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

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);
    }
相关推荐
刷帅耍帅4 小时前
设计模式-享元模式
设计模式·享元模式
刷帅耍帅4 小时前
设计模式-模版方法模式
设计模式
刷帅耍帅5 小时前
设计模式-桥接模式
设计模式·桥接模式
MinBadGuy7 小时前
【GeekBand】C++设计模式笔记5_Observer_观察者模式
c++·设计模式
刷帅耍帅7 小时前
设计模式-生成器模式/建造者模式Builder
设计模式·建造者模式
蜡笔小新..1 天前
【设计模式】软件设计原则——开闭原则&里氏替换&单一职责
java·设计模式·开闭原则·单一职责原则
性感博主在线瞎搞1 天前
【面向对象】设计模式概念和分类
设计模式·面向对象·中级软件设计师·设计方法
lucifer3111 天前
JavaScript 中的组合模式(十)
javascript·设计模式
lucifer3111 天前
JavaScript 中的装饰器模式(十一)
javascript·设计模式
蜡笔小新..1 天前
【设计模式】软件设计原则——依赖倒置&合成复用
设计模式·依赖倒置原则·合成复用原则