C++設計模式淺識觀察者模式

來源:互聯網
上載者:User
觀察者模式(Observer):定義了一種一對多的依賴關係,讓多個觀察者對象同時監聽某一個主體對象。這個主題對象在狀態發生變化時,會通知所有觀察者對象,使它們能夠自動更新自己。

模式實現:

[code]//觀察者抽象基類class Observer{public:    virtual void Update(int) = 0;};//主題、目標class Subject{public:    virtual void Attach(Observer *) = 0;  //重視    virtual void Detach(Observer *) = 0; //脫離    virtual void Notify() = 0; //通知};class ConcreateObserver: public Observer{private:    Subject *m_pSubject;   //1.具體觀察者,維護一個指向ConcreteSubject對象的引用2.儲存有關狀態,這些狀態應與目標的狀態保持一致public:                     //3.實現Observer的更新介面以使自身狀態與目標的狀態保持一致    ConcreateObserver(Subject *pSubject): m_pSubject(pSubject){}    void Update(int value){        std::cout << "ConcreateObserver get the update. New State: " << value << std::endl;    }};class ConcreateObserver2: public Observer{private:    Subject *m_pSubject;public:    ConcreateObserver2(Subject *pSubject):m_pSubject(pSubject){}    void Update(int value){        std::cout << "ConcreateObserver2 get the update. New State: " << value << std::endl;    }};//1.將有關狀態存入各ConcreateObserver對象//2.當它的狀態發生改變時,向它的各個觀察者發出通知class ConcreateSubject: public Subject{private:    std::list<Observer *> m_ObserverList;    int m_iState;public:    void Attach(Observer *pObserver);    void Detach(Observer *pObserver);    void Notify();    void setState(int state){        m_iState = state;    }};void ConcreateSubject::Attach(Observer *pObserver){    m_ObserverList.push_back(pObserver);}void ConcreateSubject::Detach(Observer *pObserver){    m_ObserverList.remove(pObserver);}void ConcreateSubject::Notify(){    std::list<Observer *>::iterator it = m_ObserverList.begin();    while(it != m_ObserverList.end()){        (*it)->Update(m_iState);        ++it;    }}

用戶端:

[code]int main(){    //Create Subject    ConcreateSubject *pSubject = new ConcreateSubject();    //Create Observer    Observer *pObserver = new ConcreateObserver(pSubject);    Observer *pObserver2 = new ConcreateObserver2(pSubject);    //Change the state    pSubject->setState(2);    //Register the observer    pSubject->Attach(pObserver);    pSubject->Attach(pObserver2);    pSubject->Notify();  //Output: ConcreateObserver get the update. New State: 2                                  //ConcreateObserver2 get the update. New State: 2    //Unregister the observer    pSubject->Detach(pObserver);    pSubject->setState(3);    pSubject->Notify();  //Output: ConcreateObserver2 get the update. New State: 3    delete pObserver;    delete pObserver2;    delete pSubject;}

以上就是C++設計模式淺識觀察者模式的內容,更多相關內容請關注topic.alibabacloud.com(www.php.cn)!

  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.