赞
踩
- class State
- {
- public:
- virtual void Handle(Context *context);
- };
- class ConcreteStateA :public State
- {
- public:
- void Handle(Context *context)
- {
- context->state = new ConcreteStateB();//设置下一个状态
- }
- };
-
- class ConcreteStateB :public State
- {
- public:
- void Handle(Context *context)
- {
- context->state = new ConcreteStateA();
- }
- };
- class Context
- {
- private:
- State *state;
-
- public:
- Context(State *state)//定义初始状态
- {
- this->state = state;
- }
-
- void setState(State *state)//设置当前的状态
- {
- this->state = state;
- }
-
- State *getState()//读取当前的状态
- {
- return this->state();
- }
-
- void Request()
- {
- state->Handle();//对当前状态下的请求做处理
- }
- };
- int main()
- {
- Context *context = new Context(new ConcreteStateA());
-
- context->Request();
- }
- #ifndef _STATE_H_
- #define _STATE_H_
-
- class Work;
-
- class State
- {
- public:
- virtual void writeProgram(Work *w);
- };
-
- class ForenoonState :public State
- {
- public:
- void writeProgram(Work *w) override;
- };
-
- class NoonState :public State
- {
- public:
- void writeProgram(Work *w) override;
- };
-
- class AfternoonState :public State
- {
- public:
- void writeProgram(Work *w) override;
- };
-
- class EveningState :public State
- {
- public:
- void writeProgram(Work *w) override;
- };
-
- class SleepingState :public State
- {
- public:
- void writeProgram(Work *w) override;
- };
-
- class RestState :public State
- {
- public:
- void writeProgram(Work *w) override;
- };
-
- #endif
- #include"State.h"
- #include"Work.h"
- #include<iostream>
-
- using std::cout;
- using std::endl;
-
- void State::writeProgram(Work *w)
- {
- cout << "开始写程序" << endl;
- }
-
- void ForenoonState::writeProgram(Work *w)
- {
- if (w->getHour() < 12)
- {
- cout << "时间:" << w->getHour() << "\t精神百倍!" << endl;
- }
- else
- {
- w->setState(new NoonState());
- w->writeProgram();
- }
- }
-
- void NoonState::writeProgram(Work *w)
- {
- if (w->getHour() < 13)
- {
- cout << "时间:" << w->getHour() << "\t中午困死了,能不能休息会!" << endl;
- }
- else
- {
- w->setState(new AfternoonState());
- w->writeProgram();
- }
- }
-
- void AfternoonState::writeProgram(Work *w)
- {
- if (w->getHour() < 17)
- {
- cout << "时间:" << w->getHour() << "\t下午的状态还是不错的,继续加油!" << endl;
- }
- else
- {
- w->setState(new EveningState());
- w->writeProgram();
- }
- }
-
- void EveningState::writeProgram(Work *w)
- {
- if (w->getFinish())
- {
- cout << "工作完成! ";
- w->setState(new RestState());
- w->writeProgram();
- }
- else
- {
- if (w->getHour() < 21)
- {
- cout << "时间:" << w->getHour() << "\t加班啊,可恶!" << endl;
- }
- else
- {
- w->setState(new SleepingState());
- w->writeProgram();
- }
- }
- }
-
- void SleepingState::writeProgram(Work *w)
- {
- cout << "时间:" << w->getHour() << "\t困得不行了,睡着了!" << endl;
- }
-
- void RestState::writeProgram(Work *w)
- {
- cout << "时间:" << w->getHour() << "\t下班回家!" << endl;
- }
- #ifndef _WORK_H_
- #define _WORK_H_
-
- class State;
-
- class Work
- {
- private:
- State *state;
- double hour;
- bool finish;
-
- public:
- Work();
-
- void setState(State *state);
- State *getState();
-
- void setHour(double hour);
- double getHour();
-
- void setFinish(bool finish);
- bool getFinish();
-
- void writeProgram();
- };
-
- #endif
- #include"Work.h"
- #include"State.h"
-
- Work::Work()
- {
- state = new ForenoonState();
- this->hour = 9;
- this->finish = false;
- }
-
- void Work::setState(State *state)
- {
- this->state = state;
- }
-
- State * Work::getState()
- {
- return this->state;
- }
-
- void Work::setHour(double hour)
- {
- this->hour = hour;
- }
-
- double Work::getHour()
- {
- return this->hour;
- }
-
- void Work::setFinish(bool finish)
- {
- this->finish = finish;
- }
-
- bool Work::getFinish()
- {
- return this->finish;
- }
-
- void Work::writeProgram()
- {
- this->state->writeProgram(this);
- }
- #include"State.h"
- #include"Work.h"
- #include<iostream>
-
- using namespace std;
-
- int main()
- {
- Work *work = new Work();
- work->setHour(9);
- work->writeProgram();
- work->setHour(12.5);
- work->writeProgram();
- work->setHour(16);
- work->writeProgram();
- work->setHour(20);
- work->writeProgram();
- work->setFinish(true);
- work->writeProgram();
- work->setHour(10);
- //时间重置,那么状态也应该重置一下
- work->setState(new ForenoonState());
- work->setFinish(false);
- work->writeProgram();
- work->setHour(22);
- work->writeProgram();
-
- system("pause");
- return 0;
- }
时间:9 精神百倍!
时间:12.5 中午困死了,能不能休息会!
时间:16 下午的状态还是不错的,继续加油!
时间:20 加班啊,可恶!
工作完成! 时间:20 下班回家!
时间:10 精神百倍!
时间:22 困得不行了,睡着了!
请按任意键继续. . .
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。