Click the Subscribe Button to Observe
You’re 10 years old, and you can’t get enough of your favorite YouTuber. You never want to miss a video, and look forward with avid intensity to each new drop. But since, gasp, your mom won’t let you be on your phone all the time, how will you know when a new video is posted? Subscribe, of course!
In Java, objects sometimes need to be alerted when something they contain or are connected to is modified. The Observer design pattern is the framework we can use to keep track of changes on our objects. In Bootcamp, we’re using the robust Spring framework as our Observer on the Text Action Game (TAG) project.
The behavior protocol for Observer described in the Gang of Four’s famous book on Design Patterns states: define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.
Subscibe Now!
So, how are the dependents notified and updated about the change? Like the avid YouTube fan, dependants get notifications from their subscription. This subscription allows all the objects affected by the change to get update information automatically every time something is changed in the object to which the dependants are subscribed.
Structure
I found this flowchart from Refactoring Guru very helpful in picturing the different interactions between elements of the Observer pattern.
In it, you can see the Observer pattern described in 6 parts:
Name | Role in the Observer Pattern | |
---|---|---|
Publisher | Sends events to other objects. Events happen when the publisher is changed or does something. | |
Subscription List | When a new event happens, the publisher uses a notification method to notify all subscribers on the list | |
Subscriber | The subscriber implements the notification interface, so it can get all the event details every time there is an update | |
Concrete Subscribers | These special subscribers do someting in response to getting a notification | |
Updates | Contain special context information so that the subscibers know what to do with the update | |
Clients | Creates the publisher and subscriber objects and signs the subscribers up for updates |
For a more expert and in depth description of the Observer pattern, checkout this excellent post from Refactoring Guru. It describes the Observer pattern in easy to understand language and includes some more great infographics and illustrations to help make envisioning the Observer pattern easy.
Click here for a table of Design Patterns that inclues some class notes on the topic.