首页 > 代码库 > 状态模式

状态模式

 允许对象在内部状态发生改变时改变它的行为,对象看起来好像修改了它的类。

在state模式中我们将状态逻辑和动作实现进行分离。当一个操作中要维护大量的 case 分支语句,并且这些分支依赖于对象的状态。state模式将每一个分支都封装到独立的类中。

代码:

技术分享
#include <iostream>
using namespace std;

#define CC_SAFE_DELETE(p) if ((p)) {delete (p); (p) = NULL;}

class Work;

class State
{
public:
    virtual ~State() {}
public:
    virtual void writeProgram(Work *w) = 0;
};

// 上午工作状态
class ForenoonState : public State
{
public:
    virtual void writeProgram(Work *w);
};

// 中午工作状态
class NoonState : public State
{
public:
    virtual void writeProgram(Work *w);
};

// 下午工作状态
class AfternoonState : public State
{
public:
    virtual void writeProgram(Work *w);
};

// 晚间工作状态
class EveningState : public State
{
public:
    virtual void writeProgram(Work *w);
};

// 睡眠状态
class SleepingState : public State
{
public:
    virtual void writeProgram(Work *w);
};

// 下班休息状态
class RestState : public State
{
public:
    virtual void writeProgram(Work *w);
};

class Work
{
public:
    Work()
    {
        _curState = new ForenoonState();
        _taskFinished = false;
    }

    ~Work()
    {
        CC_SAFE_DELETE(_curState);
    }
    inline void setHour(int h){ _hour = h; }
    inline int getHour(){ return _hour; }
    inline bool getTaskFinished(){ return _taskFinished; }
    void setTaskFinished(bool finished = true){ _taskFinished = finished; }

    void changeState(State *s)
    {
        if (_curState != s)
        {
            CC_SAFE_DELETE(_curState);
            _curState = s;
        }
    }

    void writeProgram()
    {
        _curState->writeProgram(this);
    }

    
private:
    int _hour;    //时间
    State *_curState;  //工作状态
    bool _taskFinished;
};


void ForenoonState::writeProgram(Work *w)
{
    if (w->getHour() < 12)
    {
        cout << "当前时间: " << w->getHour() << "点 上午工作,精神百倍" << endl;
    }
    else
    {
        w->changeState(new NoonState());
        w->writeProgram();
    }
}

void NoonState::writeProgram(Work *w)
{
    if (w->getHour() < 13)
    {
        cout << "当前时间: " << w->getHour() << "点 午休时间" << endl;
    }
    else
    {
        w->changeState(new AfternoonState());
        w->writeProgram();
    }
}

void AfternoonState::writeProgram(Work *w)
{
    if (w->getHour() < 18)
    {
        cout << "当前时间: " << w->getHour() << "点 下午状态不错, 努力工作" << endl;
    }
    else
    {
        w->changeState(new EveningState());
        w->writeProgram();
    }
}

void EveningState::writeProgram(Work *w)
{
    if (w->getTaskFinished())
    {
        w->changeState(new RestState()); // 下班
        w->writeProgram();
    }
    else
    {
        if (w->getHour() < 22)
        {
            cout << "当前时间: " << w->getHour() << "点 加班哦, 疲惫至极" << endl;
        }
        else
        {
            w->changeState(new SleepingState());
            w->writeProgram();
        }
    }
}

void RestState::writeProgram(Work *w)
{
    cout << "当前时间是: " << w->getHour() << "点 下班回家了" << endl;
}

void SleepingState::writeProgram(Work *w)
{
    cout << "当前时间是: " << w->getHour() << "点, 不行了,睡觉了" << endl;
}

void test()
{
    Work *w = new Work();
    w->setHour(9);
    w->writeProgram();
    w->setHour(10);
    w->writeProgram();
    w->setHour(12);
    w->writeProgram();
    w->setHour(14);
    w->writeProgram();
    w->setHour(19);
    w->writeProgram();
    //w->setTaskFinished();
    w->setHour(20);
    w->writeProgram();
    w->setHour(23);
    w->writeProgram();
}

int main()
{
    test();
    cin.get();
    return 0;
}
View Code

 

效果:

技术分享

 

状态模式