The following code uses C # To implement the observer mode:
// "Observer" Interface
Public interface IObserver {
Void Notify (object anObject );
}
// "Observed object" Interface
Public interface IObservable {
Void Register (IObserver anObserver );
Void UnRegister (IObserver anObserver );
}
Both the observer and the observed object are implemented from these two interfaces. All operations are defined by these two interfaces, rather than the specific implementation. So the observer and the object to be observed are not bound together. We can easily change the observer and any part of the observed object without affecting other parts.
The following describes the objects to be observed. The following class is the base class of all observed objects and implements the required methods for all observed objects. We use a Hashtable container as the observer. The Code is as follows:
// All base classes of the observed object
Public class ObservableImpl: IObservable {
// Save the container of the observed object
Protected Hashtable _ observerContainer = new Hashtable ();
// Register the observer
Public void Register (IObserver anObserver ){
_ ObserverContainer. Add (anObserver, anObserver );
}
// Cancel registration
Public void UnRegister (IObserver anObserver ){
_ ObserverContainer. Remove (anObserver );
}
// Notify the observer of the event
Public void policyobservers (object anObject ){
// Enumerate the observers in the container and notify them of the events one by one
Foreach (IObserver anObserver in _ observerContainer. Keys ){
AnObserver. Notify (anObject );
}
}
}
The above class is not the final object to be observed, but the base class of all the objects to be observed, which implements the functions shared by all the observed objects. This class can be simply defined as abstract, so that programmers cannot create their instances. Interfaces and virtual classes that implement these interfaces not only maintain loose coupling between classes, but also allow multiple specific implementations to use the same functions.
The following is an example of implementing the observer mode:
// Business data (observed object)
Public class SomeData: ObservableImpl {
// The data in the Observer
Float m_fSomeValue;
// Change data attributes
Public float SomeValue {
Set {
M_fSomeValue = value;
Base. NotifyObservers (m_fSomeValue); // notify the observer of the changed message.
}
}
}
// User Interface (observer)
Public class SomeKindOfUI: IObserver {
Public void Notify (object anObject ){
Console. WriteLine ("The new value is:" + anObject );
}
}
// Actual call Process
Public class MainClass {
Public static void Main (){
// Create the observer and the observer
SomeKindOfUI ui = new SomeKindOfUI ();
SomeData data = new SomeData ();
// Register the observer in the observed object
Data. Register (ui );
// Change the data in the observed object. Then, the observed object will be notified to the observer.
Data. SomeValue = 1000f;
// Unregister the observer and stop the observer
Stock. UnRegister (stockDisplay );
}
}
. NET
The above form is that we have implemented the observer mode in the most basic way, and we have developed a specific type for the observer mode. In the. NET Framework, using proxies and events can better implement the observer mode. C # Introduction to middleware and events can be found in this article: in C #, the event is triggered by proxy, which contains a detailed description of proxy and event, as well as routines, I will not talk about it here. Other languages supported by. NET also have their own implementation methods.
In the event mode, the class that declares the event is the observer. The observer does not need to register the observer, but only needs to publish an event without performing any operation. The observer does not need to register himself to the observed object. Instead, he wants to create an instance for a specific proxy and bind the proxy to a method. Register or revoke the observer's observation on the object in this way. By carefully studying the proxy and event modes, it is not difficult to find that the IObserver and IObservable interfaces can reduce the coupling between the observer and the observed object, proxy and event almost eliminate the coupling between the two modules, improving flexibility a lot.