作者iamstudent (stu)
看板OOAD
标题Re: [请教] 请教strategy、state pattern in C++
时间Sat Apr 23 20:17:56 2011
我好像想出解法了
利用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();
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.113.210.62
※ 编辑: iamstudent 来自: 140.113.210.62 (04/23 20:19)
※ 编辑: iamstudent 来自: 140.113.210.62 (04/23 20:25)
※ 编辑: iamstudent 来自: 140.113.210.62 (04/23 20:31)