State 模式

State模式中我们将状态逻辑和动作实现进行分离。允许一个对象在其内部状态改变时改变它的行为,对象看起来似乎修改了它的类;在一个状态即将结束的时候启用下一个状态。

 

 1 /////////state.h/////////////////////////////////////////////////////////////////
 2 #pragma  once 
 3 class Context ;
 4 class State
 5 {
 6 public:
 7     virtual ~State();
 8     State();
 9     virtual void OperationInterface(Context* ) = 0 ;
10     virtual void OperationChangeState(Context* ) = 0 ;
11     
12 protected:
13     bool ChangeState(Context* con , State* st);
14 private:
15 };
16 
17 class  ConcreteStateA : public State
18 {
19 public:
20     ~ConcreteStateA();
21     ConcreteStateA();
22     void OperationInterface(Context* );
23     void OperationChangeState(Context* );
24 protected:
25 private:
26 };
27 
28 class ConcreteStateB : public State
29 {
30 public:
31     ~ConcreteStateB();
32     ConcreteStateB();
33     void OperationInterface(Context* );
34     void OperationChangeState(Context* );
35 protected:
36 private:
37 };
 1 ////////state.cpp//////////////////////////////////////////////////////////////////
 2 #include "state.h"
 3 #include "Context.h"
 4 #include <iostream>
 5 using namespace std;
 6 State::~State()
 7 {
 8 
 9 }
10 State::State()
11 {
12 
13 }
14 bool State::ChangeState(Context* con , State* st)
15 {
16     con->ChangeState(st);
17     return true ;
18 }
19 
20 ConcreteStateA::ConcreteStateA()
21 {
22 
23 }
24 ConcreteStateA::~ConcreteStateA()
25 {
26 
27 }
28 void ConcreteStateA::OperationInterface(Context* con)
29 {
30     cout<<"ConcreteStateA::OperationInterface......"<<endl; 
31 }
32 
33 void ConcreteStateA::OperationChangeState(Context* con)
34 {
35     OperationInterface(con);
36     this->ChangeState(con , new ConcreteStateB());
37 }
38 
39 ConcreteStateB::~ConcreteStateB()
40 {
41 
42 }
43 ConcreteStateB::ConcreteStateB()
44 {
45 
46 }
47 void ConcreteStateB::OperationInterface(Context* con)
48 {
49     cout<<"ConcreteStateB::OperationInterface......"<<endl; 
50 }
51 
52 void ConcreteStateB::OperationChangeState(Context* con)
53 {
54     OperationInterface(con);
55     this->ChangeState(con , new ConcreteStateA() );
56 }
 1 //////////////////Context.h////////////////////////////////////////////////////////
 2 #pragma once
 3 class State;
 4 class Context
 5 {
 6 public:
 7     ~Context();
 8     Context();
 9     Context(State* st);
10     void OprationInterface(); 
11     void OperationChangState(); 
12 protected:
13 private:
14     friend class State ;
15     bool ChangeState(State* st);
16     State* _st ;
17 };
 1 /////////Context.cpp/////////////////////////////////////////////////////////////////
 2 #include "Context.h"
 3 #include "state.h"
 4 Context::Context()
 5 {
 6 
 7 }
 8 Context::Context(State* st)
 9 {
10     _st = st ;
11 }
12 
13 bool Context::ChangeState(State* st)
14 {
15     _st = st ;
16     return true ;
17 }
18 Context::~Context()
19 {
20     delete _st ;
21 }
22 
23 void Context::OprationInterface()
24 {
25     _st->OperationChangeState(this) ;//主要此处,调用时改变为下一个状态。
26 }
27 
28 void Context::OperationChangState()
29 {
30     _st->OperationChangeState(this);
31 }
 1 ////////main.cpp//////////////////////////////////////////////////////////////////
 2 #include "Context.h"
 3 #include "state.h"
 4 #include <iostream>
 5 using namespace std;
 6 int main()
 7 {
 8     State* st = new ConcreteStateA();
 9     Context* con = new Context(st);
10     con->OprationInterface();
11     con->OprationInterface();
12     con->OprationInterface();
13     if (con)
14     {
15         delete con ;
16     }
17     if (st)
18     {
19         delete st ;
20     }
21     getchar();
22     return 0 ;
23 }

State 模式在实现中,有两个关键点:
1)将 State 声明为 Context 的友元类(friend class) ,其作用是让 State 模式访问 Context 的 protected接口 ChangeSate() 。

2)State 及其子类中的操作都将 Context*传入作为参数,其主要目的是 State 类可以通过这个指针调用 Context中的方法 (在本示例代码中没有体现)。这也是 State 模式和 Strategy 模式的最大区别所在。

 

 

posted @ 2014-05-25 16:36  月轩  阅读(315)  评论(0编辑  收藏  举报