The observer mode is used in android

Source: Internet
Author: User

The observer mode is used in android

Observer mode (Observer)

The observer mode is the behavior mode of the object, and is called as the Model-View Mode. This mode defines a one-to-many dependency so that multiple observer objects can listen to a role object at the same time. Once the status of this role object changes, it will notify all observer objects so that they can automatically update themselves.

The structure of the observer mode is as follows:

 

As shown in the figure above, the observer mode involves four roles, as shown below:

A. abstract object role: this role is an abstract role. It stores all references to the observer object in A collection. Each topic object can have several observer objects, A topic role is also called an abstract object to be observed. It is generally represented by an abstract class or an interface.

At the same time, this role contains specific management methods for the observer and notifies all observer objects when the topic role changes.

B. Specific entity role: this role is a specific role that implements an abstract entity role. It contains a reference to the state of an object role. Once the object role changes, the notification method is called to notify all the observer objects.

C. Abstract observer object: this role provides common interfaces for all specific observer objects and updates itself in real time when the entity changes are notified, this role is generally implemented using abstract classes and interfaces.

D. Specific observer object: place the relevant state to a specific observer object, and send a notification to all the observers when the State inside the specific subject changes. This role is also called an observer role.

The following examples illustrate the use of the observer mode. The example is as follows: the user wants to buy several clothes of different styles of the same brand. if the user has already selected clothes of different styles, add them to the shopping cart and pay for them; after the payment is successful, you need to refresh your shopping cart in time so that you can know the payment result in time, so that you can easily make a new payment or select another style and pay for it. Now, the description is complete. Next, see the detailed class diagram of the function:

 

See the code below! I will list the core code of the observer mode below. For more information, see the project code package I uploaded. The details are as follows:

Abstract entity role (Cart ):

/**

*@ Description:

* Abstract object role-object entity followed by the observer

*/

Public abstract classCart {

PrivateVector Observers = NewVector ();

 

PublicCart (){

Super();

}

 

/**

*@ Description:

* Register an observer object

*/

Public voidAttach (SalesObserver observer ){

Observers. addElement (observer );

}

 

/**

*@ Description:

* Delete A registered observer object

*/

Public voidDetach (SalesObserver observer ){

Observers. removeElement (observer );

}

 

Public voidDetach (IntIndex ){

Observers. remove (index );

}

 

/**

*@ Description:

* Notify all registered observer objects

*/

Public voidNotifyAllBills (){

Enumeration Enumes = observers ();

While(Enumes. hasMoreElements ()){

SalesObserverobj = (SalesObserver) enumes. nextElement ();

Obj. updateSale (obj );

}

}

 

/**

*@ Description:

* After the payment is successful, the payment status of all items in the shopping cart is updated.

* Note: The local cache data is updated here (not through the interface)

*/

Public booleanBillsPay (){

BooleanPayStatus =True;

// Asynchronous Network payment

//TODO

 

// The payment is successfully simulated here.

If(PayStatus ){

// Update local cache data

UpdateLocalCache ();

}

 

Return true;

}

 

/**

*@ Description:

* Update Cache

*/

Private voidUpdateLocalCache (){

For(SalesObserver obs: observers ){

Salesale = obs. getSale ();

Sale. setPayStatus (Payment completed );

}

}

 

PublicEnumeration Observers (){

ReturnObservers. elements ();

}

}

 

ShoppingCart ):

/**

*@ Description:

* Specific entity roles-notify various observer objects to update the status

*/

Public classShoppingCartExtendsCart {

 

PublicShoppingCart (){

Super();

}

 

Protected voidBillPayResult (BooleanFlag ){

If(! Flag ){

// Payment failure prompt (this case is not considered here)

Return;

}

 

// NotifyObervers

NotifyAllBills ();

}

}

 

Abstract Observer role (Observer ):

/**

*@ Description:

* Abstract observer-Provides update Methods for all specific observers

*/

Public interfaceObserver {

 

PublicObject updateSale (Object obj );

 

}

 

SalesObserver ):

/**

*@ Description:

* Specific observer-perform the change status operation

*/

Public classSalesObserverImplementsObserver {

PrivateSale sale =Null;

 

PublicSalesObserver (Sale sale ){

This. Sale = sale;

}

 

@ Override

PublicObject updateSale (Object obj ){

This. Sale = (Sale) obj;

ReturnSale;

}

 

PublicSale getSale (){

ReturnSale;

}

 

Public voidSetSale (Sale sale ){

This. Sale = sale;

}

}

 

Well, the core code has been pasted. The following shows the demonstration of the entire process:

Add shopping cart:

 

Remove shopping cart:

 

Effect after payment (the effect of the previous shopping cart is the same as that of the remove shopping cart ):

 

 

 

 

 

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.