CodeBlanks Logo Icon CodeBlanks

Observer

Learn event-based notification with the Observer pattern.

What is the Observer pattern?

Observer lets a subject notify all attached observers when its state changes. This is a one-to-many dependency. It is common in event systems, news feeds and GUI updates.

In C++ a Subject class holds a list of Observer pointers. attach(o) adds an observer; notify(msg) calls update(msg) on each. Observers derive from an abstract Observer class so different observer types can attach to the same subject.

attach and notify

Inside the Subject class a std::vector<Observer*> is kept. The attach(Observer* o) method adds a pointer to this list. The notify(const std::string& msg) method calls update(msg) on every observer in the list.

Example: subject.attach(&observer); attaches an observer. Then subject.notify("Hello"); runs update on each observer. Even if more observers are added, the subject notifies them all with the same notify code. Adding a new observer type is easy: just write a new Observer subclass.

Practice Write a Subject and Observer

Define an abstract Observer with virtual void update(const std::string& msg) = 0;. A PrintObserver subclass prints std::cout << msg; in update. A Subject class holds a std::vector<Observer*> and has attach(Observer*) and notify(const std::string&) methods. In main create a Subject and a PrintObserver, attach it, then call notify("Hello").

Starter code

main.cpp
#include <iostream>
#include <vector>
#include <string>

// Define the Observer abstract class and PrintObserver class
// Define the Subject class

int main() {
    // Create a Subject and PrintObserver, attach it, call notify
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <vector>
#include <string>

class Observer {
public:
    virtual ~Observer() = default;
    virtual void update(const std::string& msg) = 0;
};

class PrintObserver : public Observer {
public:
    void update(const std::string& msg) override {
        std::cout << msg;
    }
};

class Subject {
    std::vector<Observer*> observers;
public:
    void attach(Observer* o) { observers.push_back(o); }
    void notify(const std::string& msg) {
        for (Observer* o : observers) o->update(msg);
    }
};

int main() {
    Subject s;
    PrintObserver p;
    s.attach(&p);
    s.notify("Hello");
    return 0;
}

Expected output

Hello

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Match each piece to its role

Term Meaning
class Observer Abstract observer
class Subject The notifier
attach(o) Adds an observer
notify(msg) Notifies all observers

2 Complete the notify call

Question
subject.____("Hello");
Answer
subject.notify("Hello");

Explanation: notify tells all observers.

3 What does the notify(msg) method do?

  • Empties the list
  • Calls update on each observer Correct answer
  • Changes the subject state
  • Creates a new observer

Explanation: notify calls the update method on each observer in the list.

All lessons in Advanced