Design Pattern: Observer Pattern in Java

Observer pattern is used to automatically notify state change in object to all its dependent objects. It works as a pub/sub for state change between objects.

This article demonstrates Observer pattern implementations in Java. Check the following examples.

Examples

Check the following examples of the Observer pattern implementation in Java.

Example #1: General Observer

Let’s consider a general example of a simple Observer pattern demonstration.

Class Diagram

Take a look at the class diagram.

Subject Interface

// Subject.java

package com.bigboxcode.designpattern.observer.observer;

public interface Subject {

    int getState();

    void setState(int state);

    void attach(Observer observer);

    void notifyObservers();

}

Concrete Subject

// ConcreteSubject.java

package com.bigboxcode.designpattern.observer.observer;

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

public class ConcreteSubject implements Subject{

    private int state;

    private List<Observer> observerList = new ArrayList<>();

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;

        notifyObservers();
    }

    public void attach(Observer observer) {
        observerList.add(observer);
    }

    public void notifyObservers() {
        for (Observer observer : observerList) {
            observer.sendUpdate();
        }
    }

}

Observer Interface

package com.bigboxcode.designpattern.observer.observer;

public abstract class Observer {
    protected Subject subject;

    public abstract void sendUpdate();
}

Observer One

package com.bigboxcode.designpattern.observer.observer;

public class ObserverOne extends Observer{

    public ObserverOne(Subject subject){
        this.subject = subject;
        this.subject.attach(this);
    }

    @Override
    public void sendUpdate() {
        System.out.println( "Received in ObserverOne: " + subject.getState());
    }
}

Observer Two

package com.bigboxcode.designpattern.observer.observer;

public class ObserverTwo extends Observer{

    public ObserverTwo(Subject subject){
        this.subject = subject;
        this.subject.attach(this);
    }

    @Override
    public void sendUpdate() {
        System.out.println( "Received in ObserverTwo: " + subject.getState());
    }
}

Demo

// Demo.java

package com.bigboxcode.designpattern.observer.observer;

public class Demo {
    public static void main(String[] args) {
        Subject subject = new ConcreteSubject();

        new ObserverOne(subject);
        new ObserverTwo(subject);

        System.out.println("Setting subject value to 10");
        subject.setState(10);

        System.out.println("n-----------------------------n");

        System.out.println("Setting subject value to 999");
        subject.setState(999);
    }
}

Output

Setting subject value to 10
Received in ObserverOne: 10
Received in ObserverTwo: 10

-----------------------------

Setting subject value to 999
Received in ObserverOne: 999
Received in ObserverTwo: 999

Source Code

Use the following link to get the source code:

Other Code Implementations

Use the following links to check Observer pattern implementation in other programming languages.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.