Understanding Notifications through Publisher-Subscriber design pattern and Java

Notification is a very common word nowadays. But do you know the principle behind this model? Let's see how this model works behind the scenes

·

3 min read

Notification is a very common word nowadays. I remember the first time I heard this word was when I used Facebook. There was this panel that provided some alerts with data. This alert was called a notification.

But this notification model is rampant everywhere now. Whenever there is a source for information and a consumer, this model can be implemented. The source can notify the consumer using this model. The best example to understand this is YouTube.

Most of us have a favorite YouTuber. And often they ask us to subscribe to their channel. Once you click that subscribe button, you become a subscriber, and whenever a video is uploaded on that channel, you get a notification.

Let's see how this can be implemented in Java.

Let's start with a YoutubeChannel class.

This class will act as a source of notification and will notify its subscribers.

Next, we need a Subscriber class. The object of Subscriber will receive the notification and will print that it has been notified.

Let’s look at the source code of these classes.

package tech.divij;

import java.util.List;
import java.util.LinkedList;

public class YoutubeChannel {
    String channelName;
    private List<Subscriber> subscribers = new LinkedList<>();

    public YoutubeChannel(String channelName) {
        this.channelName = channelName;
    }

    public void addSubscriber(Subscriber subscriber) {
        this.subscribers.add(subscriber);
        System.out.println("Subscriber " 
            + subscriber.name + 
                " added to channel " + 
                    this.channelName);
    }

    public removeSubscriber(Subscriber subscriber) {
        System.out.println(this.subscribers.remove(subscriber) 
            ? "Subscriber " + subscriber.subscriberName + 
                " is removed" : "Subscriber not found"));
    }

    public void notifyAllSubscribers() {
        System.out.println("Notifying all subscribers");
        for (Subscriber subscriber: subscribers) {
            subscriber.notify();
        }
    }
}
package tech.divij;
public class Subscriber {
    String subscriberName;
    public void notify() {
        System.out.println("Subscriber " 
        + this.subscriberName + 
            " has been notified");
    }
    public Subscriber(String subscriberName) {
        this.subscriberName = subscriberName;
    }
}

Now let’s make use of above class for Publisher-Subscriber model. For this, let’s use NotificationDemo class:

package tech.divij;
public class NotificationDemo {
    public static void main(String args[]) {
        YoutubeChannel channel = new YoutubeChannel("Planet Earth");
        Subscriber subscriber1 = new Subscriber("Ankit");
        Subscriber subscriber2 = new Subscriber("Bill");
        Subscriber subscriber3 = new Subscriber("Chandler");
        channel.addSubscriber(subscriber1);
        channel.addSubscriber(subscriber2);
        channel.notifyAllSubscribers();
        channel.addSubscriber(subscriber3);
        channel.removeSubscriber(subscriber1);
        channel.notifyAllSubscribers();
    }
}

Output:

Subscriber Ankit added to channel Planet Earth

Subscriber Bill added to channel Planet Earth

Notifying all subscribers

Subscriber Ankit has been notified

Subscriber Bill has been notified

Subscriber Chandler added to channel Planet Earth

Subscriber Ankit is removed

Notifying all subscribers

Subscriber Bill has been notified

Subscriber Chandler has been notified

As you can see in class NotificationDemo, first, we created a channel named Planet Earth, then we added two subscribers to it, Ankit and Bill, and notified them successfully. Then we removed subscriber Ankit and added subscriber Chandler, and again notified all subscribers of Planet Earth YoutubeChannel, and subscribers Bill and Chandler are notified.

This is the publisher-subscriber model in its simplest form. But why does this work? The simple idea behind the working of this model is that the objects in Java are saved on the heap, and they can be passed across the program by reference. This way, the publisher keeps references of the subscriber objects, and once it needs to notify them, it can do so through the reference that it has.

I hope this article has helped you in understanding the Publisher-Subscriber Design pattern. We can also make it more interesting using OOPs concepts and some other design principles. I will show that to you in another blog.

Until then, adios!

Happy Coding!