Skip to content

Latest commit

 

History

History
43 lines (19 loc) · 2.13 KB

Observer.md

File metadata and controls

43 lines (19 loc) · 2.13 KB

Observer Pattern

The "Observer" pattern, a behavioral design pattern commonly used for implementing distributed event handling systems.

1. Context

The Observer pattern is often found in distributed systems where components need to maintain consistency or react to state changes in other components. It's particularly useful in event-driven architectures and reactive systems.

2. Problem

How can one part of a system notify other parts about changes in its state without creating tight coupling? Directly calling methods on dependent components would create a web of dependencies, making the system harder to understand, maintain, and extend.

3. Solution

The Observer pattern proposes a "subject" and "observer" relationship. The "subject" maintains a list of "observers." Whenever the subject undergoes a change in state, it notifies all its observers. This decouples the classes and makes them easily extensible. Observers can be added or removed at runtime without altering the underlying code of the subject.

4. Benefits

  • Decoupling: Reduces the coupling between the subject and the observers, adhering to the Single Responsibility and Open/Closed SOLID design principles.

  • Dynamic Relationships: Provides the flexibility to add or remove observers at runtime.

  • Broadcasting: Easily notifies multiple components, which is otherwise cumbersome in a tightly coupled system.

5. Drawbacks

  • Memory Leaks: Observers must unregister themselves when they're no longer needed, or memory leaks could occur.

  • Ordering Issues: Notifications might not be sent in the order observers expect, causing issues in certain scenarios.

  • Over-notifying: Can lead to performance issues if observers are notified too often or for minor changes.

6. Alternative Solutions

  • Event Bus: A centralized event system can replace the observer pattern for more complex scenarios but introduces its own set of complexities and challenges.

  • Reactive Streams: Libraries like RxJS or Project Reactor offer a more robust, functional programming approach to dealing with asynchronous data streams but come with a learning curve.