《C++设计模式》状态模式
文章目录
- 一、前言
- 二、实现
- 一、UML类图
- 二、实现
一、前言
状态模式理解最基本上的我觉得应该也是够用了,实际用的话,也应该用的是Boost.MSM状态机。
相关代码可以在这里,如有帮助给个star!AidenYuanDev/design_patterns_in_modern_Cpp_20
二、实现
一、UML类图
二、实现
#include #include 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 } void off(Light_Switch* ls) override; }; class Off_State : public State { public: Off_State() { cout } }; class Light_Switch { private: shared_ptr} void set_state(shared_ptr state_ = std::move(state); } void on() { state_-on(this); } void off() { state_-off(this); } }; void On_State::off(Light_Switch* ls) { cout cout auto ls = make_shared
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。