Adapter Definition:
Adapter mode (Adapter pattern): Converts an interface into another interface that the customer wants, and the adapter mode can work with those classes that are incompatible with the interface, whose alias is the wrapper (Wrapper). The adapter mode can be either a class-structured pattern or an object-structured pattern.
Role description
Target role: This is the interface you expect to get. Note: Because the class adapter pattern is discussed here, the target cannot be a class.
Source (adapee) role: A suitable interface is now required.
Adapter (adaper) Role: The adapter class is at the heart of this pattern. The adapter converts the source interface into the target interface. Obviously, this role cannot be an interface, but must be a concrete class.
Classification:
class adapters and Object adapters
Class Adapter:
Specific implementation: Class Adapter mode
/** * Target角色 */publicinterface FiveVolt { publicintgetVolt5();}
/**
* Adaptee characters, objects that need to be converted
*/
publicclass Volt220 { publicintgetVolt220() { return220; }}
Adapter role
publicclass ClassAdapter extends Volt220 implements FiveVolt { @Override publicintgetVolt5() { return5; }}
The target role gives the desired destination interface, while the Adaptee class is the object that needs to be converted. The adapter is the interface that converts the Volt220 into target. Corresponding to the target is to obtain a 5V output voltage, and adaptee that the normal output voltage is 220V, at this time we need the power adapter class to convert 220V voltage to 5V voltage, to solve the problem of incompatible interface.
publicclass Test { publicstaticvoidmain(String[] args) { new ClassAdapter(); System.out.println("输出电压 : " + adapter.getVolt5()); }}
Object adapter:
Specific implementations of object adapters
As with the class's adapter pattern, the object's adapter pattern transforms the API of the class being adapted into the API of the target class, unlike the class's adapter pattern, where the object's adapter pattern is not connected to the Adaptee class using an inheritance relationship, but instead uses a proxy relationship to connect to the Adaptee class.
The Adaptee Class (Volt220) does not have a getVolt5 () method, and the client expects this method. To enable the client to use the Adaptee class, you need to provide a wrapper class adapter. This wrapper class wraps a Adaptee instance so that the wrapper class can link the API of Adaptee with the API of the target class. Adapter and Adaptee are delegated relationships, which determines that the adapter pattern is an object.
The sample code is as follows:
/**
* Target Role
*/
publicinterface FiveVolt { publicintgetVolt5();}
/**
* Adaptee characters, objects that need to be converted
*/
publicclass Volt220 { publicintgetVolt220() { return220; }}
Object Adapter Mode
publicclass ObjectAdapter implements FiveVolt { Volt220 mVolt220; publicObjectAdapter(Volt220 adaptee) { mVolt220 = adaptee; } publicintgetVolt220() { return mVolt220.getVolt220(); } @Override publicintgetVolt5() { return5; }}
Note that in order to save code here, we have not followed some basic object-oriented principles.
Examples of Use:
publicclass Test { publicstaticvoidmain(String[] args) { new ObjectAdapter(new Volt220()); System.out.println("输出电压 : " + adapter.getVolt5()); }}
Schema structure: The adapter pattern contains the following roles:
Target: Object abstract class
Adapter: Adapter Class
Adaptee: Adapter Class
Client: Customer Class
Adapter mode has two implementations of object adapter and Class Adapter: Object adapter:
Class Adapter:
Advantages:
1. decouple the target class from the adapter class by introducing an adapter class to reuse the existing adapter class without modifying the original code.
2. The transparency and reusability of the classes are increased, and the specific implementations are encapsulated in the adapter class, which is transparent to the client class and improves the reusability of the adapter.
3. Flexibility and scalability are very good, by using the configuration file, you can easily replace the adapter, or can not modify the original code based on the addition of new adapter classes, fully comply with the "open and close principle."
The class adapter pattern also has the following advantages:
Because the adapter class is a subclass of the adaptive class, it is possible to displace some of the adapters ' methods in the adapter class, making the adapter more flexible.
The object adapter pattern also has the following advantages:
An object adapter can adapt multiple different adapters to the same target, that is, the same adapter can match the matching class and its subclasses to the target interface.
Cons: The disadvantages of the class adapter pattern are as follows:
For Java, C # and other languages that do not support multiple inheritance, a maximum of one adapter class at a time, and the target abstract class can only be abstract class, can not be a specific class, its use has some limitations, can not be an adapter class and its subclasses are suitable to the target interface.
The disadvantages of the object adapter pattern are as follows:
It is not easy to replace the method of the adapter class when compared to the class adaptor pattern. If we must replace one or more methods of the adaptive class, we have to do a subclass of the appropriate ligand class, replace the method of the adaptive class, and then adapt the sub-class of the adaptive class as the true adapter, and the process is more complicated.
My QR code is as follows, welcome to exchange discussion
You are welcome to pay attention to the "It question summary" subscription number. Every day to push the classic face test and interview tips, are dry! The QR code of the subscription number is as follows:
Reference:
Https://github.com/simple-android-framework-exchange/android_design_patterns_analysis/tree/master/adapter/mr.simple
Http://design-patterns.readthedocs.io/zh_CN/latest/structural_patterns/adapter.html
Http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html
Design mode-Adapter mode