This is the second post in a series about design patterns. I decided to make a dating profile for each pattern as a way to remember the various patterns, because I’m always mixing them up. This series is not meant to be a deep dive into the patterns and how to implement them (there are many posts on that topic already) but is meant to provide a high level understanding.
The Observer pattern has always tripped me up. It is one of the easier patterns to explain using real world examples, but I rarely implement it in practice so often forget how its composed. The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state (the subject), all of its dependents (the observers) are notified and can then update accordingly. Common examples of the Observer pattern include, social media post notifications, package tracking, and weather reports.
Being the boss
With the Observer pattern, the subject likes to think its in charge. If something changes, the subject will notify all of the observers of the change, and the observers are responsible for listening for changes. But the subject is not responsible for doing anything in response to the change. The subjects are the ones doing the heavy lifting and making updates.
The Observer pattern would definitely have one of those profiles that makes it a point to say that they’re an “entrepreneur” but is purposefully vague about what that means and what they actually do. They “delegate."
Over-Tinderer
With the Observer pattern, the subject notifies all its observers all the time. The subject is not responsible for being discerning and only notifying observers that actually care about the changes. If an observer doesn’t need to react to some of the subject’s changes, then the observer itself will not take any action.
In the online dating world, the Observer pattern is the stereotypical over-Tinderer. It would swipe right on everything and send the always meaningful ‘Hey’ over and over again to anyone they’ve matched with. The Observer pattern is not interested in having their matches reach out to them, they want to initiate the conversations but don’t put very much effort into it.
Not exclusive
In this pattern, the subject can have as many observers as necessary and has no issue having more observers added to the list or removed from the list. Since the subject is always notifying all observers of the same thing at the same time, it really doesn’t have any commitment to any of them.
The Observer pattern would not be interested in exclusivity. Though it enjoys the benefits of a tightly coupled relationship, it wants to be able to have that relationship with multiple people at a time.