惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Tailwind CSS Blog
博客园 - 聂微东
S
Schneier on Security
The Last Watchdog
The Last Watchdog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Webroot Blog
Webroot Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Schneier on Security
Schneier on Security
PCI Perspectives
PCI Perspectives
C
Cyber Attacks, Cyber Crime and Cyber Security
V
Visual Studio Blog
Blog — PlanetScale
Blog — PlanetScale
Spread Privacy
Spread Privacy
Cisco Talos Blog
Cisco Talos Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
Help Net Security
Help Net Security
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
NISL@THU
NISL@THU
博客园 - Franky
N
Netflix TechBlog - Medium
Know Your Adversary
Know Your Adversary
L
Lohrmann on Cybersecurity
F
Fortinet All Blogs
WordPress大学
WordPress大学
U
Unit 42
Hacker News: Ask HN
Hacker News: Ask HN
Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理
爱范儿
爱范儿
A
Arctic Wolf
酷 壳 – CoolShell
酷 壳 – CoolShell
Application and Cybersecurity Blog
Application and Cybersecurity Blog
S
Security Affairs
H
Hacker News: Front Page
TaoSecurity Blog
TaoSecurity Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
B
Blog RSS Feed
罗磊的独立博客
Cloudbric
Cloudbric
Y
Y Combinator Blog
B
Blog
H
Help Net Security
Microsoft Azure Blog
Microsoft Azure Blog
L
LINUX DO - 最新话题
The Register - Security
The Register - Security
D
DataBreaches.Net
GbyAI
GbyAI

博客园 - 于光远

泛型编程 分区理论知识整理 SPI协议及驱动开发框架 I2C协议及驱动开发框架 使用qemu 加载linux-6.18.1内核 linux 内核 单例模式 Linux性能分析:从监控到优化的完整工具链 C++模板元编程实现序列化与反序列化 引用 bootchart linux 脚本打印时间 接口编程 不定参数解析,结合tuple 独立于main运行的程序 类模板继承 std::ostream & operator<< Flash HIDL --HelloWorld
策略模式
于光远 · 2025-11-10 · via 博客园 - 于光远

策略模式(Strategy Pattern)是一种行为型设计模式,其核心思想是定义一组算法,将每个算法封装为独立的类,使它们可以互相替换12。该模式通过将算法与使用它的客户端解耦,实现运行时动态切换算法,避免使用多重条件判断语句(如if-else)

核心特点

  1. 算法封装:每个算法被封装为独立的策略类,实现同一接口。
  2. 动态切换:客户端可在运行时选择不同策略,无需修改原有代码。
  3. 开闭原则:新增策略只需添加新类,无需修改现有代码。

优势与局限

  • 优势:

    • 算法独立于客户端,符合单一职责原则。
    • 支持运行时动态切换,灵活性高。
  • 局限:

    • 可能增加类数量,适用于多算法场景。

普通代码

// 策略接口
class Strategy {
public:
    virtual void execute() = 0;
};

// 具体策略
class StrategyA : public Strategy {
public:
    void execute() override { /* 实现A */ }
};

// 上下文类
class Context {
private:
    Strategy* strategy_;
public:
    void setStrategy(Strategy* s) { strategy_ = s; }
    void action() { strategy_->execute(); }
};
#include <iostream>

// 策略接口
template<typename T>
class OperationStrategy {
public:
    virtual T execute(T a, T b) = 0;
    virtual ~OperationStrategy() = default;
};

// 具体策略
template<typename T>
class AddOperation : public OperationStrategy<T> {
public:
    T execute(T a, T b) override {
        return a + b;
    }
};

template<typename T>
class MultiplyOperation : public OperationStrategy<T> {
public:
    T execute(T a, T b) override {
        return a * b;
    }
};

// 上下文类
template<typename T>
class Context {
private:
    OperationStrategy<T>* strategy_;
    
public:
    // 构造函数接受策略对象
    Context(OperationStrategy<T>* strategy) : strategy_(strategy) {}
    
    T executeStrategy(T a, T b) {
        return strategy_->execute(a, b);
    }
};