The observer pattern is a pattern similar to message distribution, a useful design pattern for scenarios where a task needs to be monitored by multiple objects, or when a member object needs to reverse-notify the class object.
Here to the example of the big talk design pattern, office staff A, B, c in watching the stock watch movie, then the boss came back, was a, B, C paid bribes after the front desk mm issued a notice to a, B, C,a, B, C received notice hurriedly shut down the computer, close the stock window, pretending to work.
1. Observer interface
Class Iobserver{public:virtual~iobserver () {}virtualvoidonevent (tstring tstrevent) = 0;};
2. Specific Observer class
Class Cobserver:public Iobserver{public:cobserver (tstring tstrobserver): M_tstrname (tstrobserver) {}virtual~ Cobserver () {}virtualvoidonevent (tstring tstrevent) {_tprintf (_t ("%s get%s event!! \ n "), M_tstrname.c_str (), Tstrevent.c_str ());} Private:tstringm_tstrname;};
3. The Observer class, which is inherited by the observed object, has an interface to add and remove observer objects, and notifies all the observer objects in the list when the notification is received
Class Cbase_observeobject{public:cbase_observeobject () {}virtual~cbase_observeobject () {}booladdobserver ( iobserver* pocobserver) {if (pocobserver) {m_listobserver.push_back (pocobserver); returntrue;} Returnfalse;} Voiddelobserver (iobserver* pocobserver) {m_listobserver.remove (pocobserver);} Intnotify (tstringtstrevent) {if (!m_listobserver.empty ()) {Std::list<iobserver*>::iterator itObserer;for ( Itobserer = M_listobserver.begin (); Itobserer! = M_listobserver.end (); itobserer++) {iobserver* poobserver = *itobserer;poobserver->onevent (tstrevent);}} Returnm_listobserver.size ();} Private:std::list<iobserver*> M_listobserver;};
4, the object of the Observer, issued an event notification
Class Cboss:public Cbase_observeobject{public:voidcome () {_tprintf ("Boss:ha Ha, Boys, I come in\n"); Notify (_t ("Boss Come");}; Voidleave () {_tprintf ("Boss:good Bye, boys\n"); Notify (_t ("Boss Leave"));};
5. Examples of Use
void Observetest () {CObserveroCOberver1 (_t ("Viki")), OCOberver2 (_t ("Jim")); Cbossocboss;ocboss.addobserver (&ocoberver1); Ocboss.addobserver (&ocoberver2); OCBoss.Come ();// Delete the OCOberver1 Observer object, when the boss left, OCOberver1 will not receive notice ocboss.delobserver (&ocoberver1); Ocboss.leave ();}
The results of the operation are as follows:
We chat in QQ, sometimes hang a QQ on the computer, and then hang one on the phone, when we receive a friend message, our mobile phone and QQ on the computer will also receive the same message notification, which is actually the typical use of the observer pattern, at this time the message is an observer of the notification event, And the mobile phone and the computer side is an observer object, of course, the situation here is slightly more complex, need to match the message sender and receiver of the object, decide which objects need to notify, otherwise a person and sister chat with enthusiasm, then unaware in the live broadcast.
Big talk design mode C + +-viewer mode