一个类中的Fire事件从另一个类订阅

时间:2013-01-15 07:16:16

标签: c++ algorithm events

我有一个类AwesomeMousePointer,它有一些功能可以开始在鼠标上播放动画:

class AwesomeMousePointer{
void startAnimat();
void stopAnimat();
};

我还有另一个对象,我负责确定鼠标动画是否应该启动(这是基于对象的内部命中测试,例如:如果鼠标位于对象内部具体时间)

class SomeShape(){
Event<MouseArgs> startAnim
Event<bool> interrutptAnim
bool hitTest(int x, int y);

//Inside some loop function, check if the mouse is inside the object
if(hitTest(mouseXPos, mouseYPos)){
//if the mouse if inside for x time
NotiftyEvent(startAnim, MouseArgs);
}
else{
//mouse left the object
NotifyEvent(interruptAnim, false);
}

现在,再次在我的AwesomeMousePointer内,我将添加事件的监听器,即

AddListener(SomeShape::startAnim, &AwesomeMousePointer::startAnim);
AddListener(SomeShape::interruptAnim, &AweseommousePointer::interruptAnim);

使用NotifyEventAddListener的单一事件系统在我尝试的简短不同示例中正常工作。现在在我的应用程序中,我有很多SomeShape和一个AwesomeMousePointer的对象。我的问题是,上述动画的逻辑是否有效,或者我是否应该明确地明确传递SomeShape对象以订阅他们的事件,事情会变得有点困难。

例如:

AddListener(shapeObject1.startAnim, &AwesomeMousePointer::startAnimat);
AddListener(shapeObject2.startAnim, &AwesomeMousePointer::startAnimat);
AddListener(shapeObject3.startAnim, &AwesomeMousePointer::startAnimat);

AddListener(SomeShape::startAnim, &AwesomeMousePointer::startAnimat);

上面的第二个会解决吗?如果没有,如果没有明确地传递对象将如何做到这一点,因为这使得不清楚,ShapeObjects不应该在MousePointer内。

如果我将SomeShape内的事件设为静态,那么这会有效吗?

static Event<MouseArgs> startAnim
static Event<bool> interrutptAnim

1 个答案:

答案 0 :(得分:0)

您说您不希望发件人和目标之间存在耦合。在这种情况下,使用Poco事件是不合适的。根据{{​​3}},您应该使用通知而不是事件,因为您的发件人和目标不需要彼此了解。