首页 > 代码库 > State Pattern(状态模式)

State Pattern(状态模式)

  • 状态(State)模式:

  当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。

 

  • 状态模式的结构:

上下文环境(Context):

  它定义了客户程序需要的接口并维护一个具体状态角色的实例,将与状态相关的操作委托给当前的Concrete State对象来处理。

抽象状态(State):

  定义一个接口以封装使用上下文环境的的一个特定状态相关的行为。

具体状态(ConcreteState):

  实现抽象状态定义的接口。

 

 1 //State.h 2 # ifndef _STATE_H_ 3 # define _STATE_H_ 4  5 class Context; 6  7 class State 8 { 9 public:10         State();11         virtual ~State();12         virtual void OperationInterface(Context*);13         virtual void OperationChangeState(Context*);14 protected:15         bool ChangeState(Context*, State*);16 private:17 };18 19 class ConcreteStateA:public State20 {21 public:22         ConcreteStateA();23         virtual ~ConcreteStateA();24         virtual void OperationInterface(Context*);25         virtual void OperationChangeState(Context*);26 protected:27 private:28 };29 30 class ConcreteStateB:public State31 {32 public:33         ConcreteStateB();34         virtual ~ConcreteStateB();35         virtual void OperationInterface(Context*);36         virtual void OperationChangeState(Context*);37 protected:38 private:39 };40 41 # endif
State.h
 1 //State.cpp 2 # include <iostream> 3 # include "State.h" 4 # include "Context.h" 5 using namespace std; 6  7 State::State() 8 { 9         cout << "Construct State" << endl;10 }11 State::~State()12 {13         cout << "Destruct State" << endl;14 }15 void State::OperationInterface(Context* con)16 {17         cout << "State::OperationInterface" << endl;18 }19 void State::OperationChangeState(Context* con)20 {21         cout << "State::OperationChangeState" << endl;22 }23 bool State::ChangeState(Context* con, State* st)24 {25         con->ChangeState(st);26         return true;27 }28 29 ConcreteStateA::ConcreteStateA()30 {31         cout << "Construct ConcreteStateA" << endl;32 }33 ConcreteStateA::~ConcreteStateA()34 {35         cout << "Destruct ConcreteStateA" << endl;36 }37 void ConcreteStateA::OperationInterface(Context* con)38 {39         cout << "ConcreteStateA::OperationInterface" << endl;40 }41 void ConcreteStateA::OperationChangeState(Context* con)42 {43         OperationInterface(con);44         this->ChangeState(con, new ConcreteStateB());45 }46 47 ConcreteStateB::ConcreteStateB()48 {49         cout << "Construct ConcreteStateB" << endl;50 }51 ConcreteStateB::~ConcreteStateB()52 {53         cout << "Destruct ConcreteStateB" << endl;54 }55 void ConcreteStateB::OperationInterface(Context* con)56 {57         cout << "ConcreteStateB::OperationInterface" << endl;58 }59 void ConcreteStateB::OperationChangeState(Context* con)60 {61         OperationInterface(con);62         this->ChangeState(con, new ConcreteStateA());63 }
State.cpp
 1 //Context.h 2 # ifndef _CONTEXT_H_ 3 # define _CONTEXT_H_ 4  5 class State; 6 class Context 7 { 8 public: 9         Context();10         Context(State*);11         ~Context();12         void OperationInterface();13         void OperationChangeState();14 protected:15 private:16         friend class State;17         bool ChangeState(State*);18         State* p_state;19 };20 21 # endif
Context.h
 1 //Context.cpp 2 # include <iostream> 3 # include "Context.h" 4 # include "State.h" 5 using namespace std; 6  7 Context::Context() 8 { 9         cout << "Construct Context" << endl;10 }11 Context::Context(State* state)12 {13         this->p_state = state;14         cout << "Construct Context" << endl;15 }16 Context::~Context()17 {18         delete p_state;19         cout << "Destruct Context" << endl;20 }21 void Context::OperationInterface()22 {23         p_state->OperationInterface(this);24 }25 bool Context::ChangeState(State* state)26 {27         this->p_state = state;28         return true;29 }30 void Context::OperationChangeState()31 {32         p_state->OperationChangeState(this);33 }
Context.cpp
 1 //main.cpp 2 # include <iostream> 3 # include "State.h" 4 # include "Context.h" 5 using namespace std; 6  7 int main() 8 { 9         State* st = new ConcreteStateA();10         Context* con = new Context(st);11         con->OperationInterface();12         con->OperationInterface();13         if(con != NULL)14                 delete con;15         if(st != NULL)16                 st = NULL;17         return 0;18 }
main.cpp

result:

blank@linux-bai:~/Projects/State> g++ -o a *.cpp && ./a
Construct State
Construct ConcreteStateA
Construct Context
ConcreteStateA::OperationInterface
ConcreteStateA::OperationInterface
Destruct ConcreteStateA
Destruct State
Destruct Context