posts - 183,  comments - 10,  trackbacks - 0
来自于《大话设计模式》
状态模式(State):当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

UML 类图:


代码实现 C++:
  1 #include <iostream>
  2 using namespace std;
  3 
  4 class Work;
  5 class State;
  6 double getHour(Work* w);
  7 void   setState(Work * w, State * s);
  8 bool   getFinish(Work* w);
  9 void   WriteProgramOutside(Work * w);
 10 
 11 class State
 12 {
 13 public:
 14     virtual void WriteProgram(Work * w) = 0;
 15 };
 16 
 17 class RestState : public State
 18 {
 19 public:
 20     virtual void WriteProgram(Work * w)
 21     {
 22         cout << "当前时间:" << getHour(w) << " 点下班回家了。" << endl;
 23     }
 24 };
 25 
 26 class SleepingState : public State
 27 {
 28 public:
 29     virtual void WriteProgram(Work * w)
 30     {
 31         cout << "当前时间:" << getHour(w) << " 点不行了,睡着了。" << endl;
 32     }
 33 };
 34 
 35 class EveningState : public State
 36 {
 37 public:
 38     virtual void WriteProgram(Work * w)
 39     {
 40         if (getFinish(w))
 41         {
 42             setState(w, static_cast<State*>(new RestState));
 43             WriteProgramOutside(w);
 44         }
 45         else
 46         {
 47             if (getHour(w) < 21.0)
 48             {
 49                 cout << "当前时间:" << getHour(w) << " 点 加班哦,疲惫之极。" << endl;
 50             }
 51             else
 52             {
 53                 setState(w, new SleepingState);
 54                 WriteProgramOutside(w);
 55             }
 56         }
 57     }
 58 };
 59 
 60 class AfternoonState : public State
 61 {
 62 public:
 63     virtual void WriteProgram(Work * w)
 64     {
 65         if (getHour(w) < 17.0)
 66         {
 67             cout << "当前时间:" << getHour(w) << " 点 下午状态还不错,继续努力。" << endl;
 68         }
 69         else
 70         {
 71             setState(w, new EveningState);
 72             WriteProgramOutside(w);
 73         }
 74     }
 75 };
 76 
 77 class NoonState : public State
 78 {
 79 public:
 80     virtual void WriteProgram(Work * w)
 81     {
 82         if (getHour(w) < 13.0)
 83         {
 84             cout << "当前时间:" << getHour(w) << " 点 饿了,午饭;犯困,午休。" << endl;
 85         }
 86         else
 87         {
 88             setState(w, new AfternoonState);
 89             WriteProgramOutside(w);
 90         }
 91     }
 92 };
 93 
 94 class ForenoonState : public State
 95 {
 96 public:
 97     virtual void WriteProgram(Work* w)
 98     {
 99         if (getHour(w) < 12.0)
100         {
101             cout << "当前时间:" << getHour(w) << " 点 上午工作,精神百倍!" << endl;
102         }
103         else
104         {
105             setState(w, new NoonState);
106             WriteProgramOutside(w);
107         }
108     }
109 };
110 
111 class Work
112 {
113 private:
114     State * current;
115     double hour;
116     bool finish;
117 public:
118     double getHour()
119     {
120         return hour;
121     }
122     void setHour(double h)
123     {
124         hour = h;
125     }
126     bool getFinish()
127     {
128         return finish;
129     }
130     void setFinsh(bool f)
131     {
132         finish = f;
133     }
134     void setState(State * w)
135     {
136         delete current;
137         current = w;
138     }
139     void WriteProgram()
140     {
141         current->WriteProgram(this);
142     }
143     Work()
144     {
145         current = new ForenoonState;
146         finish = false;
147     }
148     ~Work()
149     {
150         delete current;
151     }
152 };
153 
154 double getHour(Work * w)
155 {
156     return w->getHour();
157 }
158 void setHour(Work * w, double h)
159 {
160     w->setHour(h);
161 }
162 bool getFinish(Work * w)
163 {
164     return w->getFinish();
165 }
166 void setFinish(Work * w, bool f)
167 {
168     w->setFinsh(f);
169 }
170 void setState(Work * w, State * s)
171 {
172     w->setState(s);
173 }
174 
175 void WriteProgramOutside(Work * w)
176 {
177     w->WriteProgram();
178 }
179 
180 int main()
181 {
182     Work* emergencyProjects = new Work;
183     emergencyProjects->setHour(9.0);
184     emergencyProjects->WriteProgram();
185     emergencyProjects->setHour(10.0);
186     emergencyProjects->WriteProgram();
187     emergencyProjects->setHour(12.0);
188     emergencyProjects->WriteProgram();
189     emergencyProjects->setHour(13.0);
190     emergencyProjects->WriteProgram();
191     emergencyProjects->setHour(14.0);
192     emergencyProjects->WriteProgram();
193     emergencyProjects->setHour(17.0);
194     emergencyProjects->WriteProgram();
195 
196     // emergencyProjects->setFinsh(true);
197     emergencyProjects->setFinsh(false);
198 
199     emergencyProjects->WriteProgram();
200     emergencyProjects->setHour(19.0);
201     emergencyProjects->WriteProgram();
202     emergencyProjects->setHour(22.0);
203     emergencyProjects->WriteProgram();
204 
205     delete emergencyProjects;
206     return 0;
207 }
posted on 2011-04-27 16:39 unixfy 阅读(211) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理