开发者

Asynchronous Observer Pattern

I wanted 开发者_C百科to find out other ways to do Asynchronous Observer Pattern without using Message Queue. Ideas and examples are mostly welcomed. :-) (Think of this as a brainstorming session).

PS Language preference is up to you.


In Java, you could submit notification tasks to an Executor which manages a single thread of group of threads that are all responsible just for handling observable events. You still effectively have a queue, but instead of a "message queue" in the traditional sense it's a queue of notification tasks.


The obvious and most simple approach would be to start a new thread in the Subject for each one of the Observers registered to the Subject, and let these threads to run each one of the Observers' "update()" method.

A more sophisticated approach would be to use a middleware class as a bus to let the subject "send events" to it. This middleware class would use several threads to invoke all the "registered" Observers, but this starts to diverge from the classic Observer Pattern. Anyway, with this approach you prevent the Subject from dealing with threads.

This approach would very flexible to scale to different "kinds of events", with many Subjects and many Observers registering to the middleware class for arbitrary kinds of events.


In .NET you can use the Reactive Framework.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜