C + + design pattern shallow observer pattern

Source: Internet
Author: User
Observer Mode (OBSERVER): Defines a one-to-many dependency that allows multiple observer objects to listen to a principal object at the same time. This subject object notifies all observer objects when the state changes, enabling them to automatically update themselves.

Pattern implementation:

[code]//Observer abstract base class class observer{public:virtual void Update (int) = 0;};/  /subject, Target class subject{public:virtual void Attach (Observer *) = 0; Value virtual void Detach (Observer *) = 0; Out of virtual void Notify () = 0;   Notice};class concreateobserver:public observer{private:subject *m_psubject; 1. The specific observer, maintains a reference to the ConcreteSubject object 2. Stores the state, which should be consistent with the state of the target public://3. Implements the update interface of the observer to keep its state and the state of the target To Concreateobserver (Subject *psubject): M_psubject (psubject) {} void Update (int value) {std::cout << "Co Ncreateobserver get the update.    New state: "<< value << Std::endl; }};class concreateobserver2:public observer{private:subject *m_psubject;public:concreateobserver2 (Subject *pSubje CT): M_psubject (psubject) {} void Update (int value) {std::cout << ConcreateObserver2 get the update.    New state: "<< value << Std::endl; }};//1. The status is deposited into each Concreateobserver object//2. When its state changes, it notifies its various observers of 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::D etach (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; }}

Client:

[Code]int Main () {    //create Subject    concreatesubject *psubject = new Concreatesubject ();    Create Observer    Observer *pobserver = new Concreateobserver (psubject);    Observer *pobserver2 = new ConcreateObserver2 (psubject);    Change of 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;}

The above is the C + + design mode of shallow knowledge of the content of the observer mode, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.