Adapter is an adaptation of the middleware, it exists in the mismatch between the two, for the connection between the two, the mismatch becomes a match, simple point of view is commonly seen in the adapter, such as the existence of the converter.
There are two types of adapter modes: Class Adapter, object adapter, interface adapter
The first two in the implementation of a little difference, the same effect, the third interface adapter is a large difference.
1. Class Adapter Mode:
Principle: The adapter function is implemented through inheritance.
When we want to access the interface A does not have the method we want, but in another interface B found the appropriate method, we can not change the access interface A, in this case, we may define an adapter p to relay, the adapter p to implement the interface a we access, So that we can continue to access the current method in interface a (although it is not our dish at present), and then inherit the implementation class BB of interface B, so that we can access the method of interface B in the adapter p, then we directly refer to the appropriate method in BB in the interface a method in adapter p. This completes a simple class adapter.
See examples below: We take PS2 and USB as an example
PS2 interface: PS2
Interface PS2 {2 void isPs2 (); 3}
USB Interface: USB
Interface USB {2 void isusb (); 3}
USB interface Implementation class: Usber
Implements USB { @Overridevoid Isusb () {5 System.out.println ("USB port"); } 8}
Adapter: Adapter
Implements Ps2 { @Overridevoid isPs2 () { isusb (); } 8}
Test method: Clienter
Class Clienter {void main (string[] args) {new Adapter (); p.isps2 (); } 8}
Show Results:
USB port
Example Explanation:
My hand has a PS2 plug device, but the host only a USB plug socket, how to do? Get a converter and convert the PS2 plug into a USB plug to use.
Interface Ps2: Describes the PS2 interface format
Interface USB: Description USB Interface Format
Class Usber: Is the interface USB implementation class, is the specific USB interface format
Adapter: Used to convert the PS2 interface format into a USB interface format
2. Object Adapter Mode
Principle: The adapter function is implemented by combination.
When we want to access the interface A does not have the method we want, but in another interface B found the appropriate method, we can not change the access interface A, in this case, we may define an adapter p to relay, the adapter p to implement the interface a we access, So we can continue to access the method in current interface A (although it is not our dish at present), then define the private variable C (object) in adapter p (b interface to the variable name), and then define a constructor with parameters to assign a value to object C, Then use object C in the method implementation of the A interface to invoke its method from the B interface.
See example below: We still take PS2 and USB as an example
PS2 interface: PS2
Interface PS2 {2 void isPs2 (); 3}
USB Interface: USB
Interface USB {2 void isusb (); 3}
USB interface Implementation class: Usber
Implements USB { @Overridevoid Isusb () {5 System.out.println ("USB port"); } 8}
Adapter: Adapter
1PublicClass AdapterImplementsPS2 {2 3 private USB Usb; 4 public Adapter (USB USB) { 5 this.usb = Usb; 6 } 7 8 public void< Span style= "COLOR: #000000" > IsPs2 () { 9 Usb.isusb (); 10 }11 12}
Test class: Clienter
Class Clienter {void main (string[] args) {new Adapter (new usber ()); p.isps2 (); } 8}
The results show:
USB port
Example Explanation:
My hand has a PS2 plug device, but the host only a USB plug socket, how to do? Get a converter and convert the PS2 plug into a USB plug to use.
Interface Ps2: Describes the PS2 interface format
Interface USB: Description USB Interface Format
Class Usber: Is the interface USB implementation class, is the specific USB interface format
Adapter: Used to convert the PS2 interface format into a USB interface format
3. Interface Adapter Mode
Principle: The adaptation is achieved by means of an abstract class, which is adapted slightly to the adaptation described above.
When there is such an interface, which defines n-many methods, and we now only want to use one to several methods, if we implement the interface directly, then we have to implement all the methods, even if we just do not need to empty the method (write a pair of curly braces, Does not do the concrete method implementation) also can cause this class to become bloated, the invocation also is inconvenient, at this time we can use an abstract class as the middleware, namely the adapter, uses this abstract class to implement the interface, but in the abstract class all methods are empty, then we create the abstract class inheritance class, And rewrite the few methods that we need to use.
Target interface: A
1 public interface A {2 void A (); 3 void b (); 4 void C (); 5 void D (); 6 void E (); 7 void F (); 8}
Adapter: Adapter
1PublicAbstractClass AdapterImplementsA2public void A () {}3 public void b () {}4 public Void C () {}5 public void D () {}6 public void E () {} 7 public void F () {}8}
Implementation class: Ashili
1 public class ashili extends Adapter {2 public void< Span style= "COLOR: #000000" > A () {3 System.out.println ("implement a method called" ); 4 }5 public void D () {6 SYSTEM.OUT.PRINTLN ("Implement D method called" 7 }8}
Test class: Clienter
Class Clienter {void main (string[] args) {new Ashili (); a.a (); A.D (); } 9}
4. Adapter Mode Application Scenario
The class adapter is consistent with the object adapter's usage scenario, which is only slightly different from the implementation, and is mainly used for the following scenarios:
(1) If you want to use a class that already exists, but it does not conform to the existing interface specifications, resulting in the inability to access directly, creating an adapter can indirectly access the methods in this class.
(2) We have a class that we want to design as a reusable class (which can be accessed in multiple places), and we can create an adapter to adapt this class to other classes that do not provide the appropriate interface.
The above two scenarios are actually from two angles to describe a class of problems, that is to access the method is not in the appropriate interface, one from the interface (is accessed), one from the access (active access).
Interface Adapter Usage Scenarios:
(1) To use one or some of the methods in the interface, but there are too many methods in the interface, we have to implement the interface and implement all of the methods, we can use the abstract class to implement the interface, do not implement the method (empty only), and then we inherit this abstract class by rewriting the way we want to do it. This abstract class is the adapter.
"Adapter Mode" and application scenario in Java design mode