Observer Design Pattern

Hikmet Çakır
2 min readAug 14, 2022

--

Photo by Maarten van den Heuvel on Unsplash

Observer is a structural design pattern that lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.

Sometimes a structure may need to be changed and some structures are need to be notified about changing. Roughly, the change may need to be reported to another place or places.

For instance, let’s suppose we’re a ticket seller. We’re selling concert tickets and customers tell us their own attention areas for example a customer tell to us he likes Jazz, Blues and Country genres etc. and if there are some events related with customer’s own attention areas, the system will notify the customer. For structures like this, we should use observer design pattern.

How to Implement

  1. Declare the subscriber interface. It generally contains only one method which name is update.
  2. Implement the update notification methods in concrete subscriber class.
  3. Create a manager class to manage methods that is related with notification mechanism such as add subscriber, remove subscriber and send notify etc.
  4. Create a concrete publisher class. This class does various operations then it calls creating manager class’s notify method to send notification.
  5. The client must create all necessary subscribers and register them with proper publishers.

Example Implementation

I had given an example about concert ticket seller before. Let’s implement this structure. Firstly, we should declare a subscriber interface so I defined a interface that name is EventListener. This interface contains only one method.

Secondly, the concreate subscriber classes which implements subscriber interface should be defined. I created two concreate subscriber classes which names are SmsNotificationListener and EmailNotificationListener. Both of them implement EventListener class.

Thirdly, we should define manager class to manage events. Basically, this class should contain some methods that provides event managing such as add subscriber, remove subscriber and send notification etc.

Fourthly, we should create publisher class. This class provides various operations and sends notification through these operations.

Additionally, I defined an enum that name is Genre. This enum gives information about which genre supports.

On the other hand, EventDetail provides sending event’s detail like when will event start?, what is the price? and which musician?

In the end, let’s look from client side. In the first, some subscriber are being added to salesman then salesman’s sending notifications related with events.

I hope everything’s clear for you. If you don’t understand any part, feel free ask me. Also, I shared the codes I explained and more in GitHub. Additionally, certainly I recommend that you should practice with this structures. I coded these structures and these structures’ some test scenarios. You can check out to repository.

I used various resource for prepare this essay. I indicated in following. You can check out.

--

--

No responses yet