Android design mode (2)-----Viewer mode

Source: Internet
Author: User

Observer Pattern sometimes referred to as the Publish/subscribe pattern, the Observer pattern defines a one-to-many dependency that allows multiple observer objects to listen to a Subject object at the same time. This subject object notifies all observer objects when the state changes, enabling them to automatically update themselves. There are four characters in the Observer pattern:

Abstract topic: It is an interface. It saves references to all observer objects in a single aggregation, and each subject can have any number of observers. Abstract topics provide an interface that allows you to add and remove observer objects. There are three abstract methods:

Attach () Adds the Observer object to the collection

Detach () Removes the Observer object from the collection

notify () update Observer object

specific topics: Implement abstract topics, put states into specific topics, and when specific topics change, send notifications to update the state of the Observer. Three ways to rewrite abstract topics: attach (), detach (), notify ()

abstract Observer: Define an abstract class for all observers and update yourself at the time of notification: there is an abstract method:

update (): Update yourself when you receive a notification

specific Observer: Inherit the abstract observer, rewrite its required interface IU, update itself so that it is consistent with the state of the subject. Overriding the abstract method of the parent class: update ()

The relationship of the four can be expressed as:


Let's analyze an example:

When shopping, the customer pays, the cashier needs to be accounted for, accounting invoicing, delivery of the delivery of these three jobs at the same time.

Let's take a look at the code:

Abstract Topics:

/** * Abstract Theme: It saves references to all observer objects in a single aggregation, each subject can have any number of observers. * Abstract topic provides an interface that can add and remove observer objects. * @createTime: 2014-10-28  pm 3:31:44 */public interface Isubject {list<jobstation> observers = new ArrayList <JobStation> ();/** * Increase the Observer */void Attach (Jobstation observer);/** * Remove observer */void Detach (Jobstation observer);/** * to The Observer (*/void) gave notice to Notify ();}

Specific topic: Here is the customer as a specific topic

/** *  specific subject, here is the customer: the status of the relevant to the specific observer object; * Notify all registered observers when changes are made to the internal status of a specific subject * * @createTime: 2014-10-28  pm 3:31:57 */public Class Customer implements Isubject {public String customerstate;/** * Customer status */public String customerstate () {return custome Rstate;} @Overridepublic void Notify () {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 Viewer:

/** * Work as an abstract observer here: Define an interface for all specific observers and update yourself when you get a topic notification * @createTime: 2014-10-28  pm 3:31:34 */public Abstract class jobstation {/** * update status */public abstract void Update ();}
Specific Observer: Teller

/** * Cashier, as the specific observer here: implements the update interface required by the abstract observer role in order to reconcile its state with the topic State * @createTime: 2014-10-28  pm 3:41:08 */public class Cashier Extends Jobstation {private String cashierstate;private customer Customer;public Cashier (Customer customer) { This.customer = Customer;} @Overridepublic void Update () {if (customer. Customerstate (). Equals ("paid")) {log.d ("CCCCCCC", "I am a cashier, I register"); cashierstate = "recorded";}}}

Specific Observer: Accounting

/** * Accountant, as the specific observer here: implements the update interface required by the abstract observer role in order to reconcile its state with the topic State * @createTime: 2014-10-28  pm 3:41:08 */public class Accountant extends Jobstation {private String accountantstate;private customer Customer;public Accountant (Customer Customer) {This.customer = customer;} @Overridepublic void Update () {if (customer. Customerstate (). Equals ("paid")) {log.d ("CCCCCCC", "I am an accountant, I will issue an invoice"); accountantstate = "Invoiced";}}}

Specific Observer: Distribution Clerk

/** * Distribution Clerk, as the specific observer here: implements the update interface required by the abstract observer role in order to reconcile its state with the topic State * @createTime: 2014-10-28  pm 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 the distribution clerk, I come to ship");d illivierymanstate = "Shipped";}}}

Writing here is basically finished, I hope to help you.

Android design mode (2)-----Viewer mode

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.