我有几个专门的类。例如容器模式与专门的儿童
class Transition_Matrix : public Transition
{
SetMatrix(Matrix* pStartMatrix);
};
class Transition_Vector : public Transition
{
SetVector(Vector* pStartVector);
}
class Transition_Container : public Transition
{
}
我想在Animate_Container做呼叫SetVector()或SetMatrix(),而不声明功能需要被设定对象的每种类型。例如,我不想申报Animate_Container如下...
class Transition_Container : public Transition
{
SetMatrix(Matrix* pStartMatrix);//loops through all children calling SetMatrix
SetVector(Vector* pStartVector);//loops through all children calling SetVector
}
我不想Animate_Container知道它有哪些儿童。但我希望在容器上调用这些函数的方便性,所以我不必通过孩子搜索并找出在“过渡”矩阵或向量时应调用哪些函数。
我应该在这里使用什么正确的模式?
基本上我想在根容器上设置一个矩阵或向量,并让它传播给每个可能想要使用它的孩子。
想法?
是的,这是一个很好的答案,这是我以前实施它的方式。但是,我想知道是否有办法做到这一点,以便Transition类不需要了解Matrix或Vector。 –
_你为什么不想'过渡'知道'矩阵'和'矢量'? –
@DannyDiaz我已经更新了我的答案,包括一个使用'boost :: any'的例子 –