# Observer Design Pattern

In this pattern, we call the publisher the **SUBJECT** and the subscribers the **OBSERVER**

{% hint style="success" %}
**Strive for loosely coupled designs between objects that interact**
{% endhint %}

Loosely couples designs allow us to build a flexible OO system that can handle change because they minimize the interdependency between objects.

## When to Use?

When you need many other objects to receive the update when another object changes

## Pros ( :thumbsup: ) & Cons ( :thumbsdown: )

:thumbsup: Loose coupling - Subject ( Publisher ) doesn't need to know about the Observer ( Subscriber ).

:thumbsup: You can establish relations between objects at runtime.

:thumbsup: Changes to either the subject or an observer will not affect the other.

:thumbsdown: Subject ( Publisher ) may send the updates that don't matter to the observer ( Subscriber ).

## Code

{% embed url="<https://gist.github.com/SunilGudivada/00c2205814b7e4682a924887baf67bae>" %}

{% hint style="info" %}
With **Java's** built-in support all you have to extend observable and tell it when to notify the observers. The API does the rest for you. Check it out `java.util.Observer, java.util.Observable` but this is deprecated from java 9.\
\
The event model supported by Observer and Observable is quite limited, the order of notifications delivered by Observable is unspecified, and state changes are not in one-for-one correspondence with notifications. For a richer event model, consider using the java.beans package. For reliable and ordered messaging among threads, consider using one of the concurrent data structures in the java.util.concurrent package.
{% endhint %}

## Reference

* <https://www.newthinktank.com/2012/08/observer-design-pattern-tutorial/>
* <https://refactoring.guru/design-patterns/observer>
* <https://sourcemaking.com/design_patterns/observer>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.sunilgudivada.dev/notebook/design-patterns/behavioral-design-patterns/observer-design-pattern.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
