Adapter mode
Basic introduction
- The adapter mode converts the interface of a class into another interface expected by the client. The main purpose is compatibility, so that the two classes that cannot work together due to interface mismatch can work together. Its alias is wrapper.
- The adapter mode is a structural mode.
- It is mainly divided into three categories: class adapter mode, object adapter mode and interface adapter mode.
- working principle:
- Adapter mode: convert the interface of one class to another, so that the classes with incompatible interfaces can be compatible.
- From the user's point of view, the Adaptee is decoupled.
- The user calls the target interface method transformed by the adapter, and the adapter then calls the relevant interface method of the Adaptee.
- The user receives the feedback result and feels that he is only interacting with the target interface.
Class Adapter
Code example
public class Voltage220 {
public int output220V(){
int src = 220;
System.out.println("220");
return src;
}
}
public interface Voltage5V {
int output5V();
}
public class VoltageAdapter extends Voltage220 implements Voltage5V{
@Override
public int output5V() {
System.out.println("5");
return output220V()/44;
}
}
public class Phone {
public void charging(Voltage5V voltage5V){
if (voltage5V.output5V() == 5){
System.out.println("charge");
}
}
}
public class Client {
public static void main(String[] args) {
Phone phone = new Phone();
phone.charging(new VoltageAdapter());
}
}
Precautions and details
- Java is a single inheritance mechanism, so the class adapter needs to inherit the src class, which is a disadvantage, because it requires that the dst class must be an interface, which has certain limitations.
- The methods of src class will be exposed in the Adapter, which also increases the cost of use.
- Because it inherits the src class, it can re the methods of the src class according to the requirements, which enhances the flexibility of the Adapter.
object adapter
Code example
public class VoltageAdapter implements Voltage5V {
private Voltage220 voltage220;
public VoltageAdapter(Voltage220 voltage220) {
this.voltage220 = voltage220;
}
public int output5V() {
int dst = 0;
if (voltage220 != null){
dst = voltage220.output220V()/44;
}
return dst;
}
}
public class Client {
public static void main(String[] args) {
Phone phone = new Phone();
phone.charging(new VoltageAdapter(new Voltage220()));
}
}
Precautions and details
- Object adapter and class adapter are actually the same idea, but they are implemented in different ways. According to the principle of composite reuse, composition is used to replace inheritance, so it solves the limitation that class adapters must inherit src, and it does not require dst to be an interface.
- Lower cost and more flexible.
- The object adapter pattern is a commonly used adapter pattern.
Interface adapter mode
Basic introduction
- When you do not need to implement all the methods provided by the interface, you can first design an abstract class to implement the interface and provide a default implementation for each method in the interface. Then the subclass of the abstract class can selectively override some methods of the parent class to implement the requirements.
Code example
public interface Interface {
void method1();
void method2();
void method3();
void method4();
}
public abstract class AbstractAdapter implements Interface{
@Override
public void method1() {
}
@Override
public void method2() {
}
@Override
public void method3() {
}
@Override
public void method4() {
}
}
public class Client {
public static void main(String[] args) {
AbstractAdapter abstractAdapter = new AbstractAdapter(){
@Override
public void method1() {
System.out.println("m1");
}
};
abstractAdapter.method1();
}
}
Precautions and details
- The three naming methods are named according to how src is given to the Adapter.
- Class Adapter: given as a class, in the Adapter, src is inherited as a class.
- Object Adapter: given by object, src is held as an object in the Adapter.
- Interface Adapter: the interface is given to. In the Adapter, src is implemented as an interface.
- The biggest function of the Adapter pattern is to integrate incompatible interfaces.
- In the actual development, the implementation is not limited to these three classical modes.