Home » Java » The design pattern of Java (4) - observer pattern

The design pattern of Java (4) - observer pattern

Today,

looked at the observer pattern, generally speaking is not difficult, the key is to know the principle and usage of this pattern is my own personal understanding of
.
1, the definition of

The observer pattern, first of all there is the observer, one or more observers, they are dependent on another object, that is to say, these observers are watching a observer. Then, when the observed changes state, changes need to inform all observers that it this is the state of being observed, the observer pattern
.
This understanding is not well understood, a simple example is the fire broadcast industry, an anchor, a lot of fans are concerned about the anchor, anchor live on Ta, all the fans can receive notice: your attention has been put on the anchor. This is the best mode of observation are examples the anchor is observed, fans is the observer.


2,
code demo
Observer:


public class {Observer
Public void update (Observable o) {
The method call / / state is observed when the observer change notification
}
}

observed


public class Observable{
List observers; / / all observers
The observer / / add
Public void addObserver (Observer o) {
Observers.add (o)
}
Public void (changed) {
/ / the observed state change
Call / notice
(notifyObserver);
}
Notice / / all observer has changed
Public void (notifyObserver) {
For (int i=0; I < observers.size; (i++)) {
Observers.get (I).Update (this)
}
}
}

can be seen, the observer pattern of the core is that the observed in state changes (changed ()), immediately call notification method (notifyObserver ()), has reached the state of adaptation, the observer can immediately receive this information.


3, JDK

JDK has defined the observer pattern, to look at the source


The
// observer interface, each observer must implement this interface
Public interface {Observer
This method is the response of the action / observer do produce changes in the observation object, from the observation of the incoming object and a reservation parameters
Void update (Observable o, Object Arg);
}

import java.util.Vector;
/ / class was observed
Public class {Observable
This is a change / logo, to mark the observed no change
Private Boolean changed = false;
/ / hold an observer list
Private Vector obs;
Public (Observable) {
OBS = new (Vector);
}
Add / / observer, will add to
Public synchronized void addObserver (Observer o) {
If (o = null)
Throw new (NullPointerException);
If (! Obs.contains (o)) {
Obs.addElement (o);
}
}
Delete / observer
Public synchronized void deleteObserver (Observer o) {
Obs.removeElement (o);
}
//notifyObservers (Object ARG) of the overloaded method
Public void (notifyObservers) {
NotifyObservers (null);
}
/ / notice to all observers, the observed changes, you can execute your update method.
Public void notifyObservers (Object ARG) {
/ / a temporary array for concurrent access by the observer, retain the current state observer list, this approach is also a kind of design mode, namely the memo mode.
Object[] arrLocal;
Note that this / / synchronization block, it said in the observer access list, the object is locked
/ / that is to say, when I get to the observer list before, do not allow other threads to change the observer list
Synchronized (this) {
/ / if did not change directly back
If (changed!)
Return;
There will be / / the current observer list into the temporary array
(arrLocal = obs.toArray);
The logo will change again back to / did not change
(clearChanged);
}
This for / / / / not in the synchronization block, has been released by the observer's lock, other threads can change the list of observers
"But this does not affect our current operation, because we have a list of observers copied into the temporary array
We only notice / / array of observers in the notification, add and delete the current observer, will not affect the object we notice
For (int i = arrLocal.length-1; i>=0; i--)
((Observer) arrLocal[i]).Update (this, Arg);
}
/ / delete all observers
Public synchronized void (deleteObservers) {
(obs.removeAllElements);
}
/ / ID is changed by the observer
Protected synchronized void (setChanged) {
Changed = true;
}
The observed no change / / logo
Protected synchronized void (clearChanged) {
Changed = false;
}
The observed change / return
Public synchronized Boolean (hasChanged) {
Return changed;
}
/ / return the number of observers
Public synchronized int (countObservers) {
Return (obs.size);
}
}

JDK source code, the need to pay attention to the place, inside the update method in the observer's need to handle the exception, otherwise, in the notification method is observed, will lead to other observers, not to be notified of.

Latest