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

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);
    }
相关推荐
华仔啊3 小时前
别学23种了!Java项目中最常用的6个设计模式,附案例
java·后端·设计模式
Keya6 小时前
MacOS端口被占用的解决方法
前端·后端·设计模式
已读不回1437 小时前
设计模式-单例模式
前端·设计模式
long3161 天前
构建者设计模式 Builder
java·后端·学习·设计模式
一乐小哥1 天前
从 JDK 到 Spring,单例模式在源码中的实战用法
后端·设计模式
付春员1 天前
23种设计模式
设计模式
Zyy~2 天前
《设计模式》工厂方法模式
设计模式·工厂方法模式
克拉克盖博2 天前
chapter03_Bean的实例化与策略模式
java·spring·策略模式
ikkkkkkkl2 天前
C++设计模式:面向对象设计原则
c++·设计模式·面向对象
whitepure2 天前
万字详解Java中的面向对象(二)——设计模式
java·设计模式