首页 > 代码库 > 状态模式
状态模式
允许对象在内部状态发生改变时改变它的行为,对象看起来好像修改了它的类。
在state模式中我们将状态逻辑和动作实现进行分离。当一个操作中要维护大量的 case 分支语句,并且这些分支依赖于对象的状态。state模式将每一个分支都封装到独立的类中。
代码:
#include <iostream> using namespace std; #define CC_SAFE_DELETE(p) if ((p)) {delete (p); (p) = NULL;} class Work; class State { public: virtual ~State() {} public: virtual void writeProgram(Work *w) = 0; }; // 上午工作状态 class ForenoonState : public State { public: virtual void writeProgram(Work *w); }; // 中午工作状态 class NoonState : public State { public: virtual void writeProgram(Work *w); }; // 下午工作状态 class AfternoonState : public State { public: virtual void writeProgram(Work *w); }; // 晚间工作状态 class EveningState : public State { public: virtual void writeProgram(Work *w); }; // 睡眠状态 class SleepingState : public State { public: virtual void writeProgram(Work *w); }; // 下班休息状态 class RestState : public State { public: virtual void writeProgram(Work *w); }; class Work { public: Work() { _curState = new ForenoonState(); _taskFinished = false; } ~Work() { CC_SAFE_DELETE(_curState); } inline void setHour(int h){ _hour = h; } inline int getHour(){ return _hour; } inline bool getTaskFinished(){ return _taskFinished; } void setTaskFinished(bool finished = true){ _taskFinished = finished; } void changeState(State *s) { if (_curState != s) { CC_SAFE_DELETE(_curState); _curState = s; } } void writeProgram() { _curState->writeProgram(this); } private: int _hour; //时间 State *_curState; //工作状态 bool _taskFinished; }; void ForenoonState::writeProgram(Work *w) { if (w->getHour() < 12) { cout << "当前时间: " << w->getHour() << "点 上午工作,精神百倍" << endl; } else { w->changeState(new NoonState()); w->writeProgram(); } } void NoonState::writeProgram(Work *w) { if (w->getHour() < 13) { cout << "当前时间: " << w->getHour() << "点 午休时间" << endl; } else { w->changeState(new AfternoonState()); w->writeProgram(); } } void AfternoonState::writeProgram(Work *w) { if (w->getHour() < 18) { cout << "当前时间: " << w->getHour() << "点 下午状态不错, 努力工作" << endl; } else { w->changeState(new EveningState()); w->writeProgram(); } } void EveningState::writeProgram(Work *w) { if (w->getTaskFinished()) { w->changeState(new RestState()); // 下班 w->writeProgram(); } else { if (w->getHour() < 22) { cout << "当前时间: " << w->getHour() << "点 加班哦, 疲惫至极" << endl; } else { w->changeState(new SleepingState()); w->writeProgram(); } } } void RestState::writeProgram(Work *w) { cout << "当前时间是: " << w->getHour() << "点 下班回家了" << endl; } void SleepingState::writeProgram(Work *w) { cout << "当前时间是: " << w->getHour() << "点, 不行了,睡觉了" << endl; } void test() { Work *w = new Work(); w->setHour(9); w->writeProgram(); w->setHour(10); w->writeProgram(); w->setHour(12); w->writeProgram(); w->setHour(14); w->writeProgram(); w->setHour(19); w->writeProgram(); //w->setTaskFinished(); w->setHour(20); w->writeProgram(); w->setHour(23); w->writeProgram(); } int main() { test(); cin.get(); return 0; }
效果:
状态模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。