Re: [请教] 请教strategy、state pattern in C++

楼主: iamstudent (stu)   2011-04-23 20:17:56
我好像想出解法了
利用inner class来办到
不过还没有测试验证
//原本的大class
class BigClass
{
private:
StateInterface::Inner _data;
StateInterface* _state;
public:
//设定状态
inline void SetState(StateInterface* state)
{
_state = state;
_state->_inner = &_data;
}
//会变化功能的函数
inline void DoSomething()
{
_state->DoSomething();
}
}
//state 的共通接口
class StateInterface
{
protected:
friend BigClass
//原本的Data放这边
class Inner
{
public:
...
}
Inner* _inner;
public:
//做事情的纯虚拟函数
virtual void DoSomething()=0;
}
//其他继承state的类别模式
class StateInitial : public StateInterface
{
public:
//实做
virtual void DoSomething();
}

Links booklink

Contact Us: admin [ a t ] ucptt.com