Android Design Pattern series (2)-Observer Pattern of SDK source code

Source: Internet
Author: User

The observer pattern is a very common design pattern that can be seen everywhere in many systems, especially when the data status changes and needs to be notified.
This article uses abstractcursor as an example to expand the analysis.
The observer mode, Observer pattern, is a very practical mode. This mode is used in various platforms that I have come into contact with and the print template interpreter used in projects.

1. Intention
Defines a one-to-many dependency between objects. When the status of an object changes, all objects dependent on it are notified and automatically updated.
Popular Words:Dependency publishing-subscribe to Event Notification update listening 

2. Structure


This is the simplest observer mode. A target object can add or delete an observer. When its status or behavior changes, notify the registered observer through notify to perform the update operation.
By analyzing the specific situation of abstractcursor, we find that the actual work sometimes requires unified management of the observer, and even there are many observer types that can be divided into several series, which is much more complicated, this problem can be well solved through rational layering. The following shows the observer pattern structure used in abstractcurosr in Android based on the actual situation:


The observer is divided into two series.

3.Code
The core code is as follows:

Public abstract class abstractcursor {// define the manager datasetobservable mdatasetobservable = new datasetobservable (); registrant metadata = new contentobservable (); // register and uninstall two types of observer public void observer (contentobserver observer) {mcontentobservable. registerobserver (observer);} public void unregistercontentobserver (contentobserver observer) {// cursor will unregister all obse Rvers when it close if (! MClosed) {mcontentobservable. unregisterobserver (observer) ;}} public void registerdatasetobserver (datasetobserver observer) {mdatasetobservable. registerobserver (observer);} public void unregisterdatasetobserver (datasetobserver observer) {mdatasetobservable. unregisterobserver (observer);} // The protected void onchange (Boolean selfchange) {synchronized (mselfobserverlock) {mcontentobservabl E. dispatchchange (selfchange); If (mpolicyuri! = NULL & selfchange) {mcontentresolver. policychange (mpolicyuri, mselfobserver) ;}} protected void policydatasetchange () {mdatasetobservable. policychanged ();}}

let's look at the observable class and datasetobservable class:

Public abstract class observable <t> {/*** observer list */protected final arraylist <t> mobservers = new arraylist <t> (); Public void registerobserver (T observer) {If (Observer = NULL) {Throw new illegalargumentexception ("the observer is null. ");} synchronized (mobservers) {If (mobservers. contains (observer) {Throw new illegalstateexception ("Observer" + observer + "is already registered. ");} mobservers. add (observer) ;}} public void unregisterobserver (T observer) {If (Observer = NULL) {Throw new illegalargumentexception ("the observer is null. ");} synchronized (mobservers) {int Index = mobservers. indexof (observer); If (Index =-1) {Throw new illegalstateexception ("Observer" + observer + "was not registered. ");} mobservers. remove (INDEX) ;}} public void unregisterall () {synchronized (mobservers) {mobservers. clear ();}}}

And

 
Public class datasetobservable extends observable <datasetobserver> {/*** when data changes, notify all Observers */Public void policychanged () {synchronized (mobservers) {for (datasetobserver observer: mobservers) {observer. onchanged ();}}}//...... (Other Methods )}

The observer datasetobserver class is an abstract class:

 
Public abstract class datasetobserver {public void onchanged () {// do nothing }}

So let's take a look at its subclass:

 
Public class alphabetindexer extends datasetobserver {/** @ hide is hidden by the Android system */@ override public void onchanged () {// observe the data changes, the observer does what he should do. Super. onchanged (); malphamap. clear ();}}

Contentobserver is similar.

4. effect
(1 ). behavior mode
(2 ). abstract coupling between the target and the observer (Classic implementation ).
(3). Support for Broadcast Communication (I believe this android developer should be inspired to see it ).
(4). Pay attention to unexpected updates, which is also one of the reasons for the observer to manage updates.

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.