Viewer mode Observer in Android

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/feiduclear_up/article/details/42167487


The Observer pattern "defines a one-to-many dependency between objects, and when an object's state changes, all objects that depend on it are notified and automatically updated", which can be understood as multiple observers subscribing to a message to the observer and notifying the appropriate observer to do their thing when the observer changes. The observer in ANDROID implements the Observer interface, and the observer inherits the observable class. Use the following steps:


1. The observer datawatcher implements the Observer interface.

2. The Observer datachange inherits the observable class.

3. The observer datawatcher to the observer DataChange to subscribe to the event (adding the Observer datawatcher to the DataChange of the person being observed).

4. The observed DataChange data changes, notify the observer datawathcer the corresponding data changes, please do the appropriate action.

5. The observer datawather to the observer DataChange to unsubscribe from the event (removing the observer datawatcher from the datachange of the observer).



The code is implemented as follows:

1. Observer class

Package Com.example.observer;import java.util.observable;import java.util.observer;/** * todo< Please describe what this class is for > *  * @author XJP * @data: December 26, 2014 Morning 9:29:56 * @version: V1.0 */public abstract class Datawatcher implements Observer {@Overridepublic void update (Observable Observable, Object data) {}}

2. The class of the person being observed

Package Com.example.observer;import java.util.observable;/** * todo< Please describe what this class is for > *  * @author XJP * @data: December 26, 2014 Morning 9:30:21 * @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 Notifydatachange (data data) {//How did the observer inform the Observer that the data has changed?? Here are two ways to be critical. Setchanged (); notifyobservers (data);}}


3. Operation class

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, DA TA);//The observer receives notifications from the observer to update its 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);//Simulate the observed data changes and update the data. Data Mdata = new data (); Mdata.setdatachange (0);D atachange.getinstance (). Notifydatachange (Mdata);}    @Overrideprotected void Onresume () {super.onresume ();//The Observer adds a subscription event to the observed person. Datachange.getinstance (). Addobserver (watcher);} @Overrideprotected void OnPause () {super.onpause ();//observers remove datachange.getinstance () from the Observer queue. Deleteobserver (Watcher );}}

4. Data class:

Package com.example.observer;/** * todo< Please describe what this class is for > *  * @author XJP * @data: December 26, 2014 Morning 9:33:33 * @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 am mainly a framework for implementing the Observer Pattern code in Android, and how to write code depends on the actual business. In fact, the main idea of programming is the implementation of the framework.


Viewer mode Observer in Android

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.