Adapter mode (Adapter):
The adapter pattern is a behavior pattern that transforms the interface of one class into another one that is expected, so that the two classes that would otherwise be unable to work together can work together. The adapter mode has two forms of adapter and object adapter, which only describes the object's adapter mode (similar to class adapter mode), and the following is the abbreviated class diagram structure for the class's adapter (left) and object adapter pattern (right):
The roles involved in the adapter pattern are adaptive, target, and adapter roles, as described below:
Adaptation role: Provides an interface role that requires a transformation.
Target role: The interface that the caller expects.
Adapter role: An interface that turns the adaptation role into a target role.
As before, we also cite an example to illustrate the use of the adapter pattern. In the e-commerce platform, it is most common to check items, view product details, add shopping carts and pay. If the original system has realized the product query, add shopping cart and payment function, query function including query products, view details, and calculate the total price of goods; add shopping cart including view details, product add shopping cart and payment, currently these two modules function is not working together, So below we will adapt the shopping cart to be able to work with the view products to achieve their integrated operation. Then their detailed structure class diagram is as follows:
OK, here's a list of the core code for the adapter pattern.
Target Role (Saleshopcenter):
/**
* @description :
* target Object
*/
Public interface Saleshopcenter {
List<sale>scansaleitems (String pKey);
Salescansaledetail (String pId);
double Calculatetotalprice ();
}
Adaptation role (ShoppingCart):
/**
* @description :
* objects to match
*/
Public class ShoppingCart {
Private SaleSale =null;
Public static list<sale> Results =null; // Simulating cached data ?
Public Sale scansaledetail (StringpId) {
// Asynchronous Request Network
// TODO
// Here the simulation implementation
Sale = Getsale (pId);
return Sale;
}
Public voidaddsaleshoppingcart (list<sale> sales) {
// Asynchronous Request Network
// TODO
Addcart (sales);
}
Public booleanbillpay (list<sale> sales) {
// Asynchronous Request Network
// TODO
// here the simulation payment succeeds
Boolean Paystatus =true;
if (null ==saleshoppingmanager. Results
|| 0 > Saleshoppingmanager. results. Size ()) {
Paystatus = false;
}
return Paystatus;
}
Private Sale 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;
}
}
return Sale;
}
private voidaddcart (list<sale> sales) {
results = sales;
}
}
Adapter Role (Saleshoppingmanager):
/**
* @description :
* Adapter Object Role
*/
Public class Saleshoppingmanager implementsSaleshopcenter {
Private ShoppingCartCart =null;
Public static list<sale> Results =null; // Simulating cached data
Public Saleshoppingmanager () {
Cart = new ShoppingCart ();
}
@Override
Public List<sale> Scansaleitems (StringpKey) {
results = new arraylist<sale> ();
// Asynchronous Network Requests
// TODO
// Here the simulation implementation
Results = getsales ();
return results;
}
@Override
Public Sale scansaledetail (StringpId) {
return cart. Scansaledetail (pId);
}
@Override
Public doublecalculatetotalprice () {
if (null = =Results | | 0 >results. Size ()) {
return 0;
}
Double Totalprice = 0;
for (SaleSale :results) {
totalprice + = Sale. Getpprice ();
}
return Totalprice;
}
Private List<sale> Getsales () {
List<sale>Result = new arraylist<sale> ();
SaleSale = new Sale ();
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 = new Sale ();
sale2. Setpid ("0000121311");
sale2. Setbid ("1111232311");
sale2. Setpname (" shirt-Black collar ");
sale2. Setpprice (123.34);
sale2. SETPDESCRI (" Black, round neck, high-end atmosphere! ");
result. Add (sale2);
Salesale3 = new Sale ();
sale3. Setpid ("0000121322");
sale3. Setbid ("1111232322");
sale3. Setpname (" shirt-Black collar ");
sale3. Setpprice (123.24);
sale3. SETPDESCRI (" Black, round neck, high-end atmosphere! ");
result. Add (sale3);
return result;
}
Public ShoppingCart Getcart () {
return cart;
}
}
Below is the operation result diagram of the program, I will upload the project code later, interested students can download run. As follows:
Okay, here we go. The adapter mode has been introduced.
Click I download the code!
Technology Exchange Group:179914858
Adapter roles are used in Android