开发者

Pattern needed to emulate subscription service

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?


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜