DEV Community

Prashant Mishra
Prashant Mishra

Posted on

Observer

Observer pattern is used when there is one to many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically. Observer pattern falls under behavioural pattern category.
Example: a Youtube Channel and its subscribers
It is also called pub sub model

Whenever new contents in the youtube channel is added or existing content is modified, all the subscribers of the youtube channel will be notified

Let's understand the same example

observer

Subject or YoutubeChannel

package Patterns.Behavioral.observer;

import java.util.ArrayList;
import java.util.List;

public class YouTubeChannelSubject{
    private String channelName;
    private List<String> contents;
    List<Observer> subscriberObservers;
    public YouTubeChannelSubject(){
        this.channelName = "Mr.Beast";
        contents = new ArrayList<>();
        subscriberObservers = new ArrayList<>();
    }
    public void addNewContent(String name){
        contents.add(name);
        //notify all listeners
        notifySubscriberObserver();
    }
    public void removeContent(String name){
        if(contents.contains(name)){
            contents.remove(name);
            //notify all listeners
            notifySubscriberObserver();
        }
    }
    public List<String> getContents(){
        return this.contents;
    }
    public String getChannelName(){
        return this.channelName;
    }
    public void addObserver(Observer observer){
        this.subscriberObservers.add(observer);
    }

    public void removeObserver(Observer observer){
        this.subscriberObservers.remove(observer);
    }

    public void notifySubscriberObserver(){
        for(Observer subscriber : subscriberObservers){
            subscriber.update();
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Abstract Subscriber or Observer

package Patterns.Behavioral.observer;

public abstract class Observer {
    public String subscriberName;
    public YouTubeChannelSubject subject;
    public Observer(YouTubeChannelSubject youTubeChannelSubject,String name){
        this.subject = youTubeChannelSubject;
        this.subscriberName = name;
    }
    abstract void update();
    public YouTubeChannelSubject getSubject(){
        return this.subject;
    }
}
Enter fullscreen mode Exit fullscreen mode

Concrete Subscribers

package Patterns.Behavioral.observer;

public class SubscriberOneObserver extends Observer {
    public SubscriberOneObserver(YouTubeChannelSubject subject,String name){
        super(subject,name);
        // add this subscriber as of the observers of the youtube channel defined by object 'subject'
        this.subject.addObserver(this);
    }

    @Override
    void update() {
        System.out.println(this.subscriberName +" got updated !");
        System.out.println(this.subject.getChannelName()+" has updated contents, the total content on the channel now is "+ this.subject.getContents().size());
        System.out.println("List of contents on the channel "+this.subject.getChannelName()+" are  as follows: ");
        for(String content : subject.getContents()){
            System.out.println(content);
        }
        System.out.println("------------------------------");
    }

}
Enter fullscreen mode Exit fullscreen mode
package Patterns.Behavioral.observer;

public class SubscriberTwoObserver extends Observer {
    public SubscriberTwoObserver(YouTubeChannelSubject subject,String name){
        super(subject,name);
        // add this subscriber as of the observers of the youtube channel defined by object 'subject'
        this.subject.addObserver(this);
    }

    @Override
    void update() {
        System.out.println(this.subscriberName +" got updated !");
        System.out.println(this.subject.getChannelName()+" has updated contents, the total content on the channel now is "+ this.subject.getContents().size());
        System.out.println("List of contents on the channel "+this.subject.getChannelName()+" are  as follows: ");
        for(String content : subject.getContents()){
            System.out.println(content);
        }
        System.out.println("------------------------------");
    }

}
Enter fullscreen mode Exit fullscreen mode

Main

package Patterns.Behavioral.observer;

public class Main {
    public static void main(String args[]){
        //creating youtube channel
        YouTubeChannelSubject subject = new YouTubeChannelSubject();

        //subscriber's of the above youtube channel
       new SubscriberOneObserver(subject,"prashant");
       new SubscriberTwoObserver(subject,"sandeep");

       //now updating the contents of the youtube channel
       subject.addNewContent("Men Vs Women survive the wilderness of $500,000");
       subject.addNewContent("7 Days stranded in a cave");
    }
}
Enter fullscreen mode Exit fullscreen mode

output

prashant got updated !
Mr.Beast has updated contents, the total content on the channel now is 1
List of contents on the channel Mr.Beast are  as follows: 
Men Vs Women survive the wilderness of $500,000
------------------------------
sandeep got updated !
Mr.Beast has updated contents, the total content on the channel now is 1
List of contents on the channel Mr.Beast are  as follows: 
Men Vs Women survive the wilderness of $500,000
------------------------------
prashant got updated !
Mr.Beast has updated contents, the total content on the channel now is 2
List of contents on the channel Mr.Beast are  as follows: 
Men Vs Women survive the wilderness of $500,000
7 Days stranded in a cave
------------------------------
sandeep got updated !
Mr.Beast has updated contents, the total content on the channel now is 2
List of contents on the channel Mr.Beast are  as follows: 
Men Vs Women survive the wilderness of $500,000
7 Days stranded in a cave
------------------------------
prashantmishra@Prashants-MacBook-Air machinecoding % 

Enter fullscreen mode Exit fullscreen mode
đź‘‹ While you are here

Reinvent your career. Join DEV.

It takes one minute and is worth it for your career.

Get started

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.

đź‘‹ Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay