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;
}