← Back to list

Observer Design Pattern vs State Design Pattern

Both the Observer and State design patterns may seem similar at first glance, as they are both used to handle internal state changes…

Samith Ailapperuma · 2025-11-09 18:11 · 1 claps · 2.1 min read
#design-patterns #observer-design-pattern #state-design-pattern
Open on Medium ↗

Observer Design Pattern vs State Design Pattern

Both the Observer and State design patterns may seem similar at first glance, as they are both used to handle internal state changes. However, the purposes they serve and the ways they handle state changes are quite different.

Observer Design Pattern

The key idea is that an object (the subject) would notify all its dependents (the observers) about a state change, allowing them to update accordingly. The subject would not be aware of how many observers are subscribed. It simply sends the update and the observers decide how to handle it.

Consider an example of a weather station. The weather station would periodically check the temperature and humidity and notify all its observers whenever the weather information changes.

interface Observer {
    void update(float temperature, float humidity);
}

interface Subject {
    void registerObserver(Observer o);
    void removeObserver(Observer o);
    void notifyObservers();
}

class WeatherStation implements Subject {
    private List<Observer> observers = new ArrayList<>();
    private float temperature;
    private float humidity;

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o);
    }

    @Override
    public void notifyObservers() {
        for (Observer o : observers) {
            o.update(temperature, humidity);
        }
    }

    // This simulates new weather readings
    public void setMeasurements(float temperature, float humidity) {
        this.temperature = temperature;
        this.humidity = humidity;
        notifyObservers();
    }
}

Assume that there are several observers, such as a phone display, a TV display, and a statistics dashboard, that require this information. These observers can implement the Observer interface and define their own behaviour upon receiving the weather updates through the update() method.

Each observer defines its own behaviour upon receiving updates, and new observers can be registered through the registerObserver() method.

State Design Pattern

The key idea is that an object changes its behaviour based on its internal state. Each state is encapsulated in a seperate class, and the context delegates behaviour to the object representing the current state.

Consider an example of a TCP connection. The TCP connection could be in multiple states such as Closed or Established. The same open() action would behave differently based on which state the system is currently in.

// State interface
interface State {
    void handle(TCPConnection context);
}

// Concrete States
class ClosedState implements State {
    public void handle(TCPConnection context) {
        System.out.println("Opening connection...");
        context.setState(new EstablishedState());
    }
}

class EstablishedState implements State {
    public void handle(TCPConnection context) {
        System.out.println("Already connected!");
    }
}

// Context
class TCPConnection {
    private State state = new ClosedState();

    public void setState(State state) { this.state = state; }

    public void open() { state.handle(this); }
}

Calling the open() method when the internal state of the TCP Connection class is Closed would change the state to Established, while calling the method when the internal state is Established would simply print “Already connected”.

Conclusion

In conclusion, the Observer design pattern and State design pattern serve very different purposes. The Observer pattern is used to notify observers of a state change, while the State pattern alters the object’s behaviour based on its internal state.


메타데이터
post_id
d878eb60730f
slug
observer-design-pattern-vs-state-design-pattern-d878eb60730f
url
https://medium.com/@samithailapperuma/observer-design-pattern-vs-state-design-pattern-d878eb60730f
canonical_url
https://medium.com/@samithailapperuma/observer-design-pattern-vs-state-design-pattern-d878eb60730f
author_url
https://medium.com/@samithailapperuma
status
ok
fetched_at
2026-08-06 10:49:58