Observer mode Observer and androidobserver in Android

Source: Internet
Author: User

Observer mode Observer and androidobserver in Android

Reprinted please indicate the source: http://blog.csdn.net/feiduclear_up/article/details/42167487


Observer mode: "defines a one-to-many dependency between objects. When the State of an object changes, all objects dependent on it will be notified, and automatically update ". It can be understood that multiple observers subscribe to messages to one observer. When the observer changes, the corresponding observer is notified to perform his/her own tasks. The Observer in Android implements the Observer interface, and the Observer inherits the Observable class. The procedure is as follows:


1. Observer DataWatcher implements the Observer interface.

2. the Observable class is inherited by the observer DataChange.

3. The observer DataWatcher subscribes to the observer DataChange (adds the observer DataWatcher to the observer DataChange ).

4. If the data in the DataChange field of the observer changes, notify the observer that the corresponding data in DataWathcer has changed. Please perform the corresponding operations.

5. The observer DataWather cancels the subscription to the observer DataChange (the observer DataWatcher is removed from the observer DataChange ).



The code is implemented as follows:

1. Observer class

Package com. example. observer; import java. util. observable; import java. util. observer;/*** TODO <describe what this class is doing> ** @ author xjp * @ data: 9:29:56, January 1, December 26, 2014 * @ version: v1.0 */public abstract class DataWatcher implements Observer {@ Overridepublic void update (Observable observable, Object data ){}}

2. Observer class

Package com. example. observer; import java. util. observable;/*** TODO <describe what this class is doing> ** @ author xjp * @ data: 9:30:21, January 1, December 26, 2014 * @ version: v1.0 */public class DataChange extends Observable {private static DataChange instance = null; public static DataChange getInstance () {if (null = instance) {instance = new DataChange ();} return instance;} public void policydatachange (Data data) {// how can I notify the observer that the Data has changed? What about it ?? The two methods here are the key. SetChanged (); yyobservers (data );}}


3. Operation

Package com. example. observer; import java. util. observable; import android. OS. bundle; import android. app. activity; import android. util. log; import android. view. menu; public class MainActivity extends Activity {private DataWatcher watcher = new DataWatcher () {@ Overridepublic void update (Observable observable, Object data) {super. update (observable, data); // The Observer receives the notification from the observer to update its own data operations. Data mData = (Data) data; Log. I ("Test", "mData ---->" + mData. getDataChange () ;}}; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // simulates changes to the observer data and updates the data. Data mData = new Data (); mData. setDataChange (0); DataChange. getInstance (). notifyDataChange (mData) ;}@ Overrideprotected void onResume () {super. onResume (); // The Observer adds a subscription event to the observer. DataChange. getInstance (). addObserver (watcher) ;}@ Overrideprotected void onPause () {super. onPause (); // The Observer removes DataChange from the observer queue. getInstance (). deleteObserver (watcher );}}

4. Data class:

Package com. example. observer;/*** TODO <describe what this class is doing> ** @ author xjp * @ data: 9:33:33, January 1, December 26, 2014 * @ version: v1.0 */public class Data {private int dataChange; public Data () {} public int getDataChange () {return dataChange;} public void setDataChange (int dataChange) {this. dataChange = dataChange ;}}

Note: I mainly implement a framework of the observer mode code in android. The actual business needs to be taken into account for how to write the code. In fact, the main idea of programming is the implementation of the Framework.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.