Observer design pattern in C++

前端 未结 7 953
礼貌的吻别
礼貌的吻别 2020-12-06 03:00

Is the observer design pattern already defined in STL (Like the java.util.Observer and java.util.Observable in Java) ?

相关标签:
7条回答
  • 2020-12-06 03:56
    #include <iostream>
    #include <string>
    #include <set>
    using namespace std;
    class Subject;
    class Observer {
    public:
      virtual void update(Subject & subject) = 0;
    };
    // also knows as Observable in literature
    class Subject
    {
      string state;
      set<Observer*> observers;
    public:
      void attachObserver(Observer *o) { observers.insert(o); }
      void detachObserver(Observer *o) { observers.erase(o); }
      void notifyObservers()
      {
        for (auto &o : observers)
        {
          o->update(*this);
        }
      }
      string getState() { return state; }
      void changeState(const string & s)
      {
        state = s;
        notifyObservers();
      }
    };
    class ObserverImpl : public Observer
    {
      string state;
    public:
      void update(Subject & sbj) override
      {
        state = sbj.getState();
      }
      string getState() { return state; }
    };
    int main()
    {
      ObserverImpl a, b, c;
      Subject subject;
      subject.attachObserver(&a);
      subject.attachObserver(&b);
      subject.attachObserver(&c);
      subject.changeState("Observer pattern");
      cout << a.getState() << endl;
      cout << b.getState() << endl;
      cout << c.getState() << endl;
      return 0;
    }
    

    please also see UML/flow diagrams http://codepatterns.ddns.net/

    0 讨论(0)
提交回复
热议问题