Adapter mode used in android

Source: Internet
Author: User

Adapter mode used in android

Adapter mode ):

The adapter mode is a behavior mode that converts an interface of a class to another interface expected, this allows the two classes that cannot work together because of interfaces to work together. The adapter mode can be a class adapter or an Object Adapter. Here we only introduce the Object Adapter mode (similar to the class adapter mode). The following is the class adapter (left) and the simple class graph structure of the Object Adapter mode (right:

 

The role involved in the adapter mode includes the adaptive role, the target role, and the adapter role. The specific descriptions are as follows:

Adaptive Role: Provides the interface role to adapt to the conversion.

Target role: the interface that the caller expects.

Adapter role: interface for converting an adapter role into a target role.

 

As before, we also list an instance to describe the use of the adapter mode. On the e-commerce platform, it is most common to query products, view product details, add shopping cart, and pay. If the original system has implemented the product query, add shopping cart and payment functions, the query function includes querying the product, viewing details, and calculating the total price of the product; adding a shopping cart includes viewing details, adding a shopping cart to a product, and paying for the product. Currently, these two modules do not work collaboratively, so we will adapt the shopping cart to work with viewing the product, achieve their integrated operations. Their detailed structure diagram is as follows:

Now, the core code of the adapter mode is listed below.

Target role (SaleShopCenter ):

/**

*@ Description:

* Target object

*/

Public interfaceSaleShopCenter {

 

List ScanSaleItems (String pKey );

 

SalescanSaleDetail (String pId );

 

DoubleCalculateTotalPrice ();

 

}

 

ShoppingCart ):

/**

*@ Description:

* Adaptive object

*/

Public classShoppingCart {

PrivateSalesale =Null;

Public staticList Results= Null; // Simulate cached data

 

PublicSale scanSaleDetail (StringpId ){

// Asynchronous request network

//TODO

 

// Simulation implementation here

Sale = getSale (pId );

ReturnSale;

}

 

Public voidAddSaleShoppingCart (List Sales ){

// Asynchronous request network

//TODO

 

AddCart (sales );

}

 

Public booleanBillPay (List Sales ){

// Asynchronous request network

//TODO

 

// The payment is successfully simulated here.

BooleanPayStatus =True;

If(Null= SaleShoppingManager.Results

| 0> SaleShoppingManager.Results. Size ()){

PayStatus =False;

}

ReturnPayStatus;

}

 

PrivateSale getSale (StringpId ){

If(Null= SaleShoppingManager.Results

| 0> SaleShoppingManager.Results. Size ()){

Return null;

}

 

Salesale =Null;

For(Saleitem: SaleShoppingManager.Results){

If(Item. getpID (). equals (pId )){

Sale = item;

}

}

 

ReturnSale;

}

 

Private voidAddCart (List Sales ){

Results= Sales;

}

}

Adapter role (SaleShoppingManager ):

/**

*@ Description:

* Adapter object role

*/

Public classSaleShoppingManagerImplementsSaleShopCenter {

PrivateShoppingCartcart =Null;

Public staticList Results= Null; // Simulate cache data

 

PublicSaleShoppingManager (){

Cart =NewShoppingCart ();

}

 

@ Override

PublicList ScanSaleItems (StringpKey ){

Results=NewArrayList ();

// Asynchronous Network request

//TODO

 

// Simulation implementation here

Results= GetSales ();

Return Results;

}

 

@ Override

PublicSale scanSaleDetail (StringpId ){

ReturnCart. scanSaleDetail (pId );

}

 

@ Override

Public doubleCalculateTotalPrice (){

If(Null=Results| 0>Results. Size ()){

Return0;

}

 

DoubleTotalPrice = 0;

For(Salesale:Results){

TotalPrice + = sale. getpPrice ();

}

 

ReturnTotalPrice;

}

 

PrivateList GetSales (){

List Result = NewArrayList ();

 

Salesale =NewSale ();

Sale. setpID (0000121300 );

Sale. setbID (1111232300 );

Sale. setpName (shirt-black collar );

Sale. setpPrice (123.14 );

Sale. setpDescri (black, round neck, high-end atmosphere !);

Result. add (sale );

 

Salesale2 =NewSale ();

Sale2.setpID (0000121311 );

Sales2.setbid (1111232311 );

Sale2.setpName (shirt-black collar );

Sale2.setpPrice (123.34 );

Sale2.setpDescri (black, round neck, high-end atmosphere !);

Result. add (sale2 );

 

Salesale3 =NewSale ();

Sale3.setpID (0000121322 );

Sale3.setbID (1111232322 );

Sale3.setpName (shirt-black collar 22 );

Sale3.setpPrice (123.24 );

Sale3.setpDescri (black, round neck, high-end atmosphere !);

Result. add (sale3 );

 

ReturnResult;

}

 

PublicShoppingCart getCart (){

ReturnCart;

}

}

The following figure shows the operation result of the program. I will upload the project code later. If you are interested, you can download and run the program. As follows:

 

Now, the adapter mode has been introduced.

 

 

 

 

 

 

 

 

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.