[Original-tutorial-serialization] "android big talk Design Patterns"-structural patterns of design patterns Chapter 10: adapter patterns for laptops

Source: Internet
Author: User
<Big talk design model> description and copyright notice of this tutorial

Guoshi studio is a technical team dedicated to enterprise-level application development on the Android platform. It is committed to the best Android Application in China.ProgramDevelopment institutions provide the best Android enterprise application development training services.

Official contact information for Enterprise Training and Development Cooperation:

Tel: 18610086859

Email: hiheartfirst@gmail.com

QQ: 1740415547

QQ: 148325348

Guoshi studio is better for you!

L this document references and uses free images and content on the Internet, and is released in a free and open manner, hoping to contribute to the mobile Internet and the smart phone age! This document can be reproduced at will, but cannot be used for profit.

L if you have any questions or suggestions about this document, go to the official blog

Http://www.cnblogs.com/guoshiandroid/ (with the following contact information), we will carefully refer to your suggestions and modify this document as needed to benefit more developers!

L The latest and complete content of "big talk design mode" will be regularly updated on the official blog of guoshi studio. Please visit the blog of guoshi studio.

Http://www.cnblogs.com/guoshiandroid/get more updates.

 

Adapter Mode Laptop Adapter 

Adapter ModeUse Cases: 

MM's form was a mess, and promised mm to send her a notebook. Mm may be broken down. ^_^ but because cousin and mm are not in the same city, the laptop needs to be mailed. A few days later, MM finally received a notebook and was about to die happily! But when mm is about to check the actual running effect of the computer, it finds that the plug is wrong and cannot be inserted! Haha, mm is crying and immediately calls GG. Gg said with a smile after learning the details of the incident: "stupid, don't worry, I am going to buy you a plug-in converter soon OK, gg ran to the technology market to find a plug-in converter suitable for the MM computer, and did not know where the cousin of MM bought the computer. He asked several shops not to find a suitable computer. gg thought, if you can't buy it, you can't go back and explain it to mm. Finally, GG purchased the product with great efforts. When mm was able to use the computer, she sent a text message to Gg, "dear, love you, and love you in the next life !".

Adapter mode description: 

Adapter pattern: converts an interface of a class to another interface that the customer wants. The adapter mode allows the classes that cannot work together due to incompatibility of interfaces to work together.

Adapters are divided into object adapters and class adapters.

Object adapters use a combination to adapt not only to a class, but also to any subclass of the class. If a new behavior is added to the subclass, you only need to make the composite object a subclass to solve the flexibility problem. Because the class adapter adopts the inheritance method, it can only use a specific adaptive class, this has a major advantage, that is, it does not need to implement the entire adaptive class, and it can also overwrite the behavior of the adapter when necessary.

Convert the interface of a class into another interface clients
Ct.

Adapter ModeUMLFigure: 

The UML diagram for class adaptation is as follows:

 

The role involved in the class adapter is as follows:

Target role: the interface that the client expects. This is because the class adapter is discussed here, and because of the attributes inherited by the Java language, the target role cannot be a class.

Source (adaptee) role: the interface to be adapted is the object to be adapted.

Adapter role: this is the core of the class adapter mode. The adapter converts the source interface to the target interface. Obviously, the adapter role cannot be an interface, but must be a specific class.

The UML diagram of the Object Adapter is as follows:

The role involved in the Object Adapter is as follows:

Target role: the interface that the client expects. The target role can be either a specific class or an abstract class.

Source (adaptee) role: the interface to be adapted is the object to be adapted.

Adapter role: this is the core of the Object Adapter mode. The adapter converts the source interface to the target interface. Obviously, the adapter role cannot be an interface, but must be a specific class.

In-depth analysis of adapter Mode:

We often encounter the need to combine two unrelated classes. The first solution is to modify the interfaces of the respective classes, but if we do notSource codeOr, we do not want to modify their respective interfaces for an application. This scheme cannot be used at this time, and modifying the source code will inevitably affect the classes and objects related to these classes. The second method is to use an adapter, transform the original interface to the target interface required by the customer, and build a bridge between the source interface and the client. The source interface can be used by the target interface without any modification.

The purpose of the adapter mode is: if the customer needs to use a service of a class, and this service is provided by this class using a different interface, then, you can use an adapter to provide customers with an interface they want.

The specific process of using the adapter on the client is as follows:

Step 1: the client sends a request to the adapter by calling the adapter method through the target interface.

Step 2: The adapter converts requests to one or more called interfaces of the adapter.

Step 3: the client receives the call result but does not realize that this is the conversion function of the adapter.

In general, I advocate object adapters because object adapters comply with good object-oriented design principles. Object adapters use object combinations, use interfaces suitable for the client to package the adaptors. This method brings about another advantage: it is used to adapt to this, and also applies to any subclass of the adaptors; if a class adapter is used, this flexibility will be lost, and the target role of the class adapter cannot be a class, because Java is a single inheritance language.

Use Case Analysis andCodeImplementation: 

In the above application scenario, the power plug of the MM laptop cannot be used normally, that is, the plug of the notebook does not match the interface of the MM school dormitory, so GG went to the market and bought a converter suitable for the MM notebook and dormitory Power interface. The converter plug is the adapter.

 

Create a power supply class for the dormitory:

PackageCom. diermeng. designpattern. Adapter. impl;

/*

* Power Supply

*/

Public ClassCurrent {

/*

* Power supply voltage

*/

Public VoidUse220v (){

System.Out. Println ("the dormitory power supply voltage is 220v ^_^ ");

}

}

 

Create a class adapter:

PackageCom. diermeng. designpattern. Adapter. impl;

/*

* The class adapter uses the inheritance method.

*/

Public ClassAdapterclassExtendsCurrent {

/*

* The interface expected by the client

*/

Public VoidUse18v (){

System.Out. Println ("I Am a class adapter ");

// Call the current method

This. Use220v ();

}

}

 

Create a test client for the class adapter:

PackageCom. diermeng. designpattern. Adapter. client;

ImportCom. diermeng. designpattern. Adapter. impl. adapterclass;

 

/*

* Test the class adapter client.

*/

Public ClassAdapterclasstest {

Public Static VoidMain (string [] ARGs ){

// Instantiate the class Adapter

Adapterclass adapter =NewAdapterclass ();

// Call the method provided by the class adapter to the client

Adapter. use18v ();

 

}

}

 

The running result is as follows:

I am a class Adapter

The power supply voltage of the dormitory is 220v ^_^

Create an Object Adapter:

PackageCom. diermeng. designpattern. Adapter. impl;

/*

* Object Adapter

*/

Public ClassAdapterobject {

/*

* Reference to the Source Target

*/

PrivateCurrent current;

 

/*

* Instantiate current in the constructor

*/

PublicAdapterobject (current
Current ){

This. Current = current;

}

/*

* Interface provided by the Object Adapter to the client

*/

Public VoidUse18v (){

System.Out. Println ("I am an object using an adapter ");

This. Current. use220v ();

}

}

Create a test client for the Object Adapter:

package COM. diermeng. designpattern. adapter. client;

Import COM. diermeng. designpattern. adapter. impl. adapterobject;

Import COM. diermeng. designpattern. adapter. impl. current;

/*

* object test client adaptation

*/

Public class adapterobjecttest {

Public static void main (string [] ARGs) {

// declare and instantiate the Object Adapter

adapterobject adapter = New adapterobject ( New current ());

// call the method provided by the Object Adapter to the client

adapter. use18v ();

}

}

Object Adapter running result:

I am an object using an adapter

The power supply voltage of the dormitory is 220v ^_^

 

Advantages and disadvantages of the adapter mode: 

Advantages:

Using the adapter mode, you can associate one system interface with another system interface so that classes that cannot work together can work together, the adapter mode emphasizes Interface Conversion.

Disadvantages:

The class adapter cannot adapt to the subclass of the class. For the Object Adapter, it is difficult to redefine the behavior of the adapted class, but it is not impossible, in this case, subclass inheritance must be used to modify the subclass of the adaptive class.

Introduction to the actual application of the adapter mode: 

In the process of large-scale system development, we often encounter the following situations: we need to implement some features that are not yet mature with one or more external components, if we re-develop these features on our own, it will take a lot of time; therefore, in many cases, we will choose to temporarily use external components and then consider replacing them at any time. However, this may cause a problem. With the replacement of the external component library, the source code that references the external component may need to be modified in a large area, therefore, new problems may also be introduced. How can we minimize the modification surface? The adapter mode is proposed to address this similar requirement. The adapter mode transparently calls external components by defining a new interface (abstracting the functions to be implemented) and an adapter class that implements the interface. When you replace an external component, you only need to modify several adapter classes at most, and other source code will not be affected.

Specifically, the adapter mode can be applied to the following situations:

1. The system needs to use an existing class, and such an interface does not meet the requirements of the system.

2. You want to create a reusable class for some classes that are not highly correlated with each other, including some classes that may be introduced in the future, these source classes do not necessarily have the same interfaces. This situation may be classified into the scope of the facade mode from different perspectives. However, it is understandable to consider this issue from the perspective of adaptation to the existing design.

3. Insert a class into another class system through Interface Conversion.

Tip: 

in general, I advocate object adapters because they comply with good object-oriented design principles. Object adapters use object combinations, use interfaces suitable for the client to package the adaptors. This method brings about another advantage: it is used to adapt to this, and also applies to any subclass of the adaptors; if a class adapter is used, this flexibility will be lost, and the target role of the class adapter cannot be a class, because Java is a single inheritance language.

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.