Observer Pattern

📢 This article was translated by gemini-3-flash-preview

Observer Pattern (Behavioral Object Pattern)

Intent

Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Structure

Observer Pattern

Roles:

  • Subject: Knows its observers. Any number of observers can observe a single subject. It provides an interface for attaching and detaching observer objects.
  • Observer: Defines an updating interface for objects that should be notified of changes in a subject.
  • ConcreteSubject: Stores state of interest to ConcreteObservers. Sends a notification to its observers when its state changes.
  • ConcreteObserver: Maintains a reference to a ConcreteSubject. Stores state that should stay consistent with the subject’s. Implements the Observer updating interface to keep its state consistent with the subject’s.

Applicability

Use the Observer pattern when:

  • An abstraction has two aspects, one dependent on the other. Encapsulating these in separate objects lets you vary and reuse them independently.
  • A change to one object requires changing others, and you don’t know how many objects need to be changed.
  • An object needs to notify other objects without making assumptions about who these objects are (i.e., you want to avoid tight coupling).

Example 1

In a file management system, OfficeDoc and DocExplorer classes are defined. When an OfficeDoc object changes, all DocExplorer objects must update their state. Here is the class diagram using the Observer pattern:

Observer Pattern Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;

interface Observer {
    public void update();
}

interface Subject {
    public void Attach(Observer obs);
    public void Detach(Observer obs);
    public void Notify();
    public void setStatus(int status);
    public int getStatus();
}

class OfficeDoc implements Subject {
    private List<Observer> myObs;
    private String mySubjectName;
    private int m_status;
    public OfficeDoc(String name) {
        mySubjectName = name;
        this.myObs = new ArrayList<Observer>();
        m_status = 0;
    }
    public void Attach(Observer obs) { this.myObs.add(obs); }
    public void Detach(Observer obs) { this.myObs.remove(obs); }
    public void Notify() {
        for (Observer obs : this.myObs) {obs.update(); }
    }
    public void setStatus(int status) {
        m_status = status;
        System.out.println("SetStatus subject[" + mySubjectName + "]status:" + status);
    }
    public int getStatus() { return m_status; }
}

class DocExplorer implements Observer {
    private String myObsName;
    public DocExplorer(String name,Subject sub) {
        myObsName = name;
        sub.Attach(this);
    }
    public void update() {
        System.out.println("update observer[" + myObsName + "]");
    }
}

class ObserverTest {
    public static void main(String[] args) {
        Subject subjectA = new OfficeDoc("subject A");
        Observer observerA = new DocExplorer("observer A", subjectA);
        subjectA.setStatus(1);
        subjectA.Notify();
    }
}

Example 2

Subject acts as a YouTuber, and Observer acts as a Subscriber.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.util.ArrayList;
import java.util.List;

public class ObserverPattern {
    public static void main(String[] args) {
        Subject subjectA = new ConcerteSubject();

        Observer observer1 = new ConcerteObserver("Mike", subjectA);
        Observer observer2 = new ConcerteObserver("Jane", subjectA);

        subjectA.Notify();
    }
}

interface Subject{ // Subject
    public void Attach(Observer observer);
    public void Detach(Observer observer);
    public void Notify();
    public String getState();
    public void setState(String state);
}

class ConcerteSubject implements Subject{
    private String state;

    private List<Observer> observerList;

    public String getState() {
        return state;
    }

    @Override
    public void setState(String state) {
        this.state = state;
        this.Notify();
    }

    public ConcerteSubject(){
        state = "-1";
        observerList = new ArrayList<>();
    }

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

    public void Detach(Observer observer){
        observerList.remove(observer);
    }

    public void Notify(){
        for(Observer o : observerList){
            o.update();
        }
    }
}

interface Observer{ // Observer
    public void update();
}

class ConcerteObserver implements Observer{
    private String name;
    private Subject subject;
    private String state;

    public ConcerteObserver(String name, Subject subject){
        this.name = name;
        this.state = subject.getState();
        this.subject = subject;

        subject.Attach(this);
    }

    @Override
    public void update(){
        System.out.println(this.name + " Received");
        this.state = subject.getState();
    }
}