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 清晰过渡
可复用性提升 ✅ 各状态类可在其他场景复用
相关推荐
MC皮蛋侠客2 小时前
Google Test 单元测试指南
c++·单元测试·google test
艾莉丝努力练剑3 小时前
【Linux:文件】Ext系列文件系统进阶
linux·运维·服务器·c++·文件系统·文件io·ext
五阿哥永琪4 小时前
前后端跨域的解决办法
状态模式
basketball6165 小时前
C++ NULL 和 nullptr 区别 以及 nullptr 的核心实现
java·开发语言·c++
Fre丸子_6 小时前
自定义文件夹选取功能
c++
Ulyanov7 小时前
用声明式语法重新定义Python桌面UI:QML+PySide6现代开发入门(一)
开发语言·python·算法·ui·系统仿真·雷达电子对抗仿真
思麟呀8 小时前
C++工业级日志项目(六)异步日志器
linux·c++·windows
PAK向日葵9 小时前
从零实现 Python 虚拟机(二):S.A.A.U.S.O 的总体架构设计
c++·python
无限进步_9 小时前
【C++】weak_ptr、循环引用与线程安全
开发语言·数据结构·c++·算法·安全
咩咦9 小时前
C++学习笔记30:友元类、内部类和封装
c++·学习笔记·类和对象·封装·内部类·友元类·friend