设计模式观后(c++还原之二十三 状态模式)

//状态模式
//首先作者举例是电梯的升降和操作(上升状态的关门、开门、运动等动作判断;下降状态的动作判断)
//我第一次想到的是:用四个枚举来表示当前电梯状态,然后动作函数的执行用switch(状态) case(运行)来写。
//来看作者的写法吧
class Context;//后面的上下文类
class OpenningState;//开门类
class ClosingState;//关门类
class RunningState;//运行状态类
class StoppingState;//停止类

//抽象电梯类
class LiftState {
protected:
    Context* m_pContext;
public:
    virtual void SetContext(Context* _context) {
        m_pContext = _context;
    }
    virtual void Open(){}//电梯打开
    virtual void Close(){}//电梯关闭
    virtual void Run(){}//电梯运行
    virtual void Stop(){}//电梯停
};

class Context {
public:
    //定义所以电梯状态
    static OpenningState* m_pOpenningState;
    static ClosingState* m_pClosingState;
    static RunningState* m_pRunningState;
    static StoppingState* m_pStoppingState;
private:
    //定义当前电梯状态
    LiftState* m_pLiftState;
public:
    LiftState* GetLiftState() {
        return m_pLiftState;
    }
    void SetLiftState(LiftState* _liftstate) {
        m_pLiftState = _liftstate;
        //把当前环境通知到各个实现类中
        m_pLiftState->SetContext(this);
    }
    void Open() {
        m_pLiftState->Open();
    }
    void Close() {
        m_pLiftState->Close();
    }
    void Run() {
        m_pLiftState->Run();
    }
    void Stop() {
        m_pLiftState->Stop();
    }
};
//开门状态(只能做关门和开门操作)
class OpenningState : public LiftState {
public:
    void Close() {
        m_pContext->SetLiftState(m_pContext->m_pClosingState);
        m_pContext->GetLiftState()->Close();
    }
    void Open() {
        cout << "电梯开启" << endl;
    }
    void Run() {
        //nothing
    }
    void Stop() {
        //nothing
    }
};
//关门状态(只能关门、开门、运行、停止)
class ClosingState : public LiftState {
public:
    void Close() {
        cout << "关门";
    }
    void Open() {
        m_pContext->SetLiftState(m_pContext->m_pOpenningState);
        m_pContext->GetLiftState()->Open();
    }
    void Run() {
        m_pContext->SetLiftState(m_pContext->m_pRunningState);
        m_pContext->GetLiftState()->Run();
    }
};
//运行状态
class RunningState: public LiftState {
public:
    void Close() {
        //nothing
    }
    void Open() {
        //nothing
    }
    void Run() {
        cout << "正在运行" <SetLiftState(m_pContext->m_pStoppingState);
        m_pContext->GetLiftState()->Stop();
    }
};
//停止状态
class StoppingState: public LiftState {
public:
    void Close() {
        //nothing;
    }
    void Open() {
        m_pContext->SetLiftState(m_pContext->m_pOpenningState);
        m_pContext->GetLiftState()->Open();
    }
    void Run() {
        m_pContext->SetLiftState(m_pContext->m_pRunningState);
        m_pContext->GetLiftState()->Run();
    }
    void Stop() {
        cout << "电梯停止"<< endl;
    }
};
class Client {
public:
    static void main() {
        Context* p_context = new Context;
        p_context->SetLiftState(new ClosingState);
        p_context->Open();
        p_context->Close();
        p_context->Run();
        p_context->Stop();
    }
};
//为了简便,省略了些代码,所以是无法运行的,主要是思想。
//作者基本是角色和环境构成
//环境操作角色,角色设置环境
//作者代码亮点在”相互包含,相互设置“,结构好了,但耦合性高了

你可能感兴趣的:(生活)