《大话设计模式》c++实现 状态模式
状态模式包含如下角色:
- Context: 环境类
- State: 抽象状态类
- ConcreteState: 具体状态类
2)适用场景:
a)状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。把状态的判断逻辑转移到表示不同状态的一系列类中,可以把复杂的判 断逻辑简单化。(简单来说,就是把各种if else 转变成了一个个的具体状态,原来if else 每种情况下的操作现在转换到了某个具体状态中)
b)当一个对象行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为时,就可以考虑使用状态模式了。
State.h
1 #ifndef STATE 2 #define STATE 3 extern class Work; 4 5 class State 6 { 7 public: 8 State(){}; 9 virtual void writeProgram(Work* w){}; 10 }; 11 12 class ForenoonState :public State 13 { 14 public: 15 void writeProgram(Work* w); 16 }; 17 18 19 class NoonState :public State 20 { 21 public: 22 void writeProgram(Work* w); 23 }; 24 25 26 class AfternoonState :public State 27 { 28 public: 29 void writeProgram(Work* w); 30 }; 31 32 33 class RestState :public State 34 { 35 public: 36 void writeProgram(Work* w); 37 }; 38 39 #endif
state.cpp
1 #include <iostream> 2 #include "State.h" 3 #include "Work.h" 4 5 using namespace std; 6 7 void ForenoonState :: writeProgram(Work* w) 8 { 9 if (w->getHour() < 12) 10 cout << "当前时间:" << w->getHour() << "点 " << "ForenoonState,精神百倍" << endl; 11 else 12 { 13 w->setState(new NoonState);//下一个状态 14 w->writeProgram(); 15 } 16 } 17 18 void NoonState:: writeProgram(Work* w) 19 { 20 if (w->getHour() < 13) 21 cout << "当前时间:" << w->getHour() << "点 " << "NoonState,精神百倍" << endl; 22 else 23 { 24 w->setState(new AfternoonState);//下一个状态 25 w->writeProgram(); 26 } 27 } 28 29 30 void AfternoonState :: writeProgram(Work* w) 31 { 32 33 if (w->getHour() < 17) 34 cout << "当前时间:" << w->getHour() << "点 " << "AfternoonState,精神百倍" << endl; 35 else 36 { 37 w->setState(new RestState);//下一个状态 38 w->writeProgram(); 39 } 40 41 } 42 43 44 void RestState:: writeProgram(Work* w) 45 { 46 cout << "当前时间:" << w->getHour() << "点 " << "RestState,精神百倍" << endl; 47 48 }
work.h
1 #ifndef WORK 2 #define WORK 3 4 #include "State.h" 5 class Work 6 { 7 private: 8 State* currnetState; 9 double Hour; 10 bool finish; 11 public: 12 Work(); 13 ~Work(); 14 double getHour(); 15 void setHour(double h); 16 17 bool getFinish(); 18 void setFinish(bool f); 19 20 void setState(State* s); 21 void writeProgram(); 22 }; 23 24 25 26 27 #endif
work.cpp
1 #include "Work.h" 2 3 Work::Work(){ 4 currnetState = new ForenoonState; 5 Hour = 9; 6 finish = false; 7 } 8 Work::~Work() 9 { 10 if (currnetState != 0) 11 delete currnetState; 12 } 13 14 15 double Work::getHour() 16 { 17 return Hour; 18 } 19 void Work::setHour(double h) 20 { 21 Hour = h; 22 } 23 24 bool Work::getFinish() 25 { 26 return finish; 27 } 28 void Work::setFinish(bool f) 29 { 30 finish = f; 31 } 32 33 void Work::setState(State* s) 34 { 35 if (currnetState != 0) 36 delete currnetState; 37 38 currnetState = s; 39 } 40 41 void Work::writeProgram() 42 { 43 currnetState->writeProgram(this); 44 }
main.cpp
1 #include <iostream> 2 #include <cstdlib> 3 #include "Work.h" 4 //#include "State.h" 5 6 using namespace std; 7 8 void main() 9 { 10 Work emergencyProjects; 11 12 emergencyProjects.setHour(9); 13 emergencyProjects.writeProgram(); 14 15 emergencyProjects.setHour(10); 16 emergencyProjects.writeProgram(); 17 18 emergencyProjects.setHour(12); 19 emergencyProjects.writeProgram(); 20 21 emergencyProjects.setHour(13); 22 emergencyProjects.writeProgram(); 23 24 emergencyProjects.setHour(14); 25 emergencyProjects.writeProgram(); 26 27 emergencyProjects.setHour(17); 28 emergencyProjects.writeProgram(); 29 30 emergencyProjects.setFinish(false); 31 32 emergencyProjects.setHour(19); 33 emergencyProjects.writeProgram(); 34 35 emergencyProjects.setHour(22); 36 emergencyProjects.writeProgram(); 37 38 system("pause"); 39 }