Observer Design Pattern

Observer Design pattern is a behavioural design pattern, Behavioral design patterns are named so because they primarily focus on managing the behavior and interaction of objects within a software system.

The observer design pattern, also known as the publish-subscribe pattern, is a software design pattern that establishes a one-to-many relationship between objects. In this pattern, there is a subject (also called the observable or publisher) and multiple observers (also called subscribers or listeners).

The main idea behind the observer pattern is that when the subject's state or data changes, it automatically notifies and updates all its observers(subscribers). This way, the observers can react to the changes and perform their respective actions accordingly.

In simpler words, the observer pattern is like subscribing to a newsletter. The subject is the newsletter publisher, and the observers are the subscribers. Whenever a new edition of the newsletter is released, the publisher sends it to all the subscribers automatically. The subscribers receive the updates and can read the newsletter or take any other action they want.

Code Example

package observerdesignpattern;


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

interface YouTubeChannel {
    void subscribe(Subscribers user);
    void unsubscribe(Subscribers user);


    void notifyAllSubscribers(String status);

    void update(String status);
}

interface Subscribers {

    void update(String status);

}

class TechAdoraByNivedita implements YouTubeChannel {

    List<Subscribers> subscribersList = new ArrayList<>();
    @Override
    public void subscribe(Subscribers user) {
        subscribersList.add(user);

    }

    @Override
    public void unsubscribe(Subscribers user) {
        subscribersList.remove(user);
    }

    @Override
    public void notifyAllSubscribers(String status) {
        for(Subscribers user : subscribersList)
        {
            user.update(status);
        }


    }

    @Override
    public void update(String status) {
        notifyAllSubscribers(status);

    }
}

class NewUser implements  Subscribers
{

    @Override
    public void update(String status) {
        System.out.print("Notification: " + " new vide got released  " +  status);
    }
}


public class YouTube {
    public static void main(String[] args) {

        NewUser u1 = new NewUser();
        NewUser u2 = new NewUser();

        TechAdoraByNivedita t1 = new TechAdoraByNivedita();
        t1.subscribe(u1);
        t1.subscribe(u2);
        t1.update("H-Index Solution Video");
        t1.unsubscribe(u1);
        t1.update("Nth Element Solution");
    }
}

Here are some scenarios where the observer design pattern can be applied effectively:

1. Event Handling: When an event occurs in a system, the observer pattern can be used to notify and trigger actions in multiple event listeners or subscribers.

  1. Message Brokers Systems: Observer pattern is commonly employed in message-driven architectures, where a message broker facilitates the distribution of messages to multiple subscribers.

Pros

  1. Extensibility: The pattern allows for easy addition of new observers without modifying the subject.

  2. Simplifies Event Handling: The observer pattern simplifies the handling of events or changes in the system. It provides a clear and structured way to notify interested observers about state changes or events.

Cons

  1. Order of Notification: The order in which observers are notified may be important in some cases, but the basic observer pattern does not define a specific order. Additional logic may be required to handle specific notification sequences.

  2. Performance Impact: If there are a large number of observers or frequent updates, the observer pattern can introduce performance overhead. Notifying multiple observers sequentially can impact system performance.

  3. Unexpected Updates: Observers may receive updates they are not interested in.