by Sihui Huang

The Observer Design Pattern is kind of like a podcast

1*_lV71Wek7B9MUmsOjS76gQ

If you listen to podcasts, you are already familiar with the Observer pattern. In fact, you are an “observer”.

Here’s the definition for the Observer pattern:

The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

I found an interesting podcast named developer tea.

1*-lQ8wA_1gNxtwimzoqKfGA

After clicking the SUBSCRIBE button, I’m now on their subscriber list.

1*GBO65Rn7VDroo7R5sbkfKw

When developer tea releases a new episode, the app will notify me and other subscribers. It downloads the new episode for us.

1*_bOkx12Vqyr9b7zBlDUVVg

That’s exactly the definition of the Observer pattern!

The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

There is a one-to-many relationship between the developer tea podcast and subscribers.

When developer tea changes state, such as releasing a new episode, all of developer tea‘s subscribers are notified and updated.

Let’s implement it in Ruby.

Start with a simple version.

The Podcast class holds a list of episodes and has a method to add_episode to the list.

Then we can create the developer_tea podcast and add episode #1 to it like this:

1*79mOvT3PAVnOZKIzmkhAog

I want to get a notification whenever a new episode is released.

We can update me after adding a new episode to the list:

And whenever I get an update from developer_tea, I can go ahead and download the latest episode.

I enjoy listening to developer_tea so much that I recommend it to my friend, Amber. Now, Amber wants to subscribe to it as well.

1*PKqEa9I2eoOKJAs_ajZUxA

We need to make sure Amber also gets a notification whenever a new episode is released:

Hmmm, this code does what we want.

But there is a problem.

Each time we want to add a subscriber, we have to redefine the class.

Is there a way to update the subscriber list without having to redefine the class????

??We can keep a subscriber list!??

The new Podcast class keeps a subscriber list with the help of two new methods: one for adding subscribers and one for removing subscribers. When an episode is released, we update each subscriber.

1*xnCj4ij96f0MS6ioiOxk6w

Unfortunately, Amber doesn’t enjoy the podcast as much as I do and decides to unsubscribe. We use the remove_subscriber method to remove her from the subscriber list.

1*berwHCxfb4NDDv7XbcPjAw

??Yay! You just learned the Observer pattern!??

Design Principle behind the Observer pattern.

The Observer pattern utilizes the Loose Coupling design principle:

Strive for loosely coupled designs between objects that interact.

The Podcast class doesn’t know much about its subscribers. It only knows each subscriber has an update method.

This loose coupling minimizes the dependency between Podcast and its subscribers. It also maximizes flexibility. As long as it has an update method, a subscriber can be anything: a human, a group of people, an animal, or even a car.

Takeaways:

  1. The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.
  2. Loose Coupling design principle: strive for loosely coupled designs between objects that interact.

Thanks for reading. Are there any other real-life examples of the Observer pattern you can think of? ?

I publish to sihui.io weekly.

Subscribe so you won’t miss the next article from the series.

Next time we will talk about…

1*dIc3a1EKWwu3dwB1sPtvRw