模拟订阅服务所需的模式
A 发布了 B、C 和 D 对象有兴趣了解的内容,而 E、F 和 G 则不关心。
当 A 说某事时,只有 B、C、D 应该收到此“消息”。
什么设计模式可以最好地模拟这一点?
** 在不使用 Tibco 的情况下如何使用 Tibco?
A publishes something B, C and D objects are interested in knowing, where as E, F and G don't care about it.
When A says something only B,C,D should get this "message".
What design pattern helps best emulate this?
**
How do i use Tibco, without using Tibco?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在正常的 J2SE 应用程序中,事件的发布者和事件的订阅者是同一进程空间中的对象,这就是观察者模式。
订阅者通常会通过调用特定方法(例如 addListener())并实现特定接口来通知发布者他们想要侦听事件。
因此,在您的示例中,假设我们创建一个接口 NumberEventListener,它有一个方法 onNumberEvent(int number)
您的侦听器都将实现 NumberEventListener 并在您的发布者对象上调用 addListener() 。
过滤可以应用于发布者端或侦听器端。因此,对于您的实现,您可以将 addListener() 方法设置为 addListener(NumberEventListenerlistener, int min, int max),并且仅当数字在 min 和 max 之间时才会调用您的侦听器。然而,这不是典型的实现,因为它使发布者代码更加复杂,并且您不能真正灵活。
在这样的示例中,侦听器通常会接收所有相应的事件并丢弃他们不感兴趣的事件。然而,这在一定程度上取决于实际的现实场景。
侦听器是“同时”还是按顺序收到通知取决于事件发布者是按顺序调用侦听器还是在单独的线程中调用侦听器。
In case of a normal J2SE application, where the publishers of events and subscribers to events are objects within the same process space, it would be the Observer pattern.
Subscribers would typically inform the publisher that they want to listen to events by calling a specific method such as addListener(), and implementing a specific interface.
So in your example lets say we create an interface NumberEventListener which has one method onNumberEvent(int number)
Your listeners would all implement the NumberEventListener and call addListener() on your publisher object.
Filtering can be applied on either the publisher side or the listener side. So for your implementation you could have your addListener() method be addListener(NumberEventListener listener, int min, int max) and your listener would only be invoked if the number is between min and max. However this is not a typical implementation because it makes the publisher code more complicated and you can't really be flexible.
In such an example, typically listeners receive all the respective events and discard the ones they're not interested in. It however depends a little on the actual real-life scenario.
Whether the listeners get notified 'at the same time' or sequentially depends on whether the listeners are invoked sequentially or in separate threads by the publisher of the event.
发布-订阅消息交换模式怎么样?
在企业集成模式中也称为发布/订阅通道:
http://www.eaipatterns.com/PublishSubscribeChannel.html
在Java中,JMS通常提供这样一个消息服务。
What about Publish - Subscribe message exchange pattern?
Also referred to as Publish / Subscribe Channel in Enterprise Integration Patterns:
http://www.eaipatterns.com/PublishSubscribeChannel.html
In Java, JMS typically provides such a messaging service.
观察者模式。
或者,可以应用过滤器链模式,正如您提到的某些对象根据消息的内容执行某些操作。对我来说,这听起来像是一个过滤器链。
Observer pattern.
Alternatively, the Filter chain pattern may apply, as you mention that some of the objects do something based on the contents of the message. It sounds like a filter chain to me.