Observer is perfectly separated from the observer and observed object. For example, the user interface can be used as an observer, and the business data is observed by the observer, the user interface observes the change of business data. After discovering the data, it is displayed on the interface. One principle for object-oriented design is: Each class in the system will focus on a certain feature, not other aspects. A object only does one thing and do it well. The observer mode has defined clear boundaries between the modules and improves the maintenanceability and reuse of the application. The observer model has many implementations, fundamentally, the mode must contain two roles: observers and observed objects. In the examples just now, business data is observed, and the user interface is an observer. The observer and the logic association of "observation" between the observer, when the observer changes, the observer will observe such a change and make a corresponding response. If such a viewing process is used between service data, it is possible to ensure that the interface and data are divided, assuming that the demand of the application changes, and needs to modify the performance of the interface, just re-build a user interface, business data There is no need to change. "Observation" is not "directly call" to achieve the observer mode, it is to note that the interactive relationship between the observer and the observed object cannot reflect the direct call between classifications, otherwise it will enable the observer and observed objects. Closely coupled, fundamentally violates the principles of object-oriented design. Whether it is the observer "observation" observation object, or the observer will change his own change "notice" observer, it should not be called directly. Examples of achieving the observer model Realize the observer models have a lot of forms, a relatively intuitive type in the form of "registration-notification-revoking registration". The following three graphs describe such a process: 1: Observer registers ourselves into the object, and stores the observer in a container (container). 2: A variation has occurred (asKpriceChanged) in the figure, and all registered observes are obtained from the container to notify the observer. 3: The observer told the observer to revoke the observation and the observer removed the observer from the container. The observer will be registered with the observer's container, and the observer should not ask the observer's specific type, but should use the observer's interface. Such an advantage is that there are other observes in the program, so as long as this observer is also the same interface implementation. An observer can correspond to multiple observes, when the observer changes, he can notify all the observer one by one. Based on the interface, not a specific implementation - this provides greater flexibility for the program. The following code is an example of using C # implement the observer mode:
// "observer" Interfaces public interface IObserver {void Notify (object anObject);} // "observed object" Interfaces public interface IObservable {void Register (IObserver anObserver); void UnRegister (IObserver anObserver);} observer and Observing objects are implemented from both interfaces, all of which are defined by these two interfaces, not specific implementations. So the observer and the observed object are not bound together. We can easily change the observer and any part of the observed object without affecting other parts. The specific observable object is achieved below. The following class is a base class that is observed to achieve all methods that must be observed. We use a HashTable as the observer's container. Code as follows: // All the observed object base class public class ObservableImpl: IObservable {// save the observation target container protected Hashtable _observerContainer = new Hashtable (); // Register the observer public void Register (IObserver anObserver) {_observerContainer.Add (anObserver, anObserver);} // deregistration public void UnRegister (IObserver anObserver) {_observerContainer.Remove (anObserver);} // event notification observer public void NotifyObservers (object anObject) {// enumeration observation vessel If you do an event one by one, the class is notified to be the observed object to be implemented, but all the observer's base class. Among them, all the functions of all observations are implemented. This class can be simply defined as Abstract, so that the programmer cannot create its examples. Interfaces and virtual classes that implement this interface maintain both loose coupling between classes, but also make multiple specific implementations can use the same function. The observer mode is finally implemented, using user interface - business data as an example: