Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Observer: Event Notification | Behavioral Patterns
Quizzes & Challenges
Quizzes
Challenges
/
C++ Design Patterns

bookObserver: Event Notification

The Observer pattern is a behavioral design pattern that establishes a one-to-many relationship between objects so that when one object (the subject) changes its state, all its dependent objects (the observers) are automatically notified.

This pattern promotes loose coupling, allowing the subject and observers to interact without being tightly dependent on each other. It’s ideal for event-driven systems where changes in one component should trigger automatic updates in others.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
#include <iostream> #include <vector> #include <string> #include <memory> // Observer interface class Observer { public: virtual void update(const std::string& message) = 0; virtual ~Observer() = default; }; // Concrete Observer class User : public Observer { std::string name; public: User(const std::string& n) : name(n) {} void update(const std::string& message) override { std::cout << name << " received: " << message << std::endl; } }; // Subject class class NotificationCenter { std::vector<std::shared_ptr<Observer>> observers; public: void subscribe(std::shared_ptr<Observer> obs) { observers.push_back(obs); } void notifyAll(const std::string& message) { for (auto& obs : observers) obs->update(message); } }; int main() { auto user1 = std::make_shared<User>("Alice"); auto user2 = std::make_shared<User>("Bob"); NotificationCenter center; center.subscribe(user1); center.subscribe(user2); center.notifyAll("New event: Observer pattern demo!"); }

By defining a common Observer interface, each observer reacts to updates in its own way when notified by the Subject. This decouples the sender from the receivers, making the system more flexible and easier to extend.

Whenever an event occurs, the Subject broadcasts notifications to all registered observers, ensuring that every component stays synchronized automatically. This approach is particularly useful in GUI applications, messaging systems, and real-time event handling.

question mark

What is the main purpose of the Observer design pattern?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 10

bookObserver: Event Notification

Swipe to show menu

The Observer pattern is a behavioral design pattern that establishes a one-to-many relationship between objects so that when one object (the subject) changes its state, all its dependent objects (the observers) are automatically notified.

This pattern promotes loose coupling, allowing the subject and observers to interact without being tightly dependent on each other. It’s ideal for event-driven systems where changes in one component should trigger automatic updates in others.

main.cpp

main.cpp

copy
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
#include <iostream> #include <vector> #include <string> #include <memory> // Observer interface class Observer { public: virtual void update(const std::string& message) = 0; virtual ~Observer() = default; }; // Concrete Observer class User : public Observer { std::string name; public: User(const std::string& n) : name(n) {} void update(const std::string& message) override { std::cout << name << " received: " << message << std::endl; } }; // Subject class class NotificationCenter { std::vector<std::shared_ptr<Observer>> observers; public: void subscribe(std::shared_ptr<Observer> obs) { observers.push_back(obs); } void notifyAll(const std::string& message) { for (auto& obs : observers) obs->update(message); } }; int main() { auto user1 = std::make_shared<User>("Alice"); auto user2 = std::make_shared<User>("Bob"); NotificationCenter center; center.subscribe(user1); center.subscribe(user2); center.notifyAll("New event: Observer pattern demo!"); }

By defining a common Observer interface, each observer reacts to updates in its own way when notified by the Subject. This decouples the sender from the receivers, making the system more flexible and easier to extend.

Whenever an event occurs, the Subject broadcasts notifications to all registered observers, ensuring that every component stays synchronized automatically. This approach is particularly useful in GUI applications, messaging systems, and real-time event handling.

question mark

What is the main purpose of the Observer design pattern?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1
some-alt