说下对状态模式的理解:
状态模式:当一个对象的内在状态改变时候, 允许改变其行为, 这个对象看起来像是改变了其类。
主要用来解决,当控制一个对象状态转化的条件表达式过于复杂的情况。我们可以把状态的判断逻辑转移到表示不同状态的一系列类中去, 可以实现把复杂的判断逻辑简化。
通俗的来讲, 就是可以利用状态模式将一个类中含有大量分支判断的方法做到责任分解。消除庞大的条件分支语句。
需要特别说明的是, 在这个状态模式中, 我们涉及到了使用智能指针对象管理内存, 于是setState的接口被设置为智能指针对象了,这就要求客户端传入第一个状态的时候, 是应该是堆上的内存, 而不是栈上的内存, 如果使用栈上的内存, 就不太合理了。
uml图:
运行效果图:
代码:
IState.h
#ifndef _ISTATE_H_
#define _ISTATE_H_
class CWork;
/************************************************************************/
/* 状态基类 */
/************************************************************************/
class CIState{
public:
virtual void writeProgram(CWork * work) = 0;
};
#endif // _ISTATE_H_
MainState.h
#ifndef _MAINSTATE_H_
#define _MAINSTATE_H_
#include "IState.h"
#include "work.h"
#include <iostream>
#include <memory>
using std::cout;
using std::endl;
using std::shared_ptr;
/************************************************************************/
/* 各种状态表示 */
/************************************************************************/
class SleepingState : public CIState{
public:
void writeProgram(CWork * work){
cout << "当前时间" << work->Hour() << ", 好困啊, 睡会吧" << endl;
}
};
class RestState : public CIState{
public:
void writeProgram(CWork * work){
cout << "当前时间" << work->Hour() << ", 下班回家喽" << endl;
}
};
class EveningState : public CIState{
public:
void writeProgram(CWork * work){
if (work->IsCompleted()){
shared_ptr<CIState> rest(new RestState);
work->setState(rest);
work->writeProgram();
}
else{
if (work->Hour() < 21)
cout << "当前时间" << work->Hour() << ", 加班好累啊" << endl;
else{
shared_ptr<CIState> sleep(new SleepingState);
work->setState(sleep);
work->writeProgram();
}
}
}
};
class AfterNoonState : public CIState{
public:
void writeProgram(CWork * work){
if (work->Hour() < 17)
cout << "当前时间" << work->Hour() << ", 下午状态不错, 继续努力" << endl;
else{
shared_ptr<CIState> Evening(new EveningState);
work->setState(Evening);
work->writeProgram();
}
}
};
class NoonState : public CIState{
public:
void writeProgram(CWork * work){
if (work->Hour() < 13)
cout << "当前时间" << work->Hour() << ", 饿了, 有点困, 还是午休一下吧" << endl;
else{
shared_ptr<CIState> afternoon(new AfterNoonState);
work->setState(afternoon);
work->writeProgram();
}
}
};
class ForeNoonState : public CIState{
public:
void writeProgram(CWork * work){
if (work->Hour() < 12)
cout << "当前时间" << work->Hour() << ", 感觉好爽, 嗨起来" << endl;
else{
shared_ptr<CIState> noon(new NoonState);
work->setState(noon);
work->writeProgram();
}
}
};
#endif // !_MAINSTATE_H_
work.h
#ifndef _WORK_H_
#define _WORK_H_
#include "IState.h"
#include <memory>
using std::shared_ptr;
/************************************************************************/
/* 工作类 */
/************************************************************************/
class CWork{
public:
void setState(shared_ptr<CIState> state){ this->state = state; }
void writeProgram(){
state->writeProgram(this);
}
int Hour() const { return hour; }
void Hour(int val) { hour = val; }
bool IsCompleted() const { return isCompleted; }
void IsCompleted(bool val) { isCompleted = val; }
private:
int hour;
bool isCompleted;
shared_ptr<CIState> state;
};
#endif // _WORK_H_
main.cpp
#include "MainState.h"
#include "work.h"
#include <iostream>
#include <memory>
using namespace std;
#define __HEAP__
int main(){
shared_ptr<CWork> work(new CWork);
#ifdef __HEAP__
shared_ptr<CIState> state(new ForeNoonState);
#else
ForeNoonState c;
shared_ptr<CIState> state(&c);
#endif
work->setState(state);
work->Hour(9);
work->writeProgram();
work->Hour(10);
work->writeProgram();
work->Hour(12);
work->writeProgram();
work->Hour(11);
work->writeProgram();
work->Hour(13);
work->writeProgram();
work->Hour(14);
work->writeProgram();
work->Hour(17);
work->writeProgram();
work->IsCompleted(true);
work->Hour(19);
work->writeProgram();
work->Hour(22);
work->writeProgram();
system("pause");
return 0;
}