Guide and Real world example for Observer Design Pattern in Java

Posted By : Vaibhav Balyan | 30-Jul-2020

 

Introduction

Observer Design Pattern is classified under Behavioural Design Pattern and is used when several different objects dependent on other objects need to perform a custom action when the state of that object changes. These objects can notify their parent class without sharing their own information. The main functionality of the Observer Design Pattern is that it creates and defines a one-to-many relationship between objects so that when one object changes, the state of all related objects changes automatically.

Generically, the object that is being viewed is called the Subject, and objects that are viewing are called Observers.

 

Real-Life Example

The example of Youtube can be taken in order to explain the observer design pattern. Each YouTuber has many subscribers on their channel. Each subscriber wants to watch their subscribed channel's videos. So till a subscriber has an interest in a specific YouTuber’s channel, they subscribe to it. When they lose interest, they unsubscribe the channel. Here, we can think of subscribers as an observer and the YouTuber as the subject.  So, if we start creating domains we should create a class that adds, deletes, or notifies all observers.

 

class Subject{

public void subscribe(Observer observer);

public void unsubscribe(Observer observer);

public void notifyObservers(String message);

}

A interface is to be created which is called when Subject Changes.

interface observer{

public void update(String name,String message);

}

Next would be the class for youtubers where they override all methods from Subject class.

class Youtuber implements Subject{

private String youtuberName;

private ArrayList<Observer> subscribers;               // List of subscribers

public Youtuber(String youtuberName)

{

this.youtuberName = youtuberName;

subscribers = new ArrayList<Observer>;

}

@Override

public void subscribe(Observer observer)

   subscribers.add(observer);

 

@Override

public void unsubscribe(Observer observer)

   subscribers.remove(observer);

 

@Override

public void notifyObservers(String videoName)

for(Observer subscriber:subscribers)

    subscriber.update(youtuberName,videoName);

}

 

public void videoName(String videoName){

System.out.println(youtuberName + "has uploaded" + videoName + "video on his channel");

notifyObservers(videoName)

}

 

Now the class for YouTubers is ready , we will create class for subscribers now and how they can subscriber to many youtubers.

 

class Subscriber implements Observer{

private String subscriberName;

public Subscriber(String subscriberName)

this.subscriberName = subscriberName;

 

public void update(String youtuberName, String videoName)

System.out.println(subscriberName + "you have to see an amazing video by" + youtuberName + "whose video's name is" + videoName);

Finally, we will implement a pattern in this main class.

class ObserverPattern{

public static void main(String args[]){

Youtuber pewdiepie = new Youtuber("Pewdiepie");

Youtuber tSeries = new Youtuber("T-Series");

Subscriber vaibhav = new Subscriber("Vaibhav");

Subscriber gaurav = new Subscriber("Gaurav");

Subscriber joy = new Subscriber("Joy");

Subscriber vega = new Subscriber("Vega");

pewdiepie.register(vaibhav);

pewdiepie.register(joy);

tSeries.register(gaurav);

tSeries.register(vega);

pewdiepie.videoName("Fifa Gaming");

tSeries.videoName("Romantic Song");

pewdiepie.remove(joy);

}

We observe that the Subject class does not know about the Observer class and we can reuse subject and observer classes independently. Observers can be added or removed anytime from the subscribed youtube channel. The only issue in using this pattern is memory leak as Subject class will hold all the objects of the Observer's class and if we don't delete them, then there can be a memory leak.

 

We are a Java Development Company with extensive experience in custom developing mobile and web applications for enterprises around the world. Our development services include CRM, WFM, HRM, WMS, Finance, Accounting, and eCommerce software development tailormade to suit your business model. We use a wide range of technologies for automation of business operations to maximize productivity and profitability. Get in touch with us for custom software development and implementation.

 

 


Please remove URL from text

Comment is required

Sending message..
Connect with us