简体   繁体   中英

Is an application-wide Observer a valid solution?

I'm using the Reactive framework in C# and I am curious if using an application wide observer (Singleton Message Bus) is a worthy cause? In a quarantined section of the application it works like a charm but I am curious if anyone has any wisdom that screams, "NO! WTF ARE YOU THINKING!"

Technical input as well as theoretical Pattern knowledge is more than welcome!

I used an approach like this in one of my old C/C++ applications. It has the advantage that you can decouple modules, but in my experience it quickly leads to lots of problems in bigger applications:

  • You lose control of what's really happening in your application. Since every module can listen to the message bus, also every module can influence every other module of the application.
  • If the message bus is the only way to notify observers, sending out notifications can have unpredictable performance effects.
  • To solve some of the performance problems, you can try to initiate buffering messages (messages like "I will do a lot of things, so don't react immediately" and "OK, I'm done now, you may process everything I told you") but this may lead to unpredictable effects as well.

My conclusion is that a global message bus is a nice idea for small applications, but not for big ones. By using one global message bus to decouple all your modules, you effectively achieve the opposite, since everyone is now linked to everyone else, making it harder in the long term to untangle your application again.

I would suggest to make every module as independent as possible, and to use specialized observers (see observer pattern of the Gang Of Four).

This pattern is widely used to decrease tie-up between app components. I've used it in a few of my projects and it's really very cosy and useful. I'm talking about pattern in general, I don't know how is it implemented in Reactive framework.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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