设计模式--策略模式

策略模式详解与示例

一、什么是策略模式?

策略模式(Strategy Pattern)是一种行为型设计模式,它定义了一系列算法,将每一个算法封装起来,使得它们可以互换。策略模式让算法的变化独立于使用算法的客户程序,使得算法的选择和使用更加灵活和可扩展。

二、策略模式的组成

策略模式主要由以下角色组成:

  1. 策略接口(Strategy):定义所有支持的算法的公共接口。
  2. 具体策略(ConcreteStrategy):实现策略接口,提供具体的算法实现。
  3. 上下文(Context):持有一个策略对象,并可以在运行时更换策略对象。
三、策略模式的优点与缺点

优点

  • 算法封装:将不同的算法封装在不同的类中,具有更好的扩展性和可维护性。
  • 提高灵活性:客户端可以根据需要动态地选择算法,增强了程序的灵活性。
  • 降低耦合:策略模式降低了系统中各个类之间的耦合度,使得系统更加模块化。

缺点

  • 增加类的数量:策略模式会产生大量的策略类,可能会增加系统的复杂度。
四、策略模式的实现

下面通过一个简单的 Java 示例来演示策略模式的实现。假设我们有一个计算器程序,支持不同的运算方式(加法、减法、乘法、除法)。

1. 定义策略接口

策略接口定义了所有支持的算法的公共接口:

java 复制代码
public interface Strategy {
    int doOperation(int num1, int num2);
}

2. 实现具体策略

具体策略实现了策略接口中的方法,提供具体的算法实现:

java 复制代码
public class AdditionStrategy implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1 + num2;
    }
}

public class SubtractionStrategy implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1 - num2;
    }
}

public class MultiplicationStrategy implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        return num1 * num2;
    }
}

public class DivisionStrategy implements Strategy {
    @Override
    public int doOperation(int num1, int num2) {
        if (num2 == 0) {
            throw new IllegalArgumentException("Cannot divide by zero");
        }
        return num1 / num2;
    }
}

3. 定义上下文类

上下文类使用策略接口,持有一个具体的策略对象,并可以在运行时更换策略对象:

java 复制代码
public class Calculator {
    private Strategy strategy;

    public Calculator(Strategy strategy) {
        this.strategy = strategy;
    }

    public int executeStrategy(int num1, int num2) {
        return strategy.doOperation(num1, num2);
    }

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }
}

4. 测试策略模式

下面的代码演示了如何使用策略模式进行运算:

java 复制代码
public class Main {
    public static void main(String[] args) {
        Calculator calculator = new Calculator(new AdditionStrategy());
        System.out.println("10 + 5 = " + calculator.executeStrategy(10, 5));

        calculator.setStrategy(new SubtractionStrategy());
        System.out.println("10 - 5 = " + calculator.executeStrategy(10, 5));

        calculator.setStrategy(new MultiplicationStrategy());
        System.out.println("10 * 5 = " + calculator.executeStrategy(10, 5));

        calculator.setStrategy(new DivisionStrategy());
        System.out.println("10 / 5 = " + calculator.executeStrategy(10, 5));
    }
}

输出

java 复制代码
10 + 5 = 15
10 - 5 = 5
10 * 5 = 50
10 / 5 = 2
五、总结

策略模式通过将不同的算法封装在各个策略类中,允许客户端在运行时动态地选择和更换算法。它提高了系统的灵活性和可维护性,同时降低了系统的耦合度。

相关推荐
略略略咯咯8 小时前
(总结)设计模式
设计模式
Cosolar21 小时前
DeepSeek Harness 理解 Harness 的设计哲学 - 可组合的插件运行时
人工智能·设计模式·架构
Nebula_g1 天前
JavaSE基础语法:特殊类(特殊情景下的设计模式)
java·开发语言·设计模式
George_Ye2 天前
同一本书,一人一份:我如何设计 AI 个性化扫书报告
设计模式
莫得感情 o2 天前
设计模式 22 · 三个冷门模式:中介者、访问者、解释器
java·设计模式
AI人工智能+电脑小能手3 天前
大白话说Java设计模式-08-建造者模式(业务实战篇)
java·设计模式·建造者模式·架构设计·对象构建
coding4u3 天前
Mac 外接显示器完全指南:从插上线到调好每个参数
macos·计算机外设·文件管理·显示器·策略模式·quick look扩展
AustinXu3 天前
从 Claude Code 到 Claude Tag,Harness Engineering 走到了组织这一层
设计模式·团队管理
剧中有戏3 天前
单例模式从入门到精通:一个数据库连接池的完整剖析
设计模式
胡萝卜术3 天前
编译期与运行期的双重防线:从 TypeScript 类型之争到 LLM 输出的自动化择优
前端·设计模式·面试