I read the Java2 tutorial (fifth edition) again. This book is of a big and comprehensive type, so many important things are mentioned, but it is not detailed enough, so that some important knowledge points are easily ignored. no more nonsense.
Observer observer, interface, an update Method
Observable, Class
An example of the observer interface is as follows:
Public interface observer {
Void Update (observable o, object Arg); // The second parameter is similar to the function of the attachment.
}
The observable class provides the following key methods:
Public synchronized void addobserver (Observer O); // Add an observer to observe himself;
// This method changes the state of the observer. If the state of the observer is not changed, the observer will not perform any operations even if the yyobservers method is executed;
// If the status of the observer changes and the yyobservers method is executed, the status of the observer will be restored.
// Because it is a protected method, if you want to call it publicly, you must overwrite the setchanged method in the subclass and change it to public.
Protected synchronized void setchanged ();
// After the observer state changes, use one of the following two methods. If you want to transmit information to the observer, use the second method. If not, use the first method.
// Object Arg will be uploaded to the second parameter of the update (observable o, object Arg) method of the observer object.
// Actually, the changed flag of observable is true. Then, the last two methods can call the update () method of all observers.
Public void policyobservers ();
Public void policyobservers (Object Arg );
In fact, it is not appropriate to use the observer and the observer to describe the relationship between the two, because, throughout the process, the observer is always the active side, the observer is indeed a passive party and can discard the observer at any time. isn't it strange that you will take the initiative to find someone to monitor yourself?
InProgramMedium, usually
An observeable object corresponds to multiple observer objects, 1: N.
The observeable object is the active side, because it is the observeable object that actively adds the observer object and prompts them to be able (Sequence) Execute the update () method.
It is actually a function call of an object to another batch of objects, but using this mode can reduce the numberCodeTo reduce coupling.