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

本文详细介绍了策略模式的概念,包括其在处理具有多种相似逻辑场景中的应用。通过创建策略接口、具体策略类和策略管理类,解决了if...else带来的代码复杂性和可维护性问题,以数学运算为例进行了实际操作演示。

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);
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ability Liao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值