This Newsletter is a good example of how the pattern works.
A publisher (subject) and its subscribers is a good way to visualize this pattern.
The Observer Pattern defines a one-to-many relationship between a set of objects. When the state of one object changes, all of its dependents are notified.
The real power of the observer lies in loosely coupled components. They can interact but typically have very little knowledge of each other.
The subject knows only that an observer implements a certain interface (the Observer interface).
It doesn't need to know the observer's concrete class, what it does, or anything else.
We can add new observers anytime because the subject depends only on a list of objects that implement the Observer interface.
We can replace any observer at runtime with another observer, and the subject will keep sending notifications.
We can also remove observers at any time.
We never need to change the subject to add new types of observers.
Let's say we have a new concrete class that needs to be an observer. We don't need to change the subject to accommodate the new class type.
The subject doesn't care; it will notify any object that implements the Observer interface.
Changes to either the subject or an observer will not affect the other.
Loosely coupled designs allow you to build flexible systems because they cut the interdependency between objects.
Stay decoupled and Subscribed!
Awesome post brother, keep them coming!