长生栈 长生栈
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Living Team

编程技术分享
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 创建型模式

  • 结构型模式

  • 行为型模式

    • 责任链模式(Chain of Responsibility Pattern)
    • 命令模式(Command Pattern)
    • 解释器模式(Interpreter Pattern)
    • 迭代器模式(Iterator Pattern)
    • 中介者模式(Mediator Pattern)
    • 备忘录模式(Memento Pattern)
    • 观察者模式(Observer Pattern)
    • 状态模式(State Pattern)
    • 策略模式(Strategy Pattern)
      • 概念
      • 思想
      • 角色
      • 优点
      • 缺点
      • 类图
      • 示例代码
    • 模板方法模式(Template Method Pattern)
    • 访问者模式(Visitor Pattern)
  • 设计模式
  • 行为型模式
DC Wang
2023-05-28
目录

策略模式(Strategy Pattern)

# 策略模式(Strategy Pattern)

# 概念

策略模式是一种行为设计模式,它定义了一系列的算法,并将每个算法封装到具有共同接口的独立类中。这使得算法可以互相替换,而不会影响到客户端使用算法的方式。

# 思想

策略模式的核心思想是将算法的定义与使用相分离,通过封装每个算法为独立的策略类,客户端可以根据需求选择不同的策略来实现特定的行为。这种模式实现了开闭原则,使得系统的可维护性和扩展性更强。

# 角色

  • Context(上下文):定义了客户端所需的接口,维护一个对策略对象的引用,在运行时可切换不同的策略。
  • Strategy(策略):定义了所有支持的算法的共同接口,具体策略类实现了这个接口,提供具体的算法实现。
  • ConcreteStrategy(具体策略):实现了策略接口,提供具体的算法实现。

# 优点

  • 算法与客户端解耦:策略模式将算法封装在独立的类中,使得算法可以独立变化,不会对客户端产生影响。
  • 扩展性强:可以方便地增加新的策略类,拓展系统的功能。
  • 可维护性高:每个策略类的实现都独立于其他类,易于理解和修改。

# 缺点

  • 客户端必须了解不同的策略,选择合适的策略,增加了客户端的复杂性。
  • 策略过多时,会增加类的数量。

# 类图

@startuml

class Context {
    - strategy: Strategy
    + setStrategy(strategy: Strategy)
    + executeStrategy(): void
}

interface Strategy {
    + executeStrategy(): void
}

class ConcreteStrategyA {
    + executeStrategy(): void
}

class ConcreteStrategyB {
    + executeStrategy(): void
}

Context o-- Strategy
ConcreteStrategyA <|.. Strategy
ConcreteStrategyB <|.. Strategy

note top of Context : + strategy: Strategy\n- setStrategy(strategy: Strategy)\n- executeStrategy(): void
note bottom of Strategy : + executeStrategy(): void
note top of ConcreteStrategyA : + executeStrategy(): void
note top of ConcreteStrategyB : + executeStrategy(): void

@enduml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# 示例代码

#include <iostream>

// Strategy interface
class Strategy {
public:
    virtual void executeStrategy() = 0;
};

// Concrete strategies
class ConcreteStrategyA : public Strategy {
public:
    void executeStrategy() override {
        std::cout << "Executing strategy A." << std::endl;
    }
};

class ConcreteStrategyB : public Strategy {
public:
    void executeStrategy() override {
        std::cout << "Executing strategy B." << std::endl;
    }
};

// Context
class Context {
private:
    Strategy* strategy;

public:
    void setStrategy(Strategy* strategy) {
        this->strategy = strategy;
    }

    void executeStrategy() {
        strategy->executeStrategy();
    }
};

int main() {
    Context context;

    // Using ConcreteStrategyA
    ConcreteStrategyA strategyA;
    context.setStrategy(&strategyA);
    context.executeStrategy();

    // Using ConcreteStrategyB
    ConcreteStrategyB strategyB;
    context.setStrategy(&strategyB);
    context.executeStrategy();

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

示例代码描述: 上述示例代码实现了一个简单的策略模式。首先定义了一个策略接口(Strategy),包含了一个执行策略的纯虚函数 executeStrategy()。然后创建了两个具体策略类(ConcreteStrategyA和ConcreteStrategyB),它们分别实现了策略接口中的 executeStrategy() 函数,具体定义了不同的算法实现。接下来定义了上下文类(Context),其中包含了一个策略对象的引用,并提供了设置策略和执行策略的接口。在主函数中,我们通过创建不同的具体策略对象,并将其设置给上下文对象,然后调用上下文对象的 executeStrategy() 函数执行具体的策略。输出结果为:

Executing strategy A.
Executing strategy B.
1
2

可以看到,根据不同的策略对象,上下文对象执行了不同的策略算法。

编辑 (opens new window)
#设计模式#行为型模式
上次更新: 2023/06/09, 13:17:31
状态模式(State Pattern)
模板方法模式(Template Method Pattern)

← 状态模式(State Pattern) 模板方法模式(Template Method Pattern)→

最近更新
01
ESP32-网络摄像头方案
06-14
02
ESP32-PWM驱动SG90舵机
06-14
03
ESP32-实时操作系统freertos
06-14
更多文章>
Theme by Vdoing | Copyright © 2019-2025 DC Wang All right reserved | 辽公网安备 21021102001125号 | 吉ICP备20001966号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式