in the book "Java and Patterns" of Dr. Shanhong, this describes the Observer (Observer) pattern:
The Observer pattern is the behavior pattern of the object, also called the Publish-subscribe (publish/subscribe) mode, the Model-view (Model/view) mode, the source-listener (Source/listener) mode, or the slave (dependents) mode.
The Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. When the subject object changes in state, all observer objects are notified so that they can automatically update themselves.
Structure of the Observer pattern
A software system contains a variety of objects, like a thriving forest full of creatures. In a forest, all kinds of organisms depend on each other and bind to form a chain of life. The state change of one organism causes the corresponding action of other organisms, each of which is in the interaction of other organisms.
Similarly, a software system often requires changes in the state of an object, and some other objects make corresponding changes. There are many design options to do this, but in order to make the system easy to reuse, a low-coupling design should be chosen. Reducing the coupling between objects facilitates the reuse of the system, but designers need to enable these low-coupling objects to maintain coordination of actions and ensure a high degree of collaboration. The Observer pattern is the most important of the various design scenarios that satisfy this requirement. (from the network above)
is the basic model of the observer pattern, taking the call state (Callstate) as an example, to analyze the mobile phone call status changes, how to pass this event.
Registrantlist as a notified person, registrant is the Observer, the Registrantlist notifier provides add, remove, notifyregistrants to manage registrant.
1. How is the call status monitored?
Mcallstateregistrants is an object of registrantlist, generated in Basecommand.java and encapsulated by the addition and deletion of Registrantlist notifier :
protected Registrantlist mcallstateregistrants = new Registrantlist ();
public void registerforcallstatechanged (Handler H, int. "What, Object obj") { registrant R = new Registrant (h, what, obj );//Observer Mcallstateregistrants.add (R); } public void unregisterforcallstatechanged (Handler h) { mcallstateregistrants.remove (h); }
Registerforcallstatechanged a registrant observer, add it to the notifier and see how registrant is stored?
Public synchronized void Add (registrant R) { removecleared (); Registrants.add (R); }
Registrants is a ArrayList object, management registrant is actually the operation of ArrayList.
Registerforcallstatechanged is called in the Gsmcalltracker.java constructor:
Mci.registerforcallstatechanged (this, event_call_state_change, null);
This isGsmcalltracker itself,Gsmcalltracker inherited handler, here, that is to say Gsmcalltracker became the Observer, when the Callstate changed Mcallstateregistrants should notify Gsmcalltracker.
When no listening is required, you can log off:
Mci.unregisterforcallstatechanged (this);
2, MCallstateregistrants is how to inform gsmcalltracker callstate change?
In the mobile phone, when the call state changes, the modem will pass the message up, to the framework, is ril.java receive processing:
Case ril_unsol_response_call_state_changed: if (RILJ_LOGD) Unsljlog (RESPONSE); Mcallstateregistrants . Notifyregistrants (new AsyncResult (null, NULL, NULL)); Break
Mcallstateregistrants uses notifyregistrants to notify all of its observers:
Public/*synchronized*/void Notifyresult (Object result) { internalnotifyregistrants (result, null); } private synchronized void internalnotifyregistrants (Object result, throwable exception) {for (int i = 0, S = registrants.size (); I < S; i++) { registrant r = (registrant) registrants.get (i); R.internalnotifyregistrant (result, exception); } }
Registrants is the ArrayList, traversal, and notification that previously stored all observers.
3. How does the observer registrant respond to processing notifications?
above see R.internalnotifyregistrant (result, exception), the Observer knows Callstate change, who will really deal with it?
Registrant part of the code:
Internalnotifyregistrant (Object result, throwable exception) { Handler h = gethandler (); if (h = = null) { clear (); M:registrant Debug Log Enhancement log.d ("registrant", "internalnotifyregistrant (): warning! Handler is null and it could be already GCed. (what= "+ What +", userobj= "+ Userobj +", result= "+ result +", exception= "+ Exception +") "); } else { Message msg = Message.obtain (); Msg.what = what; Msg.obj = new AsyncResult (userobj, result, exception); H.sendmessage (msg); } }
Handler h = gethandler (); The Handler obtained is the one mentioned above, which isGsmcalltracker,what is the second parameter event_call_state_change when registerforcallstatechanged is registered, Here H.sendmessage is the use of Gsmcalltracker this handler sent messages. As we all know, themessage sent by handler is handled by its own handlemessage () method.
So, we see in Gsmcalltracker's Handlemessage () method:
Case Event_call_state_change: pollcallswhensafe (); Break
What do we do, we don't care ...
The Android telephony source uses a lot of observer patterns, understanding the Observer pattern, helps us to read the source code.
Please correct me if there is any wrong place to be continued.
Android and design mode-viewer (Observer) mode