Observer: 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
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.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Awesome!
Completion rate improved to 10
Observer: Event Notification
Desliza para mostrar el menú
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
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.
¡Gracias por tus comentarios!