C++ State 设计模式

C++ State 设计模式

 

 

C++ State 设计模式

 

 

 设计模式看的太快不利于消化,一方面是最近比较忙,另一方面是想让自己多消化消化自己所看的东西。所以本周只看了一个Sate设计模式

 

 

State模式的意图有2点:

1. 分散逻辑判断和处理。

对于State来说,无非就是状态的切换。说白了最终结果和switch/case差不多。对于少量的状态判断和逻辑处理,switch还行。但是对于一个较大的项目而且条件判断比较复杂,这个时候switch不仅效率低下,而且无法控制。State模式巧妙的将逻辑判断和处理放到状态对象中,而不是把条件判断和状态切换放在Context上下文中,从而分散了逻辑判断和处理。如果要更改逻辑,Context就不需要改动,只改动State类即可。

2. 去耦。我想这应该是Gof 设计模式的核心。

 

 

类图如下:

 

                            C++ State 设计模式

 

State模式中,将条件判断和状态切换放在State的派生类中。这样降低了Context逻辑判断的复杂和依赖,降低了耦合度。

 

 

主要实现代码:

 

 

 //Context.h #pragma once class State; class Context { friend class State; public: Context(void); ~Context(void); public: void OperationInterfaceA(); void OperationInterfaceB(); protected: void ChangeState(State *pState);//状态切换 private: State *__m_pState;//维护一个State基类指针 };

 

 

 

//Context.cpp #include "Context.h" #include "State.h" #include "ConcreteStateA.h" Context::Context(void) { __m_pState = ConcreteStateA::SInstance(); } Context::~Context(void) { delete __m_pState; __m_pState; } void Context::OperationInterfaceA() { __m_pState->OperationInterfaceA(this); } void Context::OperationInterfaceB() { __m_pState->OperationInterfaceB(this); } void Context::ChangeState(State *pState) { __m_pState = pState; }

 

 

//State.h #pragma once class Context; class State { public: State(void); virtual ~State(void); public: virtual void OperationInterfaceA(Context *pCxt){} virtual void OperationInterfaceB(Context *pCxt){} virtual void Handle(); virtual void ChangeState(State *pSt, Context *pCxt); };

 

 

//State.cpp #include "State.h" #include "Context.h" State::State(void) { } State::~State(void) { } void State::Handle() { } void State::ChangeState(State *pSt, Context *pCxt) { pCxt->__m_pState = pSt; }

 

 

//ConcreteStateA.h #pragma once #include "state.h" class ConcreteStateA : public State { public: static ConcreteStateA *SInstance(); ~ConcreteStateA(void); public: void Handle(); void OperationInterfaceA(Context *pCxt); private: ConcreteStateA(void); static ConcreteStateA *__m_spInstance; };

 

 

//ConcreteStateA.cpp #include <iostream> #include "ConcreteStateA.h" #include "ConcreteStateB.h" using namespace std; ConcreteStateA* ConcreteStateA::__m_spInstance =NULL; ConcreteStateA::ConcreteStateA(void) { } ConcreteStateA::~ConcreteStateA(void) { if (__m_spInstance != NULL) { delete __m_spInstance; __m_spInstance = NULL; } } ConcreteStateA* ConcreteStateA::SInstance() { if (__m_spInstance == NULL) { __m_spInstance = new ConcreteStateA(); } return __m_spInstance; } void ConcreteStateA::Handle() { cout << "Hi, I am in the StateA" << endl; } void ConcreteStateA::OperationInterfaceA(Context *pCxt) { Handle(); ChangeState(ConcreteStateB::SInstance(), pCxt);//状态切换逻辑处理 }

 

 

 

//ConcreteStateB.h #pragma once #include "state.h" class ConcreteStateB : public State { public: static ConcreteStateB* SInstance(); ~ConcreteStateB(void); public: void Handle(); void OperationInterfaceB(Context *pCxt); private: ConcreteStateB(void); static ConcreteStateB *__m_sInstance; };

 

 

//ConcreteStateB.cpp #include <iostream> #include "ConcreteStateB.h" #include "ConcreteStateA.h" using namespace std; ConcreteStateB* ConcreteStateB::__m_sInstance = NULL; ConcreteStateB::ConcreteStateB(void) { } ConcreteStateB::~ConcreteStateB(void) { if (__m_sInstance != NULL) { delete __m_sInstance; __m_sInstance = NULL; } } ConcreteStateB* ConcreteStateB::SInstance() { if (__m_sInstance == NULL) { __m_sInstance = new ConcreteStateB(); } return __m_sInstance; } void ConcreteStateB::Handle() { cout << "Hi, I am in the StateB" << endl; } void ConcreteStateB::OperationInterfaceB(Context *pCxt) { Handle(); State::ChangeState(ConcreteStateA::SInstance(), pCxt); }

 

 

 

//main.cpp #include <iostream> #include "Context.h" #include "ConcreteStateA.h" #include "ConcreteStateB.h" using namespace std; int main(void) { Context txt; txt.OperationInterfaceA(); txt.OperationInterfaceB(); txt.OperationInterfaceA(); }

 

 

输出如下:

 

Hi, I am in the StateA

Hi, I am in the StateB

Hi, I am in the StateA

 

 

 

C++ State 设计模式

 

 

 

 

上一篇:带动态背景图的二维码生成器


下一篇:Unity3D游戏开发初探—1.跨平台的游戏引擎让.NET程序员新生