Open In App

Observer Pattern | C++ Design Patterns

The Observer Pattern is a behavioral design pattern that defines a one-to-many dependency between objects, meaning that when one object (the subject) changes its state, all its dependents (observers) are notified and updated automatically. This pattern is used to build distributed event-handling systems and is a crucial part of many software architectures, including Model-View-Controller (MVC).

Key Concepts of the Observer Pattern

Before diving into the implementation of the Observer Pattern in C++, let’s understand its key components:



Observer Pattern Example in C++:

Problem statement: Suppose you are developing a weather monitoring application, in which multiple weather stations are responsible for collecting weather data, and you want to create a system where multiple displays can show real-time weather updates. When a weather station collects new data, all registered displays should be updated automatically with the latest information.

Below is the implementation of the above example problem in C++:






#include <iostream>
#include <vector>
 
// Observer interface
class Observer {
public:
    virtual void update(float temperature, float humidity, float pressure) = 0;
};
 
// Subject (WeatherStation) class
class WeatherStation {
private:
    float temperature;
    float humidity;
    float pressure;
    std::vector<Observer*> observers;
 
public:
    void registerObserver(Observer* observer) {
        observers.push_back(observer);
    }
 
    void removeObserver(Observer* observer) {
        // You can implement the removal logic if needed.
    }
 
    void notifyObservers() {
        for (Observer* observer : observers) {
            observer->update(temperature, humidity, pressure);
        }
    }
 
    void setMeasurements(float temp, float hum, float press) {
        temperature = temp;
        humidity = hum;
        pressure = press;
        notifyObservers();
    }
};
 
// Concrete Observer
class Display : public Observer {
public:
    void update(float temperature, float humidity, float pressure) {
        std::cout << "Display: Temperature = " << temperature
                  << "°C, Humidity = " << humidity
                  << "%, Pressure = " << pressure << " hPa"
                  << std::endl;
    }
};
 
int main() {
    WeatherStation weatherStation;
 
    // Create displays
    Display display1;
    Display display2;
 
    // Register displays as observers
    weatherStation.registerObserver(&display1);
    weatherStation.registerObserver(&display2);
 
    // Simulate weather data updates
    weatherStation.setMeasurements(25.5, 60, 1013.2);
    weatherStation.setMeasurements(24.8, 58, 1014.5);
 
    return 0;
}

Output:

Display: Temperature = 25.5°C, Humidity = 60%, Pressure = 1013.2 hPa
Display: Temperature = 24.8°C, Humidity = 58%, Pressure = 1014.5 hPa

Code Explaination:

Advantages of the Observer Pattern in C++ Design Patterns

Disadvantages of the Observer Pattern in C++ Design Patterns


Article Tags :