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mV0ZVN0YXRl19PA4LXE0ru49sq1wP2jrNXiuPbKtcD9tqjS5bWxx7C1xNe0zKyhozwvcD4KPHA+CjxzdHJvbmc+o6gyo6lTdGF0ZcDgo7o8L3N0cm9uZz6z6c/z17TMrMDgo6y2qNLl0ru49iC907/a0tS34tew0+tDb250ZXh0tcTSu7j2zNi2qNe0zKzP4LnYtcTQ0M6qoaM8L3A+CjxwPgo8c3Ryb25nPqOoM6OpQ29uY3JldGVTdGF0ZUEsQ29uY3JldGVTdGF0ZULA4KO6PC9zdHJvbmc+vt/M5de0zKzA4KOsw7/Su7j219MgwODKtc/W0ru49tPrQ29udGV4dLXE0ru49te0zKzP4LnYtcTQ0M6qoaM8L3A+CjxwPjxzdHJvbmc+y8ShokMmIzQzOyYjNDM7yrXP1jwvc3Ryb25nPjwvcD4KPHA+PHN0cm9uZz6jqDGjqVN0YXRlLmg8L3N0cm9uZz6jqLPpz/PXtMyswOC6zb7fzOXXtMyswOC1xMn5w/e6zbao0uW2vNTaU3RhdGUuaLrNU3RhdGUuY3Bw1tCjqTwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">#ifndef STATE
#define STATE
//#include "Work.h"
extern class Work;
class State
{
public:
State(){};
virtual void writeProgram(Work* w){};
};
class ForenoonState:public State
{
public:
void writeProgram(Work* w);
};
class NoonState:public State
{
public:
void writeProgram(Work* w);
};
class AfternoonState:public State
{
public:
void writeProgram(Work* w);
};
class EveningState:public State
{
public:
void writeProgram(Work* w);
};
class SleepingState:public State
{
public:
void writeProgram(Work* w);
};
class RestState:public State
{
public:
void writeProgram(Work* w);
};
#endif
£¨2£©State.cpp
#include#include "State.h" #include "Work.h" using namespace std; void ForenoonState::writeProgram(Work* w) { if(w->getHour()<12) { cout<<"µ±Ç°Ê±¼ä£º"< getHour()<<"µã "<<"ÉÏÎ繤×÷£¬¾«Éñ°Ù±¶"< setState(s); w->writeProgram(); } } void NoonState::writeProgram(Work* w) { if(w->getHour()<13) { cout<<"µ±Ç°Ê±¼ä£º"< getHour()<<"µã "<<"¶öÁË£¬Îç·¹£»·¸À§£¬ÎçÐÝ"< setState(new AfternoonState); w->writeProgram(); } } void AfternoonState::writeProgram(Work* w) { if(w->getHour()<17) cout<<"µ±Ç°Ê±¼ä£º"< getHour()<<"µã "<<"ÏÂÎç״̬»¹²»´í£¬¼ÌÐøŬÁ¦"< setState(new EveningState); w->writeProgram(); } } void EveningState::writeProgram(Work* w) { if(w->getFinish()) { w->setState(new RestState); w->writeProgram(); } else { if(w->getHour()<21) cout<<"µ±Ç°Ê±¼ä£º"< getHour()<<"µã "<<"¼Ó°àŶ£¬Æ£±¹ÖÁ¼«"< setState(new SleepingState); w->writeProgram(); } } } void SleepingState::writeProgram(Work* w) { cout<<"µ±Ç°Ê±¼ä£º"< getHour()<<"µã "<<"²»ÐÐÁË£¬Ë¯×ÅÁË"< getHour()<<"µã "<<"Ï°à»Ø¼ÒÁË"<
£¨3£©Work.h£¨ÕâÀïµÄWork¾ÍÊÇ״̬ģʽÖеÄContext£©
#ifndef WORK #define WORK #include "State.h" class Work { private: State* current; double Hour; bool finish; public: Work(); double getHour(); void setHour(double HOUR); bool getFinish(); void setFinish(bool FINISH); void setState(State* s); void writeProgram(); }; #endif
£¨4£©Work.cpp
#include "Work.h" Work::Work() { current=new ForenoonState; Hour=9; finish=false; } double Work::getHour() { return Hour; } void Work::setHour(double HOUR) { Hour=HOUR; } bool Work::getFinish() { return finish; } void Work::setFinish(bool FINISH) { finish=FINISH; } void Work::setState(State* s) { current=s; } void Work::writeProgram() { current->writeProgram(this); }
£¨5£©main.cpp£¨¿Í»§¶Ë£©
#include#include #include "Work.h" //#include "State.h" using namespace std; void main() { Work emergencyProjects; emergencyProjects.setHour(9); emergencyProjects.writeProgram(); emergencyProjects.setHour(10); emergencyProjects.writeProgram(); emergencyProjects.setHour(12); emergencyProjects.writeProgram(); emergencyProjects.setHour(13); emergencyProjects.writeProgram(); emergencyProjects.setHour(14); emergencyProjects.writeProgram(); emergencyProjects.setHour(17); emergencyProjects.writeProgram(); emergencyProjects.setFinish(false); emergencyProjects.setHour(19); emergencyProjects.writeProgram(); emergencyProjects.setHour(22); emergencyProjects.writeProgram(); system("pause"); }
×¢Ò⣺ÉÏÃæµÄ´úÂë»á³öÏÖÁ½¸öÀàÏ໥µ÷ÓõÄÇé¿ö£¬Çë²é¿´ÈÕÖ¾£ºC++ÖÐÁ½¸öÀ໥ÏàÒýÓõĽâ¾ö·½·¨