状态模式
“小菜,你们的加班没完没了了?”大鸟为晚上十点才到家的小菜打开了房门。
“嗨,没办法,公司的项目很急,所以要求要加班。”
“有这么急吗?这星期四天来你都在加班,有加班费吗?难道周末也要继续?”
“哪来什么加班费,周末估计是逃不了了。”小菜显然很疲惫,“经历把每个人每天的工作都排得满满的,说做完就可以回家,但是没有任何一个人可以在下班前完成的,基本都得加班,这就等于是资源加班。我走时还有哥们在加班呢。”
“再急也不能这样呀,长时间甲苯又没有加班费,士气更低落,效率大打折扣。”
“可不是!上午刚上班的时候,效率很高,可以写不少代码,到了中午,午饭一吃完,就犯困,可能是最近太累了,但还不敢休息,因为没人趴着睡觉的,都说项目急,要抓紧。所以我就这么迷迷糊糊的,到了下午三点多才略微精神点,本想着今天任务还算可以,希望能早点完成,争取不要再加班了。哪知快下班时才发现有一个功能是我理解有误,其实比想想的要复杂得多。嗨!苦呀,又多花了三个多钟头,九点多才从公司出来。”
“那你用代码来实现一下你说的这些状态吧。”
工作状态-函数版
#include <stdio.h>
static int Hour = 0; // 钟点
static bool WorkFinished = false; // 任务完成标记
static void WriteProgram()
{
if(Hour < 12)
{
printf("当前时间:%d点 上午工作,精神百倍\n",Hour);
}
else if(Hour < 13)
{
printf("当前时间:%d点 饿了,午饭;犯困,午休。\n",Hour);
}
else if(Hour < 17)
{
printf("当前时间:%d点 下午状态还不错,继续努力\n",Hour);
}
else
{
if(WorkFinished)
{
printf("当前时间:%d点 下班回家了\n",Hour);
}
else
{
if(Hour < 21)
{
printf("当前时间:%d点 加班哦,疲累之极\n",Hour);
}
else
{
printf("当前时间:%d点 不行了,睡着了。\n",Hour);
}
}
}
}
int main()
{
Hour = 9;
WriteProgram();
Hour = 10;
WriteProgram();
Hour = 12;
WriteProgram();
Hour = 13;
WriteProgram();
Hour = 14;
WriteProgram();
Hour = 17;
WriteProgram();
WorkFinished = true;
WriteProgram();
Hour = 19;
WriteProgram();
Hour = 22;
WriteProgram();
return 0;
}
“小菜,都学了这么长时间的面向对象开发,你怎么还在写面向过程的代码呀?”
“啊,我习惯性思维了,你意思是说要分一个类出来。”
“这是起码的面向对象思维呀,至少应该有个“工作”类,你的‘写程序’方法是类方法,而‘钟点’、‘任务完成’其实就是类的什么?”
“应该是对外属性,是吧?”
“问什么问,还不快去重写。”
工作状态-分类版
#include <stdio.h>
class Work
{
public:
int Hour; // 钟点
bool WorkFinished; // 任务完成标记
void WriteProgram()
{
if(Hour < 12)
{
printf("当前时间:%d点 上午工作,精神百倍\n",Hour);
}
else if(Hour < 13)
{
printf("当前时间:%d点 饿了,午饭;犯困,午休。\n",Hour);
}
else if(Hour < 17)
{
printf("当前时间:%d点 下午状态还不错,继续努力\n",Hour);
}
else
{
if(WorkFinished)
{
printf("当前时间:%d点 下班回家了\n",Hour);
}
else
{
if(Hour < 21)
{
printf("当前时间:%d点 加班哦,疲累之极\n",Hour);
}
else
{
printf("当前时间:%d点 不行了,睡着了。\n",Hour);
}
}
}
}
};
int main()
{
Work* emergencyProjects = new Work();
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->WorkFinished = false;
emergencyProjects->WriteProgram();
emergencyProjects->Hour = 19;
emergencyProjects->WriteProgram();
emergencyProjects->Hour = 22;
emergencyProjects->WriteProgram();
delete emergencyProjects;
return 0;
}
方法过长是坏味道。
状态模式
状态模式主要解决的是当控制一个对象状态转换的条件表达式国语复杂时的情况。把状态的判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化。
实现方式(UML类图)
实现代码
#include <stdio.h>
class State;
// 工作
class Work
{
public:
Work();
~Work();
void SetState(State* s);
void WriteProgram();
int Hour;
bool Finished;
protected:
State* current;
};
// 抽象状态
class State
{
public:
virtual void WriteProgram(Work* w)=0;
};
// 下班休息状态
class RestState : public State
{
public:
virtual void WriteProgram(Work* w)
{
printf("当前时间:%d点 下班回家了\n",w->Hour);
}
};
// 睡眠状态
class SleepingState : public State
{
public:
virtual void WriteProgram(Work* w)
{
printf("当前时间:%d点 不行了,睡着了。\n",w->Hour);
}
};
// 晚间工作状态
class EveningState : public State
{
public:
virtual void WriteProgram(Work* w)
{
if(w->Finished)
{
w->SetState(new RestState());
w->WriteProgram();
}
else
{
if(w->Hour < 21) printf("当前时间:%d点 加班哦,疲累之极\n",w->Hour);
else
{
w->SetState(new SleepingState());
w->WriteProgram();
}
}
}
};
// 下午工作状态
class AfternoonState : public State
{
public:
virtual void WriteProgram(Work* w)
{
if(w->Hour < 17) printf("当前时间:%d点 下午状态还不错,继续努力\n",w->Hour);
else
{
w->SetState(new EveningState());
w->WriteProgram();
}
}
};
// 中午工作状态
class NoonState : public State
{
public:
virtual void WriteProgram(Work* w)
{
if(w->Hour < 13) printf("当前时间:%d点 饿了,午饭;犯困,午休。\n",w->Hour);
else
{
w->SetState(new AfternoonState());
w->WriteProgram();
}
}
};
// 上午工作状态
class ForenoonState : public State
{
public:
virtual void WriteProgram(Work* w)
{
if(w->Hour < 12) printf("当前时间:%d点 上午工作,精神百倍\n",w->Hour);
else
{
w->SetState(new NoonState());
w->WriteProgram();
}
}
};
Work::Work()
{
current = new ForenoonState();
}
Work::~Work()
{
delete current;
}
void Work::SetState(State* s)
{
delete current;
current = s;
}
void Work::WriteProgram()
{
current->WriteProgram(this);
}
int main()
{
Work* emergencyProjects = new Work();
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->Finished = false;
emergencyProjects->WriteProgram();
emergencyProjects->Hour = 19;
emergencyProjects->WriteProgram();
emergencyProjects->Hour = 22;
emergencyProjects->WriteProgram();
delete emergencyProjects;
return 0;
}
运行结果
所有文件打包下载
posted on 2011-07-03 22:05
lwch 阅读(2765)
评论(2) 编辑 收藏 引用 所属分类:
设计模式