0
点赞
收藏
分享

微信扫一扫

GMSSL-通信

芭芭蘑菇 18小时前 阅读 2

1 概述

1.1 定义

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

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

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

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

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

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

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

1.2 解决的问题

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

问题。

2 实践

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

2.1 策略接口

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

/**
 * 数据操作策略父类
 */
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 具体策略类

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

@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 策略管理类

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

/**
 * 策略管理类
 */
@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 测试

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

    /**
     * 策略
     */
    @Test
    public void strategy() {
        int result = strategyContext.executeMathOperationStrategy
                (MathOperationStrategyTypeEnum.multiply.getCode(), 2, 7);
        System.out.println("executeStrategy result: " + result);
    }
举报

相关推荐

0 条评论