C++ 设计模式《外卖骑手状态系统》

👨‍🎓 模式名称:状态模式(State)

👦 故事背景:

👦 小明在他的平台引入了外卖骑手系统,其中就需要及时记录和更新骑手的状态。骑手的状态分为:

  • 待接单
  • 配送中
  • 休息中

不同状态下,系统允许执行的操作不同:

状态 允许操作
待接单 可以接单
配送中 可以送达
休息中 什么都不能做

😵 不使用状态模式的痛点

小明一开始直接用了一个大 switch-case 写法:

cpp 复制代码
enum RiderState { Idle, Delivering, Resting };

class Rider {
public:
    RiderState state;

    void handle(const std::string& action) {
        if (state == Idle) {
            if (action == "接单") std::cout << "订单接下来了!" << std::endl;
            else std::cout << "当前不能 " << action << std::endl;
        } else if (state == Delivering) {
            if (action == "送达") std::cout << "订单已送达!" << std::endl;
            else std::cout << "当前不能 " << action << std::endl;
        } else if (state == Resting) {
            std::cout << "休息中,啥也不能干..." << std::endl;
        }
    }
};

问题来了:

  • 每次加新状态或行为,都要修改 handle 函数
  • 状态行为之间 耦合严重
  • 系统难以扩展和维护

✅ 使用状态模式进行优化

1️⃣ 状态接口(抽象状态类)
cpp 复制代码
class RiderContext;

class RiderState {
public:
    virtual void handle(RiderContext* context, const std::string& action) = 0;
    virtual std::string name() const = 0;
    virtual ~RiderState() {}
};
2️⃣ 具体状态类
cpp 复制代码
class IdleState : public RiderState {
public:
    void handle(RiderContext* context, const std::string& action) override;
    std::string name() const override { return "待接单"; }
};

class DeliveringState : public RiderState {
public:
    void handle(RiderContext* context, const std::string& action) override;
    std::string name() const override { return "配送中"; }
};

class RestingState : public RiderState {
public:
    void handle(RiderContext* context, const std::string& action) override;
    std::string name() const override { return "休息中"; }
};
3️⃣ 状态上下文类
cpp 复制代码
class RiderContext {
private:
    RiderState* state;
public:
    RiderContext(RiderState* init) : state(init) {}

    void setState(RiderState* s) {
        state = s;
        std::cout << "状态切换为:" << state->name() << std::endl;
    }

    void request(const std::string& action) {
        state->handle(this, action);
    }
};
4️⃣ 状态行为实现
cpp 复制代码
void IdleState::handle(RiderContext* context, const std::string& action) {
    if (action == "接单") {
        std::cout << "✅ 订单已接下!" << std::endl;
        static DeliveringState delivering;
        context->setState(&delivering);
    } else {
        std::cout << "❌ 当前状态不能执行:" << action << std::endl;
    }
}

void DeliveringState::handle(RiderContext* context, const std::string& action) {
    if (action == "送达") {
        std::cout << "✅ 外卖已送达!" << std::endl;
        static IdleState idle;
        context->setState(&idle);
    } else {
        std::cout << "❌ 当前状态不能执行:" << action << std::endl;
    }
}

void RestingState::handle(RiderContext* context, const std::string& action) {
    std::cout << "😴 休息中,不能进行任何操作..." << std::endl;
}
使用示例
cpp 复制代码
int main() {
    IdleState idle;
    RiderContext rider(&idle);

    rider.request("接单");
    rider.request("送达");

    static RestingState rest;
    rider.setState(&rest);
    rider.request("接单");
}

使用状态模式的优点

问题 是否解决
状态逻辑封装 ✅ 各状态类独立处理自己的逻辑
行为扩展方便 ✅ 新状态只需添加类,无需修改已有逻辑
状态切换明确 ✅ 使用 setState 清晰过渡
可复用性提升 ✅ 各状态类可在其他场景复用
相关推荐
_哆啦A梦1 天前
Vibe Coding 全栈专业名词清单|设计模式·基础篇(创建型+结构型核心名词)
前端·设计模式·vibecoding
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
修炼前端秘籍的小帅4 天前
Stitch——Google热门的免费AI UI设计工具
前端·人工智能·ui
王码码20354 天前
Flutter for OpenHarmony:socket_io_client 实时通信的事实标准(Node.js 后端的最佳拍档) 深度解析与鸿蒙适配指南
android·flutter·ui·华为·node.js·harmonyos
蜡笔小马4 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝4 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc4 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法