Concept
Convert an interface into another interface that the customer wants. (This mode allows classes that are otherwise incompatible to work together).
UML diagram
The adapter mode has two different forms of adapter mode for the class and the adapter mode of the object .
(1) object Adapter mode structure diagram
(2) class of Adapter mode structure diagram
The roles involved in the pattern are:
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.
The difference between an object's adapter pattern is that the adapter role encapsulates the adaptee role rather than the inheritance that the class's adapter pattern takes. The principle is basically similar
Realize:
The adapter mode of the object
Public classAdapterImplementstarget{Privateadaptee adaptee; PublicAdapter () {Super(); This. Adaptee =Newadaptee (); } @Override Public voidGetHeadset2 () {adaptee.getheadset3 (); } Public Static voidMain (String args[]) {target target=NewAdapter (); Target.getheadset2 (); }}Interfacetarget{voidGetHeadset2 ();}classadaptee{ Public voidGetHeadset3 () {System.out.println ("I'm a 3.5mm headset."); }}
class's Adapter mode
Public classAdapterextendsAdapteeImplementstarget{@Override Public voidGetHeadset2 () { This. GetHeadset3 (); } Public Static voidMain (String args[]) {target target=NewAdapter (); Target.getheadset2 (); }}
Interfacetarget{voidGetHeadset2 ();}
classadaptee{ Public voidGetHeadset3 () {System.out.println ("I'm a 3.5mm headset."); }}
Default Adapter Mode
The default adapter pattern is a special adapter pattern, but this adapter is implemented by an abstract class, and all methods specified in the target interface are implemented in an abstract class, but many methods are implemented as NULL methods. And the specific subclass inherits this abstract class.
the adapter mode is intended to change the interface of the source so that it is compatible with the target interface. The default adaptation is slightly different , and it is a mediocre implementation provided to facilitate the creation of a non-mediocre adapter class.
At any time, if you are not ready to implement all of the methods of an interface, you can use the default adaptation mode to create an abstract class that gives a concrete implementation of the mediocrity of all methods. Thus, subclasses that inherit from this abstract class do not have to implement all the methods.
Adapter Adaptation degree
1. Fully adaptable
If the number of methods in the target interface is equal to the number of methods used by the adapter interface, then adapters can be fully adapted to the target interface by the adaptor interface
2. Not fully adapted
If the number of methods in the target interface is less than the number of methods that are used by the adapter interface, then adapters can only be adapted to the target interface by the adaptor interface
3, the remaining adaptation
If the number of methods in the target interface is greater than the number of methods for the adapter interface. The adapter can then fully adapt the interface to the target interface, but the target superfluous method must be given to the user to allow the default implementation
Benefits of Adapter mode
The system needs to use the existing classes, and the interfaces of this class do not meet the needs of the system. The adapter mode allows for better reuse of these features.
When implementing the adapter functionality, you can invoke the features you have developed to naturally extend the functionality of the system.
Disadvantages of Adapter Mode
Excessive use of the adapter, will make the system very messy, not easy to grasp the overall. For example, clearly see the call is a interface, in fact, the interior is adapted to the implementation of the B interface, a system if too many occurrences of this situation, is tantamount to a disaster. So if it's not necessary, you can refactor the system without using the adapter.
Adapter Mode Application Scenario
1. The system needs to use the existing classes, and the interfaces of this class do not meet the needs of the system.
2. You want to create a reusable class that works with some classes that are not too much related to each other, including some that might be introduced in the future. These source classes do not necessarily have very complex interfaces.
3. (for object adapters) in the design, you need to change the interfaces of several existing subclasses, and if you use the class's adapter pattern, you should make an adapter for each subclass, which is not practical.
References from:
http://blog.csdn.net/qq7342272/article/details/6921251
Http://www.cnblogs.com/ikuman/archive/2013/01/26/2866755.html
Http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html
Java_ Design Mode _ Adapter Mode _adapter pattern (2016-08-09)