Observer Design Pattern

The observer pattern is a software design pattern in which an object,called the subject maintains a list of its dependents,called observers and notifies them automatically of any state changes,usually by calling one of their methods.Define a one to many dependency between objects so that when one object changes,all its dependents are notified and updated automatically.

Lets take an example where we have social media post and there are multiple subscribers to that post such that if any new user add comments or like the post,all the subscribers should know about this activity.So lets implement the same with observer pattern.Here Post will act like Subject and Users will act like observers so that whenever any comment is added to post or it is liked ,all the users(observers) gets notified.

  package com.dp.observer;

  public interface Observer {
    
    void update(Object object);

  }

package com.dp.observer;

import java.util.ArrayList;
import java.util.List;
/*
 * Observer
 */
public class User implements Observer{
    
    private List<String> notifications = new ArrayList<>();
    private String name;

    @Override
    public void update(Object object) {
        System.out.println("Hey ,"+name+" "+object.toString());
        notifications.add(object.toString());
    }

    public List<String> getNotifications() {
        return notifications;
    }

    public void setNotifications(List<String> notifications) {
        this.notifications = notifications;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    

}

package com.dp.observer;

import java.util.ArrayList;
import java.util.List;
/*
 * This is the subject
 */
public class Post {
    private String postContents;
    private List<User> subscribers = new ArrayList<>();
    private List<String> comments = new ArrayList<>();
    private int likes;
    
    public String getPostContents() {
        return postContents;
    }
    public void setPostContents(String postContents) {
        this.postContents = postContents;
    }
    public List<User> getSubscribers() {
        return subscribers;
    }
    public void setSubscribers(List<User> subscribers) {
        this.subscribers = subscribers;
    }
    public List<String> getComments() {
        return comments;
    }
    public void setComments(List<String> comments) {
        this.comments = comments;
    }
    public int getLikes() {
        return likes;
    }
    public void setLikes(int likes) {
        this.likes = likes;
    }
    
    public void incrementLike(){
        likes++;
        notifyObservers("Post like is inscremented by 1");
    }
    public void decrementLikes(){
        likes--;
    }
    public void addComment(String comment){
        comments.add(comment);
        notifyObservers("New comment is added for the post as:"+comment);
    }
    public void addSubscriber(User user){
        subscribers.add(user);
    }
    private void notifyObservers(Object object){
        for (Observer observer : subscribers) {
            observer.update(object);
        }
    }

}

package com.dp.observer;

public class Client {
    public static void main(String[] args) {
        
        Post post = new Post();
        post.setPostContents("Going to manali");
        post.setLikes(10);
        
        User user = new User();
        user.setName("Nitin");
        post.addSubscriber(user);
        
        user = new User();
        user.setName("Sagar");
        post.addSubscriber(user);
        
        post.incrementLike();
        post.addComment("Happy journey");
        
        
    }

}

Output:
Hey ,Nitin Post like is inscremented by 1
Hey ,Sagar Post like is inscremented by 1
Hey ,Nitin New comment is added for the post as:Happy journey
Hey ,Sagar New comment is added for the post as:Happy journey

Comments