状态模式(state):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。
状态模式主要解决的是当控制一个对象状态转换的条件表达式国语复杂时的情况。把状态的判断逻辑转移到不同状态的一系列类中,可以把复杂的判断逻辑简化。例如本文的例子,只要我们把时间输入,就可以根据时间判断使用哪个状态,而不用我们在客户端来判断,客户端看到的只是很简单的代码,我们把复杂的逻辑判断转移到后台,而后台可以判断每个状态的下一个状态。
状态模式的好处是将与特定状态相关的行为局部化,并且将不同状态的行为分割开来。状态模式通过把各种状态转移逻辑分布到具体的状态类中,减少相互间的依赖。
使用场景:当一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为,就可以考虑状态模式。
#ifndef STATE_H #define STATE_H #include<iostream> #include<string> using namespace std; class Work; class ForenoonState; class NoonState; class AfternoonState; class EveningState; class RestState; class SleepingState; class State { friend class Work; public: State(){} virtual void WriteProgram(Work *w){ } }; class Work { friend class ForenoonState; public: //Work(){} State * current; double hour; bool finish = false; //(State * s):current(s) {} Work(); //Work():current(new ForenoonState){}//此处会出现没有合适默认构造函数可用的错误提示,因为虽然我们在之前声明了ForenoonState类,但是没有给出定义,所以无法构造 //但是可以在所有类声明完毕后实现这个默认构造函数。 bool TaskFinished(){ return finish; } void SetState(State * s){ current = s; } void WriteProgram() { current->WriteProgram(this); } }; class RestState :public State { public: RestState(){} void WriteProgram(Work* k) { cout << "当前时间: " << k->hour << " 下班回家了。\n"; } }; class SleepingState :public State { public: SleepingState(){} void WriteProgram(Work *k) { cout << "当前时间: " << k->hour << " 不行了,睡着了。\n"; } }; class EveningState :public State { public: EveningState(){} void WriteProgram(Work *k) { if (k->TaskFinished()) { k->SetState(new RestState); k->WriteProgram(); } else { if (k->hour < 21) { cout << "当前时间: " << k->hour << " 加班哦,疲累至极。\n"; } else{ k->SetState(new SleepingState); k->WriteProgram(); } } } }; class AfternoonState :public State { public: AfternoonState(){} void WriteProgram(Work *k) { if (k->hour<17) cout << "当前时间: " << k->hour << " 下午状态不错,继续努力\n"; else { k->SetState(new EveningState); k->WriteProgram(); } } }; class NoonState :public State { public: NoonState(){} void WriteProgram(Work *k) { if (k->hour<13) cout << "当前时间: " << k->hour << " 饿了,午饭,犯困,午休\n"; else { k->SetState(new AfternoonState); k->WriteProgram(); } } }; class ForenoonState :public State { friend class Work; public: ForenoonState(){} void WriteProgram(Work *k) { if (k->hour < 12) cout << "当前时间: " << k->hour << " 上午工作,精神百倍。\n"; else { k->SetState(new NoonState); k->WriteProgram(); } } }; Work::Work() :current(new ForenoonState){} #endif
#include"State.h" int main() { //State* start = new ForenoonState; //Work EmergencyProjects(start); Work EmergencyProjects; EmergencyProjects.hour = 9; EmergencyProjects.WriteProgram(); EmergencyProjects.hour = 10; EmergencyProjects.WriteProgram(); EmergencyProjects.hour = 12; EmergencyProjects.WriteProgram(); EmergencyProjects.hour = 13; EmergencyProjects.WriteProgram(); EmergencyProjects.hour = 14; EmergencyProjects.WriteProgram(); EmergencyProjects.hour = 17; //EmergencyProjects.finish = false; EmergencyProjects.finish = true;//设置状态不同时,下面的代码执行将出现不同的响应。 EmergencyProjects.WriteProgram(); EmergencyProjects.hour = 19; EmergencyProjects.WriteProgram(); EmergencyProjects.hour = 22; EmergencyProjects.WriteProgram(); return 0; }
时间: 2024-10-29 18:05:15