Design Patterns in Android-viewer mode

Source: Internet
Author: User

Observer pattern

The observer pattern, sometimes called the publish-subscribe subscribe> mode, model-view view> mode, source-listener listener> mode, or slave mode. A target object manages all the observer objects that are dependent on it and proactively notifies when its own state changes.
The Observer pattern is a more commonly used pattern, it is like subscribing to RSS, when there are new news updates, the subject of the subscription will send a message to each subscriber, where the sending message is generally called the subscriber's already defined functions, such as onevent () and so on. You can also send messages to subscribers, such as handler, by other means. Do not rigidly adhere to the fixed way.
The observer design pattern defines a one-to-many dependency between objects, so that when the state of an object changes, all objects that depend on it are notified and refreshed automatically. It is good to separate the observer from the observed object.

Class diagram of the standard observer pattern.

Class diagram

Java Example

For example, there is a data source DataSource object that uses a queue to store the data, and an add and remove to add a data and a data, but there are two objects such as text objects and image objects that need to change their contents according to the changes in the data source. You can then define a observer interface that defines the Onadd and Onpoll methods, which are used to alert the observer to the increment or decrement of the data.
For the sake of simplicity, the data stored in the queue is not used for custom objects, directly using string strings.

The code is as follows:

Observers
 Public  interface Observer {     Public void Onadd(String str); Public void OnRemove(String str);} Public  class Image implements Observer {    @Override     Public void Onadd(String str) {System.out.println ("on Add:"+ str +", Image changed"); }@Override     Public void OnRemove(String str) {System.out.println ("on Remove:"+ str +", Image changed"); }} Public  class Text implements Observer {    @Override     Public void Onadd(String str) {System.out.println ("on Add:"+ str +", Text changed"); }@Override     Public void OnRemove(String str) {System.out.println ("on Remove:"+ str +", Text changed"); }}
The person being observed
 Public  interface DataSource {     Public void Addobserver(Observer Observer); Public void removeobservers(Observer Observer); Public void Notifyadd(String str); Public void Notifyremove(String str); PublicStringAddData(String str); PublicStringRemovedata();}ImportJava.util.ArrayList;ImportJava.util.LinkedList;ImportJava.util.List; Public  class simpledatasource implements DataSource {    PrivateLinkedlist<string> Data=NewLinkedlist<string> ();PrivateList<observer> observers =NewArraylist<observer> ();@Override     Public void Addobserver(Observer Observer)    {OBSERVERS.ADD (Observer); }@Override     Public void removeobservers(Observer Observer)    {OBSERVERS.REMOVE (Observer); }@Override     Public void Notifyadd(String str) { for(Observer obs:observers)        {Obs.onadd (str); }    }@Override     Public void Notifyremove(String str) { for(Observer obs:observers)        {obs.onremove (str); }    }@Override     PublicStringAddData(String str) {Data.add (str); This. Notifyadd (str);returnStr }@Override     PublicStringRemovedata() {String str=data.remove (); This. Notifyremove (str);returnStr }}
Test
    publicstaticvoidmain(String[] args) {        new SimpleDataSource();        new Image();        new Text();        source.addObserver(mText);        source.addObserver(mImage);        source.addData("First");        source.addData("Second");        source.removeData();        source.removeObservers(mText);//移出一个观察者,不再通知mText        source.removeData();    }
Output
on  Add: first , text  changedOn  ADD: first , image  changed on  Add:second, text  changedon  Add:second, image  changedon  Remove : first , text  changedon  remove : first , image  changedon  Remove : Second, image  changed  
Apps in Android

Contentobserver should be the most commonly used observer pattern in Android. We only need to customize a contentobserver and implement the OnChange method in it.
Then through the Contentresolver.registercontentobserver (Uri URI, Boolean Notifyfordescendents,contentobserver observer) method, Registers the custom Contentobserver object with the Contentresolver object to be observed. When a notification event is required, the observer is notified through the Getcontentresolver (). Notifychange () method.
The registration and notification here is obviously not in a process, for example, in a application through the CONTENTRESOLVER registered observer, in the B application through the Contentresolver notify the Observer, The contentresolver here is not a Contentresolver object in a, and is not able to be notified by a direct call. This is actually the event registration and notification through inter-process calls. The registration and notification itself is not in the contentresolver of the application, but in the Contentservice, which implements the Icontentservice.stub binder interface for cross-process invocation. The registration and notification of contentresolver in different processes is judged based on the URI parameters inside. If you are interested, you can study the source code.

Other

There are many places in the framework used in the observer pattern, in addition to the use of contentobserver, but also the following places, the use of the form is not only through direct calls, such as registrantlist, it is through the handler notification message, But the purpose is the same as the thought.

Transactionservice in MMS applications
Registrantlist
Datasetobserver
Observers who support cross-process registration
Ipackageinstallobserver
Ipackagedataobserver
Iaudioroutesobserver

Design mode in Android-viewer mode

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.