Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Observer: Event Notification | Behavioral Patterns
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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Can you give an example of how the Observer pattern is implemented in code?

What are some real-world scenarios where the Observer pattern is commonly used?

How does the Observer pattern differ from other behavioral design patterns?

Awesome!

Completion rate improved to 10

bookObserver: Event Notification

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1
some-alt