16. 1. Explanation
Concept: defines a one-to-many dependency between objects. When the State of an object changes, all objects dependent on it are notified and updated automatically.
Main (),
IObservable, observer Interface
CHanFeiZiObservable, observed by Han Feizi
IObserver, observer Interface
CLiSiObserver, observer Li Si
CZhouSiObserver observer Zhou Si
Note: The observer is gathered by Han Feizi, and every action of Han Feizi is sent to the observer, such as Li Si or Zhou Si.
Note: a maximum of one object can be an observer and an observer. Like triggers in databases, it is difficult to maintain a complex chain. The observer is similar to the delegate processing method.
// IObservable. h
# Pragma once
# Include "IObserver. h"
# Include <iostream>
Using std: string;
Class IObservable
{
Public:
IObservable (void)
{
}
Virtual ~ IObservable (void)
{
}
Virtual void AddObserver (IObserver * pObserver) = 0;
Virtual void DeleteObserver (IObserver * pObserver) = 0;
Virtual void policyobservers (string context) = 0;
};
// HanFeiziObservable. h
# Pragma once
# Include "iobservable. h"
# Include "IObserver. h"
# Include <vector>
Using std: vector;
Class CHanFeiziObservable:
Public IObservable
{
Public:
CHanFeiziObservable (void );
~ CHanFeiziObservable (void );
Void AddObserver (IObserver * pObserver );
Void DeleteObserver (IObserver * pObserver );
Void policyobservers (string context );
Void HaveBreakfast ();
Void HaveFun ();
Private:
Vector <IObserver *> m_observerList;
Typedef vector <IObserver *>: const_iterator ObserverList_C_iterator;
};
// HanFeiziObservable. cpp
# Include "StdAfx. h"
# Include "HanFeiziObservable. h"
# Include <iostream>
Using std: string;
Using std: cout;
Using std: endl;
CHanFeiziObservable: CHanFeiziObservable (void)
{
}
CHanFeiziObservable ::~ CHanFeiziObservable (void)
{
}
Void CHanFeiziObservable: AddObserver (IObserver * pObserver)
{
M_observerList.push_back (pObserver );
}
Void CHanFeiziObservable: DeleteObserver (IObserver * pObserver)
{
ObserverList_C_iterator it = m_observerList.begin ();
For (; it! = M_observerList.end (); it ++)
{
String name = (* it)-> GetName ();
If (name. compare (pObserver-> GetName () = 0)
{
// Delete is found.
}
}
}
Void CHanFeiziObservable: NotifyObservers (string context)
{
ObserverList_C_iterator it = m_observerList.begin ();
For (; it! = M_observerList.end (); it ++)
{
(* It)-> Update (context );
}
}
Void CHanFeiziObservable: HaveBreakfast ()
{
Cout <"Han Feizi: I started eating..." <endl;
This-> policyobservers ("Han Feizi eating ");
}
Void CHanFeiziObservable: HaveFun ()
{
Cout <"Han Feizi: entertainment..." <endl;
This-> policyobservers ("Han Feizi in entertainment ");
}
// IObserver. h
# Pragma once
# Include <iostream>
Using std: string;
Class IObserver
{
Public:
IObserver (string _ name)
{
This-> m_name = _ name;
}
Virtual ~ IObserver (void)
{
}
Virtual void Update (string context) = 0;
Virtual string GetName () = 0; // a function added separately to c ++ to search for the observer when deleting the function.
Protected:
String m_name;
};
// LiSiObserver. h
# Pragma once
# Include "iobserver. h"
# Include <iostream>
Using std: string;
Class CLiSiObserver:
Public IObserver
{
Public:
CLiSiObserver (void );
~ CLiSiObserver (void );
Void Update (string context );
String GetName ();
Private:
Void ReportToQinShiHuang (string report );
};
// LiSiObserver. cpp
# Include "StdAfx. h"
# Include "LiSiObserver. h"
# Include <iostream>
Using std: cout;
Using std: endl;
Using std: string;
CLiSiObserver: CLiSiObserver (void): IObserver ("LITH ")
{
}
CLiSiObserver ::~ CLiSiObserver (void)
{
}
Void CLiSiObserver: Update (string context)
{
Cout <"Li Si: after observing the Han Feizi activity, he began to report to the boss..." <endl;
This-> ReportToQinShiHuang (context );
Cout <"Li Si: After the report is completed, Qin boss rewarded him with two radishes..." <endl;
}
Void CLiSiObserver: ReportToQinShiHuang (string report)
{
Cout <"lies: Report, Qin boss! Han Feizi has an activity ---> "<report. c_str () <endl;
}
String CLiSiObserver: GetName ()
{
Return m_name;
}
// ZhouSiObserver. h
# Pragma once
# Include "iobserver. h"
# Include <iostream>
Using std: string;
Class CZhouSiObserver:
Public IObserver
{
Public:
CZhouSiObserver (void );
~ CZhouSiObserver (void );
Void Update (string context );
String GetName ();
Private:
Void Cry (string report );
};
// ZhouSiObserver. cpp
# Include "StdAfx. h"
# Include "ZhouSiObserver. h"
# Include <iostream>
Using std: cout;
Using std: endl;
Using std: string;
CZhouSiObserver: CZhouSiObserver (void): IObserver ("Zhou Si ")
{
}
CZhouSiObserver ::~ CZhouSiObserver (void)
{
}
Void CZhouSiObserver: Update (string context)
{
Cout <"Zhou Si: after observing Han Feizi activity, he also started activity..." <endl;
This-> Cry (context );
Cout <"Zhou Si: It's really crying..." <endl;
}
Void CZhouSiObserver: Cry (string report)
{
Cout <"Zhou Si: It's because" <report. c_str () <", ---- so I'm sad! "<Endl;
}
String CZhouSiObserver: GetName ()
{
Return m_name;
}
// Observer. cpp
# Include "stdafx. h"
# Include "HanFeiZi. h"
# Include "LiSi. h"
# Include "HanFeiZiNew. h"
# Include "HanFeiziObservable. h"
# Include "LiSiObserver. h"
# Include "ZhouSiObserver. h"
# Include <iostream>
Using std: cout;
Using std: endl;
Using std: string;
Void DoNew ()
{
// IHanFeiZi. h, HanFeiZiNew. h, ILiSi. h, LiSi. h
// Cout <"---------- use a new method to try ----------" <endl;
// CHanFeiZiNew hanfeizi;
// Hanfeizi. HaveBreakfast ();
// Hanfeizi. HaveFun ();
}
Void DoNewNew ()
{
// IObservable. h, HanfeiziObservable. h, IObserver. h, LiSiObserver. h
Cout <"---------- use the update method to try again ----------" <endl;
IObserver * pLiSi = new CLiSiObserver ();
IObserver * pZhouSi = new CZhouSiObserver ();
CHanFeiziObservable * pHanFeiZi = new CHanFeiziObservable ();
PHanFeiZi-> AddObserver (pLiSi );
PHanFeiZi-> AddObserver (pZhouSi );
PHanFeiZi-> HaveBreakfast ();
Delete pLiSi;
PLiSi = NULL;
Delete pHanFeiZi;
PHanFeiZi = NULL;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
// Compare the original method and observe it with a thread.
// DoIt ();
// Aggregate the LITH class to the Han Feizi class. In this case, the coupling degree is too high and it is still in a more abstract way.
DoNew ();
// In a more abstract way, if there are more people who want to observe Han Feizi, it is impossible to allow only Li Si to observe.
DoNewNew ();
_ CrtSetDbgFlag (_ CRTDBG_LEAK_CHECK_DF | _ CRTDBG_ALLOC_MEM_DF );
_ CrtDumpMemoryLeaks ();
Return 0;
}
The observer mode is a behavior mode.