设计模式 策略模式(Strategy Pattern) C++表达

设计模式 策略模式(Strategy Pattern) C++表达

flyfish

策略模式(Strategy Pattern)是一种行为设计模式,它的核心思想是将一系列相关的算法或行为封装到独立的策略类中,并使得这些策略可以相互替换。主要用来定义一系列可互换的算法或行为。它允许在运行时选择和切换这些算法或行为,而不用修改使用它们的代码。

策略模式就像给的代码提供了一组不同的工具,每个工具都可以完成相似的工作(例如不同的送货方式)。可以根据需要随时更换这些工具,而不需要修改太多代码。

举个例子

想象一下,有一家快递公司,提供了不同的送货方式:比如通过快递车、无人机或自行车送货。每种送货方式都有不同的优缺点和适用场景。希望可以根据具体情况选择最合适的送货方式。

如何实现?

  1. 定义策略接口 :首先,定义一个公共接口,比如 DeliveryMethod,里面有一个 deliver 方法,所有具体的送货方式都要实现这个方法。
cpp 复制代码
class DeliveryMethod {
public:
    virtual void deliver() const = 0; // 定义送货方法
    virtual ~DeliveryMethod() = default; // 虚析构函数
};
  1. 实现具体策略 :然后,为每种送货方式实现这个接口,比如 TruckDeliveryDroneDeliveryBikeDelivery
cpp 复制代码
class TruckDelivery : public DeliveryMethod {
public:
    void deliver() const override {
        std::cout << "Delivering by truck." << std::endl;
    }
};

class DroneDelivery : public DeliveryMethod {
public:
    void deliver() const override {
        std::cout << "Delivering by drone." << std::endl;
    }
};

class BikeDelivery : public DeliveryMethod {
public:
    void deliver() const override {
        std::cout << "Delivering by bike." << std::endl;
    }
};
  1. 上下文类 :然后,有一个上下文类 DeliveryContext,它包含一个 DeliveryMethod 的对象,并在需要的时候调用 deliver 方法。这个上下文类可以动态地更换送货方式。
cpp 复制代码
class DeliveryContext {
private:
    std::shared_ptr<DeliveryMethod> deliveryMethod; // 存储送货方法的指针
public:
    void setDeliveryMethod(std::shared_ptr<DeliveryMethod> method) {
        deliveryMethod = method;
    }

    void executeDelivery() const {
        if (deliveryMethod) {
            deliveryMethod->deliver();
        }
    }
};
  1. 使用策略模式 :在 main 函数中,可以创建不同的送货方式,并根据情况切换它们。
cpp 复制代码
int main() {
    DeliveryContext context;

    // 设置并执行卡车送货
    context.setDeliveryMethod(std::make_shared<TruckDelivery>());
    context.executeDelivery();

    // 设置并执行无人机送货
    context.setDeliveryMethod(std::make_shared<DroneDelivery>());
    context.executeDelivery();

    // 设置并执行自行车送货
    context.setDeliveryMethod(std::make_shared<BikeDelivery>());
    context.executeDelivery();

    return 0;
}

完整示例:

cpp 复制代码
#include <iostream>
#include <memory>

// 策略接口类
class DeliveryMethod {
public:
    virtual void deliver() const = 0; // 定义送货方法
    virtual ~DeliveryMethod() = default; // 虚析构函数
};

// 具体策略A类 - 卡车送货
class TruckDelivery : public DeliveryMethod {
public:
    void deliver() const override {
        std::cout << "Delivering by truck." << std::endl;
    }
};

// 具体策略B类 - 无人机送货
class DroneDelivery : public DeliveryMethod {
public:
    void deliver() const override {
        std::cout << "Delivering by drone." << std::endl;
    }
};

// 具体策略C类 - 自行车送货
class BikeDelivery : public DeliveryMethod {
public:
    void deliver() const override {
        std::cout << "Delivering by bike." << std::endl;
    }
};

// 上下文类
class DeliveryContext {
private:
    std::shared_ptr<DeliveryMethod> deliveryMethod; // 存储送货方法的指针

public:
    // 设置策略方法
    void setDeliveryMethod(std::shared_ptr<DeliveryMethod> method) {
        deliveryMethod = method;
    }

    // 执行策略方法
    void executeDelivery() const {
        if (deliveryMethod) {
            deliveryMethod->deliver();
        }
    }
};

int main() {
    DeliveryContext context; // 创建上下文对象

    // 设置并执行卡车送货
    context.setDeliveryMethod(std::make_shared<TruckDelivery>());
    context.executeDelivery();

    // 设置并执行无人机送货
    context.setDeliveryMethod(std::make_shared<DroneDelivery>());
    context.executeDelivery();

    // 设置并执行自行车送货
    context.setDeliveryMethod(std::make_shared<BikeDelivery>());
    context.executeDelivery();

    return 0;
}

策略模式还是这样写

  1. 使用 shared_ptr :允许多个上下文对象共享同一个策略对象,节省内存。
  2. 策略工厂和注册机制 :使用 StrategyFactory 类进行策略的注册和创建,支持动态添加新策略,并避免重复创建策略对象。
  3. 简化策略设置 :通过策略名称字符串来设置策略。
cpp 复制代码
#include <iostream>
#include <memory>
#include <unordered_map>
#include <functional>

// 策略接口类
class Strategy {
public:
    virtual ~Strategy() = default; // 虚析构函数,确保派生类对象可以被正确销毁
    virtual void execute() const = 0; // 纯虚函数,定义了算法的接口
};

// 具体策略A类
class ConcreteStrategyA : public Strategy {
public:
    void execute() const override {
        std::cout << "Strategy A executed." << std::endl;
    }
};

// 具体策略B类
class ConcreteStrategyB : public Strategy {
public:
    void execute() const override {
        std::cout << "Strategy B executed." << std::endl;
    }
};

// 策略工厂类
class StrategyFactory {
public:
    using CreateStrategyFn = std::function<std::shared_ptr<Strategy>()>;

    static StrategyFactory& instance() {
        static StrategyFactory factory;
        return factory;
    }

    void registerStrategy(const std::string& name, CreateStrategyFn createFn) {
        strategyMap_[name] = createFn;
    }

    std::shared_ptr<Strategy> getStrategy(const std::string& name) {
        auto it = strategyMap_.find(name);
        if (it != strategyMap_.end()) {
            return it->second();
        }
        return nullptr;
    }

private:
    std::unordered_map<std::string, CreateStrategyFn> strategyMap_;
};

// 上下文类
class Context {
private:
    std::shared_ptr<Strategy> strategy_; // 存储策略对象的智能指针

public:
    // 设置策略方法
    void setStrategy(const std::string& strategyName) {
        strategy_ = StrategyFactory::instance().getStrategy(strategyName);
    }

    // 执行策略方法
    void executeStrategy() const {
        if (strategy_) {
            strategy_->execute(); // 调用当前策略的执行方法
        }
    }
};

int main() {
    // 注册策略
    StrategyFactory::instance().registerStrategy("A", []() { return std::make_shared<ConcreteStrategyA>(); });
    StrategyFactory::instance().registerStrategy("B", []() { return std::make_shared<ConcreteStrategyB>(); });

    Context context; // 创建上下文对象

    // 设置并执行策略A
    context.setStrategy("A");
    context.executeStrategy();

    // 设置并执行策略B
    context.setStrategy("B");
    context.executeStrategy();

    return 0;
}
相关推荐
2301_818419018 小时前
C++中的解释器模式变体
开发语言·c++·算法
爱学习的大牛1238 小时前
windows tcpview 类似功能 c++
c++
biter down9 小时前
C++11 统一列表初始化+std::initializer_list
开发语言·c++
ShineWinsu10 小时前
爬虫对抗:ZLibrary反爬机制实战分析技术文章大纲
c++
charlie11451419111 小时前
通用GUI编程技术——Win32 原生编程实战(十六)——Visual Studio 资源编辑器使用指南
开发语言·c++·ide·学习·gui·visual studio·win32
DpHard11 小时前
现代 C++ 中 push 接口为何提供 const T& 与 T&& 两个重载
c++
U-52184F6912 小时前
深度解析:从 Qt 的 Q_D 宏说起,C++ 工业级 SDK 是如何保证 ABI 稳定性的
数据库·c++·qt
泯仲12 小时前
Ragent项目7种设计模式深度解析:从源码看设计模式落地实践
java·算法·设计模式·agent
hz_zhangrl13 小时前
CCF-GESP 等级考试 2026年3月认证C++三级真题解析
c++·算法·程序设计·gesp·gesp2026年3月·gesp c++三级
kyle~14 小时前
C++----函数指针与函数指针类型 返回值类型 (*类型名)(参数列表)
开发语言·c++