• Albert Bennett

Design Patterns: Observer

Updated: Jul 12, 2021

Here is the link to the GitHub repo :)

Feel free to like, comment and share this post.

This post is all about the out's and in's of the Observer pattern. What it's used for and how to implement it.

First of, it is used when you want to notify the observers of an object when, it's state has changed.

The diagram below outlines how this pattern works. As you can see from it observers hold a reference to a subject. And when the state of the subject changes the observers are notified about it.

For our example, we want to notify the people (observers) when the TV show (subject) has ended.

Subject (TvShow): This is the object that the observers are watching. In it observers are added and removed.

Observer (Person): These are the objects that are going to be notified when the state of the TV show has been changed.

As you can see from our example when the TV show ends the people get notified of the change.

I hope that this help you to understand the Observer pattern.

<< More Design Patterns

4 views0 comments

Recent Posts

See All