Design Pattern learning-Observer Pattern

Source: Internet
Author: User

Observer Mode

GOOD: defines a one-to-many relationship, allowing multiple observer objects (company employees) to listen to a topic object (Secretary) at the same time. When the status of the topic object changes, all observers are notified so that they can update themselves.

 

Example:

# Include <string>
# Include <iostream>
# Include <vector>
Using namespace std;

Class Secretary;
// View the shares of colleagues (observer)
Class StockObserver
{
Private:
String name;
Secretary * sub;
Public:
StockObserver (string strname, Secretary * strsub)
{
Name = strname;
Sub = strsub;
}
Void Update ();
};
// Category (subject object, notifier)
Class Secretary
{
Private:
Vector <StockObserver> observers;
Public:
String action;
Void Add (StockObserver ob)
{
Observers. push_back (ob );
}
Void Notify ()
{
Vector <StockObserver>: iterator p = observers. begin ();
While (p! = Observers. end ())
{
(* P). Update ();
P ++;
}
}
};

Void StockObserver: Update ()
{
Cout <name <":" <sub-> action <". Don't play with stocks. You have to start working." <endl;
}

// Client
Int main ()
{
Secretary * p = new Secretary (); // create a notifier

// Observer
StockObserver * s1 = new StockObserver ("Xiao Li", p );
StockObserver * s2 = new StockObserver ("Xiao Zhao", p );
// Add to notification queue
P-> Add (* s1 );
P-> Add (* s2 );
// Event
P-> action = "the boss is here ";
// Notification
P-> Policy ();
Return 0;
}

 

# Include <string>
# Include <iostream>
# Include <vector>
Using namespace std;

Class SecretaryBase;
// Abstract observer
Class CObserverBase
{
Protected:
String name;
SecretaryBase * sub;
Public:
CObserverBase (string strname, SecretaryBase * strsub)
{
Name = strname;
Sub = strsub;
}
Virtual void Update () = 0;
};
// Specific observer to view the stock
Class StockObserver: public CObserverBase
{
Public:
StockObserver (string strname, SecretaryBase * strsub): CObserverBase (strname, strsub)
{
}
Virtual void Update ();
};

// Specific observer, watching the NBA
Class NBAObserver: public CObserverBase
{
Public:
NBAObserver (string strname, SecretaryBase * strsub): CObserverBase (strname, strsub ){}
Virtual void Update ();
};


// Abstract notification recipient
Class SecretaryBase
{
Public:
String action;
Vector <CObserverBase *> observers;
Public:
Virtual void Attach (CObserverBase * observer) = 0;
Virtual void required y () = 0;
};

// Specific notification recipient
Class Secretary: public SecretaryBase
{
Public:
Void Attach (CObserverBase * ob)
{
Observers. push_back (ob );
}
Void Notify ()
{
Vector <CObserverBase *>: iterator p = observers. begin ();
While (p! = Observers. end ())
{
(* P)-> Update ();
P ++;
}
}
};

Void StockObserver: Update ()
{
Cout <name <":" <sub-> action <". Don't play with stocks. You have to start working." <endl;
}
Void NBAObserver: Update ()
{
Cout <name <":" <sub-> action <". Don't watch the NBA. The boss is here." <endl;
}


// Client:
Int main ()
{
SecretaryBase * p = new Secretary (); // create an observer

// Object to be observed
CObserverBase * s1 = new NBAObserver ("Xiao Li", p );
CObserverBase * s2 = new StockObserver ("Xiao Zhao", p );
// Add to observation queue
P-> Attach (s1 );
P-> Attach (s2 );
// Event
P-> action = "the boss is here ";
// Notification
P-> Policy ();

Return 0;
}

 

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.