简体   繁体   English

多个事件的侦听器设计

[英]Design of listener for multiple events

I'm familiar with standard listeners, esp. 我熟悉标准的监听器,尤其是。 in Java. 在Java中。 For example, if you have a collection of objects, it might support a set of listeners for different things: CreateListener, ChangeListener, DeleteListener. 例如,如果您有一个对象集合,则它可能支持一组用于不同事物的侦听器:CreateListener,ChangeListener和DeleteListener。 Each has one method (eg objectChange) that is passed a list of affected objects. 每个方法都有一个方法(例如objectChange),该方法传递了一系列受影响的对象。 An app using this collection could register interest in one or more of these by implementing and register listener(s). 使用此集合的应用可以通过实现和注册侦听器来注册对其中一个或多个的兴趣。 When things happen to the objects in the collection, the appropriate listener gets invoked. 当集合中的对象发生问题时,将调用适当的侦听器。

But what if there are a number of these event types, perhaps similar but somewhat different. 但是,如果有许多此类事件类型,也许类似但有所不同,该怎么办。 Would it make sense instead to define one listener class that has many methods. 相反,定义一个具有许多方法的侦听器类是否有意义? For example: 例如:

class EventListener
{
    void objectsCreatedA( Object[] newObjects );
    void objectsCreatedB( Object[] newObjects );
    void objectsCreatedC( Object[] newObjects );

    void objectsChangedA( Object[] newObjects );
    void objectsChangedB( Object[] newObjects );
    void objectsChangedC( Object[] newObjects );

    void objectsDeletedA( Object[] newObjects );
    void objectsDeletedB( Object[] newObjects );
    void objectsDeletedC( Object[] newObjects );
}

This seems to make it easier for an app that wants to register for many of these events - they implement many methods in one class, rather than defining many classes that each implements only one method. 对于想要注册许多此类事件的应用程序来说,这似乎更容易-它们在一个类中实现了许多方法,而不是定义了每个仅实现一个方法的许多类。 Any drawbacks or other suggestions? 有任何缺点或其他建议吗?

Clarification edit: (sorry I got distracted with the holiday, not sure if this should be a separate post, but seems to make sense to follow-up on this one) 说明文字编辑:(对不起,我因假期而分心,不确定这是否应另作文章,但跟进此事似乎很有意义)

I should have specified that this code will be implemented in multiple languages, as a framework that client apps will make use of. 我应该指定此代码将以多种语言实现,作为客户端应用程序将使用的框架。 For C++, implementing multiple interfaces is difficult. 对于C ++,实现多个接口很困难。

A superset abstract listener could provide default do-nothing implementations for each method, so that a client extending it would only have to override the ones they care about. 一个超集抽象侦听器可以为每个方法提供默认的虚无实现,因此扩展它的客户端将仅需覆盖他们关心的方法。 With this approach, we could later decide to add additional methods to the abstract class and existing clients would be ok (and can override those new methods if/when they choose to). 使用这种方法,我们以后可以决定向抽象类中添加其他方法,而现有的客户端将是可以的(如果愿意,可以覆盖这些新方法)。 And only one registration is needed with one invocation, rather than the alternative of many registration methods (with the app invoking one or many). 一次调用仅需要一次注册,而不是许多注册方法的替代方法(应用程序调用一个或多个)。

With this clarification, does a superset abstract class make more sense than individual ones? 通过这种澄清,超集抽象类是否比单个抽象类更有意义?

I would group that functions into an interface only if they are semantically similar (cohesion) and if it is very likely that an object interested in the event "objectsCreatedA" is also interested in "objectsCreatedB", otherwise you'll end with some classes with unimplemented functions: pretty ugly. 仅当功能在语义上相似(内聚)并且对事件“ objectsCreatedA”感兴趣的对象也对“ objectsCreatedB”感兴趣的情况下,我才将该功能归类为一个接口。否则,将以未实现的功能:非常难看。

As an alternative, if the events are really so similar, you could pass a "type" parameter, so the receiver can factorize out some code and choose what event type to process. 或者,如果事件真的如此相似,则可以传递“ type”参数,以便接收器可以分解出一些代码并选择要处理的事件类型。

Since classes can implement multiple interfaces, I don't see that this saves you anything other than declaring that a single class implements a series of different listeners. 由于类可以实现多个接口,因此除了声明单个类实现一系列不同的侦听器之外,我看不到这为您节省了其他任何事情。 It may, on the other hand, force classes to implement methods they don't actually need to implement. 另一方面,它可能会迫使类实现他们实际上不需要实现的方法。

The only drawback I see to grouping all of those into one class is that the class could end up becoming fairly large and hard to read. 我看到将所有这些分组到一个类中的唯一缺点是该类最终可能变得相当大且难以阅读。 That being said, if the listeners are not at all similar, you could end up with a massive class which a bunch of random functionality, which would have pretty high coupling. 话虽如此,如果侦听器根本不相似,那么您可能会得到一个庞大的类,该类具有一堆随机功能,具有很高的耦合度。

If you do end up going this route, make sure that this listener only exists once (Singleton, static, whatever). 如果您确实走了这条路线,请确保此侦听器仅存在一次(Singleton,静态或其他)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM