自学内容网 自学内容网

《C++设计模式》状态模式

一、前言

状态模式理解最基本上的我觉得应该也是够用了,实际用的话,也应该用的是Boost.MSM状态机。

相关代码可以在这里,如有帮助给个star!AidenYuanDev/design_patterns_in_modern_Cpp_20

二、实现

一、UML类图

状态模式

二、实现

#include <iostream>
#include <memory>
using namespace std;

class Light_Switch;
class On_State;
class Off_State;
class State {
public:
    virtual void on(Light_Switch* ls) = 0;![请添加图片描述](https://i-blog.csdnimg.cn/direct/9c069a22ebae485d8dbd3f084c658e5d.png)

    virtual void off(Light_Switch* ls) = 0;
};

class On_State : public State {
public:
    On_State() { cout << "灯打开了" << endl; }
    void on(Light_Switch* ls) override {}
    void off(Light_Switch* ls) override;
};

class Off_State : public State {
public:
    Off_State() { cout << "灯灭了" << endl; }
    void on(Light_Switch* ls) override;
    void off(Light_Switch* ls) override {}
};

class Light_Switch {
private:
    shared_ptr<State> state_;

public:
    Light_Switch() : state_(make_shared<Off_State>()) {}
    void set_state(shared_ptr<State> state) { state_ = std::move(state); }
    void on() { state_->on(this); }
    void off() { state_->off(this); }
};

void On_State::off(Light_Switch* ls) {
    cout << "按下关灯键" << endl;
    ls->set_state(make_shared<Off_State>());
}

void Off_State::on(Light_Switch* ls) {
    cout << "按下开灯键" << endl;
    ls->set_state(make_shared<On_State>());
}

int main() {
    auto ls = make_shared<Light_Switch>();
    ls->on();
    ls->off();
    ls->on();
    return 0;
}

原文地址:https://blog.csdn.net/Ten_years_star/article/details/140286269

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!