Observer Design Pattern
The Observer design pattern, also known as the Publish-Subscribe or Dependents, is a behavioral design pattern.
Observer Design Pattern
The Observer design pattern, also known as the Publish-Subscribe or Dependents, is a behavioral design pattern.
This observer pattern can be applied when a change to one object requires notifying other dependent objects. Here, the internal state change of one object can be reflected in another dependent object without tight coupling between objects.
The classes and respective objects initiate in this observer pattern can be identified as subject and observer(s).
Design Diagram

Subject: provides an interface for client for attaching and detaching observers, and this is aware of its observers
Observer: provides the updating interface for ConcreteObserver object instances that should be notified of changes in the subject state.
ConcreteSubject: implements the Subject and stores the subject state which has the interest of ConcreteObserver object instances. Notifies the attached observers when the subject state changes.
for all o in observers {
o -> update()
}
ConcreteObserver: implements the Observer and stores the observer state that should stay consistent with the subject state.
observerState = subject -> getState ()
Implementation
Consider a weather station which has multiple observers for the weather station state value.

c
WeatherStation provides the interface for client for attaching and detaching observers.
#pragma once
#include <unordered_set>
#include "../Observer/Observer.h"
class WeatherStation {
public:
virtual void attach(Observer* observer);
virtual void detach(Observer* observer);
virtual void notify() = 0;
protected:
std::unordered_set<Observer*> observers;
};
#include "WeatherStation.h"
void WeatherStation::attach(Observer *observer) {
observers.insert(observer);
}
void WeatherStation::detach(Observer *observer) {
observers.erase(observer);
}
Observer provides the updating interface for concrete observer object instances.
#pragma once
#include <string>
class Observer {
public:
virtual void update(std::string message) = 0;
Observer();
private:
static int observerCount;
protected:
int currentObserverId;
};
#include "Observer.h"
Observer::Observer() {
observerCount++;
currentObserverId = observerCount;
}
ConcreteWeatherStation implements the WeatherStation.
#pragma once
#include "../WeatherStation.h"
class ConcreteWeatherStation: public WeatherStation {
public:
ConcreteWeatherStation(const std::string &stateMessage);
void notify() override;
void setState(std::string state);
std::string getState();
private:
std::string state;
};
#include "ConcreteWeatherStation.h"
void ConcreteWeatherStation::notify()
{
std::unordered_set<Observer *>::iterator observersIterator;
for (observersIterator = observers.begin();
observersIterator != observers.end();
observersIterator++)
{
Observer *currentObserver = *observersIterator;
currentObserver->update(this->getState());
}
}
ConcreteWeatherStation::ConcreteWeatherStation(const std::string &state) {
this->setState(state);
}
void ConcreteWeatherStation::setState(std::string state) {
this->state = state;
notify();
}
std::string ConcreteWeatherStation::getState() {
return this->state;
}
Dashboard and Forecast implements the Observer.
#pragma once
#include "../Observer.h"
class Dashboard: public Observer {
public:
void update(std::string message) override;
const std::string &getWeatherState() const;
void setWeatherState(const std::string &stationState);
Dashboard();
private:
std::string weatherState;
};
#include <iostream>
#include "Dashboard.h"
void Dashboard::update(std::string value) {
std::cout
<< "OBSERVER::"
<< this->currentObserverId
<< ":: UPDATE RECEIVED :: "
<< value
<< std::endl;
this->setWeatherState(value);
}
Dashboard::Dashboard() {}
const std::string& Dashboard::getWeatherState() const {
return weatherState;
}
void Dashboard::setWeatherState(const std::string &weatherState) {
this->weatherState = weatherState;
}
#pragma once
#include "../Observer.h"
class Forecast: public Observer {
public:
void update(std::string message) override;
const std::string &getWeatherState() const;
void setWeatherState(const std::string &weatherState);
Forecast();
private:
std::string weatherState;
};
#include <iostream>
#include "Forecast.h"
void Forecast::update(std::string value) {
std::cout
<< "OBSERVER::"
<< this->currentObserverId
<< ":: UPDATE RECEIVED :: "
<< value
<< std::endl;
this->setWeatherState(value);
}
Forecast::Forecast() {}
const std::string& Forecast::getWeatherState() const {
return weatherState;
}
void Forecast::setWeatherState(const std::string &weatherState) {
this->weatherState = weatherState;
}
Client can attach or detach observer Dashboard, Forecast instances with subject WeatherStation type concrete ConcreteWeatherStation instance.
#include <iostream>
#include "Observer/Impl/Dashboard.h"
#include "Observer/Impl/Forecast.h"
#include "Subject/Impl/ConcreteWeatherStation.h"
int Observer::observerCount = 0;
int main() {
Dashboard* dashboard = new Dashboard();
Forecast* concreteObserver2 = new Forecast();
ConcreteWeatherStation* pWeatherStation = new ConcreteWeatherStation("NORMAL");
pWeatherStation->attach(dashboard);
pWeatherStation->attach(concreteObserver2);
pWeatherStation->notify();
pWeatherStation->setState("ADVISORY");
pWeatherStation->detach(concreteObserver2);
pWeatherStation->setState("WARNING");
return 0;
} 메타데이터
- post_id
- ad1c2a0fec09
- slug
- observer-design-pattern-ad1c2a0fec09
- url
- https://medium.com/@pawara/observer-design-pattern-ad1c2a0fec09
- canonical_url
- https://medium.com/@pawara/observer-design-pattern-ad1c2a0fec09
- author_url
- https://medium.com/@pawara
- status
- ok
- fetched_at
- 2026-08-06 10:49:58