Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

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

Deslize para mostrar o 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

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 1
some-alt