Observer (dependents/publish/subscribe)

The Observer pattern suggests that when one object changes the state, it notifies its dependents and updates automatically. When implementation is in need of one-to-many dependencies, you would want to use this pattern.

The preceding diagram depicts Observer pattern structure and a sample implementation of the same for a publications app; whenever an event occurs, subscribers need to be informed. The subscribers have a different mode of publishing (SMS, print, and emailing) and may need to support new modes as well in the future, so the best fit is Observer, as we just saw.

Let's go through its benefits:

  • Enables easy broadcast of communication
  • Supports loose coupling between objects as it's capable of sending data to other objects without any change in the subject
  • Abstract coupling between subject and observer (changes in the observer do not impact subject)
  • Can add or remove Observers any time

Impacts are as follows:

  • Accidental or unintended updates impact the system heavily as it cascades to the observer down the layers
  • May lead to performance issues
  • Independent notifications may result in inconsistent state or behavior (no handshakes)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset