posts - 11,comments - 13,trackbacks - 0

冒似该模式不能彻底解决switch、case的困扰。
源码如下:

 1#ifndef __CONTENT_H__
 2#define __CONTENT_H__
 3#include "State.h"
 4
 5class Context
 6{
 7public:
 8    Context(State * state)
 9    {
10        _state = state;
11    }

12public:
13    ~Context()
14    {
15
16    }

17    void operationinterface()
18    {
19        _state->OperationInterface(this);
20    }

21    void operationchangestate(State * state)
22    {
23        _state->OperationChangeState(this,state);
24    }

25private:
26    friend class State;
27    State* _state;
28    void ChangeState(State * state)
29    {
30        _state = state;
31        return;
32    }

33}
;
34
35#endif
 1#ifndef _STATE_H_ 
 2#define _STATE_H_
 3
 4class Context; //前置声明
 5
 6class State 
 7public:
 8    State()
 9        {
10
11        }

12virtual ~State()
13{
14
15}

16virtual void OperationInterface(Context* ) = 0;
17virtual void OperationChangeState(Context*,State *= 0;
18protectedbool ChangeState(Context* con,State* st);
19}
;
20
21class State1:public State
22{
23public:
24    State1()
25    {
26
27    }

28    virtual ~State1()
29    {
30
31    }

32    void OperationInterface(Context* );
33    void OperationChangeState(Context *,State * );
34}
;
35
36class State2:public State
37{
38public:
39    State2()
40    {
41
42    }

43    virtual ~State2()
44    {
45
46    }

47    void OperationInterface(Context* );
48    void OperationChangeState(Context *,State * );
49}
;
50#endif
 1#include "State.h"
 2#include <iostream>
 3#include "content.h"
 4bool State::ChangeState(Context* con,State* st)
 5{
 6    con->ChangeState(st);
 7    return true;
 8}

 9
10void State1::OperationInterface(Context* )
11{
12    std::cout<<"State1 OperationInterface"<<std::endl;
13}

14
15void State1::OperationChangeState(Context * con,State * st)
16{
17    this->ChangeState(con,st);
18}

19
20void State2::OperationInterface(Context* )
21{
22    std::cout<<"State2 OperationInterface"<<std::endl;
23}

24
25void State2::OperationChangeState(Context * con,State * st)
26{
27    this->ChangeState(con,st);
28}
 1#include "content.h"
 2#include "State.h"
 3#include <iostream>
 4
 5int main(int argc,char * argv[])
 6{
 7    State * st = new State1();
 8    Context * con  = new Context(st);
 9    con->operationinterface();
10
11    State * st2 = new State2();
12    Context * con1  = new Context(st2);
13    con1->operationinterface();
14}
该模式必须知道当前需要用哪种state进行操作;

#define NEWSTATE(x) new State##x;
这样必须先定义State1,State2等;
问题出现了,如果现在有个值value = 2
能不能有方法自动定义到 new State2;
posted on 2009-06-18 15:36 Super- 阅读(1324) 评论(2)  编辑 收藏 引用

FeedBack:
# re: State设计模式
2009-06-18 23:51 | Super-
#include <vector>
#include <iostream>
#include <algorithm>


typedef int (*MsgProc)();
typedef struct _SWITCH_CASE_
{
int m_value;
MsgProc m_proc;
_SWITCH_CASE_(int value,MsgProc proc)
{
m_value = value;
m_proc = proc;
}
_SWITCH_CASE_(int a)
{
m_value = a;
}

bool operator == (const int value)
{
return (value == m_value);
}

}SWITCHCASE;


std::vector<SWITCHCASE> g_vec;

int DealError()
{
std::cout<<"Deal Error!"<<std::endl;
return 1;
}

int DealSuccess()
{
std::cout<<"Deal Success!"<<std::endl;
return 0;
}



int main(int argc,char * argv[])
{
SWITCHCASE a(0x01,DealError);
SWITCHCASE b(0x00,DealSuccess);
g_vec.push_back(a);
g_vec.push_back(b);
while(1)
{
int a;
std::cin>>a;
int b = 0;

std::vector<SWITCHCASE>::iterator iter = find(g_vec.begin(),g_vec.end(),a);
if(iter != g_vec.end())
{
(*iter).m_proc();
}
else
{
break;
}
}

}  回复  更多评论
  
# re: State设计模式
2009-06-19 10:09 | 远古毛利人
我觉得要根据变量产成一个新的state类的机会不多,因为状态模式里的所有状态都应该是已知的(否则怎么画出状态图?)。变量的作用最多只是帮助我们选择哪一个状态而已(如果变量的值没有哪个已有状态与之对应,可以转到出错状态)。  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理