Android design mode (2) ----- observer Mode

Source: Internet
Author: User

Android design mode (2) ----- observer Mode

The observer mode is sometimes called the publish/subscribe mode. The observer mode defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves. The observer mode has four roles:

Abstract topic: it is an interface. It saves the reference of all observer objects to one aggregation, and each topic can have any number of observers. Abstract topic provides an interface to add and delete observer objects. There are three Abstract METHODS:

Attach () adds an observer object to the set.

Detach () deletes the observer object from the set

Y () updates the observer object in the Set Based on theme changes.

Specific topic: implements the abstract topic and stores the status into a specific topic. When a specific topic changes, a notification is sent to update the observer's status. Three methods to override an abstract topic: Attach (), Detach (), Policy ()

Abstract observer: defines an abstract class for all observers and updates themselves when receiving notifications. An abstract method is provided:

Update (): Update yourself when receiving the notification

Specific observer: Inherit the abstract observer, rewrite the interface iu required by the observer, and update the observer so that the observer and the topic State are consistent. Override the abstract method of the parent class: Update ()

The relationships between the four can be expressed as follows:


Let's analyze an instance:

When buying things, after the customer pays, the cashier needs to make an account, make an invoice, and deliver the goods to the courier at the same time.

Let's look at the Code:

Abstract topic:

/*** Abstract topic: It stores reference of all observer objects in one aggregation. Each topic can have any number of observers. * An abstract topic provides an interface for adding and deleting observer objects. * @ CreateTime: 3:31:44 */public interface ISubject {List
 
  
Observers = new ArrayList
  
   
();/*** Add the observer */void Attach (JobStation observer);/*** remove the observer */void Detach (JobStation observer ); /*** send a notification to the Observer */void y ();}
  
 

Subject: Here is the customer's subject.

/*** Specific topic. Here is the customer: stores the relevant status into a specific observer object; * when the internal status of a specific topic changes, send a notification to all registered observers ** @ createTime: 3:31:57 */public class Customer implements ISubject {public String customerState; /*** customer status */public String CustomerState () {return customerState;} @ Overridepublic void every Y () {for (JobStation o: observers) {o. update () ;}@ Overridepublic void Attach (JobStation observer) {// TODO Auto-generated method stubobservers. add (observer) ;}@ Overridepublic void Detach (JobStation observer) {// TODO Auto-generated method stubobservers. remove (observer );}}

Abstract observer:

/*** Job position, as the abstract observer here: defines an interface for all the specific observers and updates themselves when receiving the topic notification * @ createTime: 3:31:34 */public abstract class JobStation {/*** Update Status */public abstract void Update ();}
Specific observer: Cashier

/*** Cashier, as the specific observer here: implements the update interface required by the abstract observer role to coordinate its status with the topic status * @ createTime: 3:41:08 */public class Cashier extends JobStation {private String cashierState; private Customer customer; public Cashier (Customer customer Customer) {this. customer = customer ;}@ Overridepublic void Update () {if (customer. customerState (). equals ("paid") {Log. d ("ccccccc", "I am a cashier, I am registering"); cashierState = "recorded ";}}}

Specific observer: Accounting

/*** Accountant, as the specific observer here: implements the update interface required by the abstract observer role to coordinate its status with the topic status * @ createTime: 3:41:08 */public class Accountant extends JobStation {private String accountantState; private Customer customer; public Accountant (Customer customer Customer) {this. customer = customer ;}@ Overridepublic void Update () {if (customer. customerState (). equals ("paid") {Log. d ("ccccccc", "I am an accountant, I am here to issue an invoice"); accountantState = "invoiced ";}}}

Specific observer: courier

/*** Courier, as the specific observer here: implements the update interface required by the abstract observer role to coordinate its status with the topic status * @ createTime: 3:41:08 */public class Dilliveryman extends JobStation {private String dillivierymanState; private Customer customer; public Dilliveryman (Customer customer) {this. customer = customer ;}@ Overridepublic void Update () {if (customer. customerState (). equals ("paid") {Log. d ("ccccccc", "I am a courier, I will ship"); dillivierymanState = "delivered ";}}}

It is basically written here, and I hope it will help you.

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.