C++ observer mode of design mode 19 (including sample code)

Observer mode is a behavioral design pattern, which defines a one-to-many dependency, allowing multiple observer objects to monitor a subject object at the same time, when the subject object state changes, it will notify all observers objects, enabling them to update themselves automatically.

The following is a sample code to implement the Observer pattern in C++:

#include <iostream>
#include <list>
#include <string>
// 抽象主题类:定义了添加、删除和通知观察者的接口
class Subject {
    
    
public:
    virtual ~Subject() {
    
    }
    virtual void Attach(Observer* observer) = 0;
    virtual void Detach(Observer* observer) = 0;
    virtual void Notify() = 0;
};
// 抽象观察者类:定义了更新的接口
class Observer {
    
    
public:
    virtual ~Observer() {
    
    }
    virtual void Update(Subject* subject) = 0;
};
// 具体主题类:维护观察者列表,实现添加、删除和通知观察者的接口
class ConcreteSubject : public Subject {
    
    
public:
    virtual ~ConcreteSubject() {
    
    }
    virtual void Attach(Observer* observer) override {
    
    
        observers_.push_back(observer);
    }
    virtual void Detach(Observer* observer) override {
    
    
        observers_.remove(observer);
    }
    virtual void Notify() override {
    
    
        for (auto observer : observers_) {
    
    
            observer->Update(this);
        }
    }
    void SetState(const std::string& state) {
    
    
        state_ = state;
    }
    const std::string& GetState() const {
    
    
        return state_;
    }
private:
    std::list<Observer*> observers_;
    std::string state_;
};
// 具体观察者类:实现更新的接口
class ConcreteObserver : public Observer {
    
    
public:
    ConcreteObserver(const std::string& name) : name_(name) {
    
    }
    virtual ~ConcreteObserver() {
    
    }
    virtual void Update(Subject* subject) override {
    
    
        ConcreteSubject* concrete_subject = dynamic_cast<ConcreteSubject*>(subject);
        if (concrete_subject) {
    
    
            std::cout << name_ << " received the update: " << concrete_subject->GetState() << std::endl;
        }
    }
private:
    std::string name_;
};
int main() {
    
    
    // 创建具体主题对象
    ConcreteSubject subject;
    // 创建具体观察者对象
    ConcreteObserver observer1("Observer 1");
    ConcreteObserver observer2("Observer 2");
    ConcreteObserver observer3("Observer 3");
    // 添加观察者
    subject.Attach(&observer1);
    subject.Attach(&observer2);
    subject.Attach(&observer3);
    // 修改状态并通知观察者
    subject.SetState("State 1");
    subject.Notify();
    // 移除观察者
    subject.Detach(&observer2);
    // 修改状态并通知观察者
    subject.SetState("State 2");
    subject.Notify();
    return 0;
}

In the above code, we first define an abstract subject class Subject and an abstract observer class Observer, respectively define the interfaces for adding, deleting and notifying observers, and the interface for updating. Next, we created a specific subject class ConcreteSubject, which maintains a list of observers, and implements the interface of adding, deleting and notifying observers.

At the same time, we also define a specific observer class ConcreteObserver, which implements the updated interface. In the main() function, we create a specific subject object subject and three specific observer objects observer1, observer2 and observer3, and add the observers to the subject object. Next, we modify the state of the subject object and notify the observers. Finally, we remove an observer and again modify the state of the subject object and notify the observer.

The advantage of the observer pattern is that it decouples the subject object and the observer object so that they can vary independently. At the same time, the observer mode also provides a one-to-many communication mechanism, making the system more flexible. However, the observer pattern may lead to a large number of observer objects, which affects the performance of the system.

Guess you like

Origin blog.csdn.net/dica54dica/article/details/130021515