Article catalog
1, Problems of using sockets in tourism in Thailand
2, Basic introduction to adapter mode
1. Introduction to class adapter mode
2. Application instance of class adapter mode
3. Precautions and details of class adapter mode
1. Introduction to object adapter mode
2. Application instance of object adapter mode
3. Precautions and details of class adapter mode
1. Introduction to interface adapter mode
2. Application example of interface adapter mode
4, Considerations and details of adapter mode
preface
This article briefly introduces the adapter pattern.
Tip: the following is the main content of this article. The following cases can be used for reference
1, Problems of using sockets in tourism in Thailand
The Thai socket is two hole (European standard). You can buy a multifunctional conversion plug (adapter) so that it can be used.
2, Basic introduction to adapter mode
- Adapter pattern converts the interface of a class into another interface expected by the client. The main purpose is compatibility, so that 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.
3, How the adapter mode works
- Adapter pattern: convert the interface of one class to another Make classes that are incompatible with the original interface 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.
- After receiving the feedback result, the user feels that he is only interacting with the target interface, as shown in the figure.
4, Class adapter mode
1. Introduction to class adapter mode
Basic introduction: the Adapter class inherits the src class, implements the dst class interface, and completes the adaptation of src - > dst.
2. Application instance of class adapter mode
- Application example description: take the charger in life as an example to explain the Adapter. The charger itself is equivalent to the Adapter, 220V AC is equivalent to src (i.e. the Adaptee), and our target DST (i.e. the target) is 5V DC
- Train of thought analysis (class diagram)
- code implementation
//Adapted class public class Voltage220V { //Output 220V voltage public int output220V() { int src = 220; System.out.println("Voltage=" + src + "Crouch"); return src; } }
//Adapter interface public interface IVoltage5V { public int output5V(); }
//adapter class public class VoltageAdapter extends Voltage220V implements IVoltage5V { @Override public int output5V() { //220V voltage obtained int srcV = output220V(); //Turn to 5V int dstV = srcV / 44 ; return dstV; } }
public class Phone { //charge public void charging(IVoltage5V iVoltage5V) { if(iVoltage5V.output5V() == 5) { System.out.println("The voltage is 5 V,Can charge~~"); } else if (iVoltage5V.output5V() > 5) { System.out.println("Voltage greater than 5 V, Can't charge~~"); } } }
public class Client { public static void main(String[] args) { System.out.println(" === Class adapter mode ===="); Phone phone = new Phone(); phone.charging(new VoltageAdapter()); } }
3. Precautions and details of class adapter mode
- Java is a single inheritance mechanism, so the class adapter needs to inherit src class, which is a disadvantage, because it requires dst to 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 rewrite the methods of the src class according to requirements, which enhances the flexibility of the Adapter.
5, Object adapter mode
1. Introduction to object adapter mode
- The basic idea is the same as the Adapter mode of the class, except that the Adapter class is modified instead of inheriting the src class, but holding an instance of the src class to solve the compatibility problem. That is, hold the src class, implement the dst class interface, and complete the adaptation of src - > dst.
- According to the "composite Reuse Principle", try to use association relationship to replace inheritance relationship in the system.
- The object adapter pattern is a commonly used adapter pattern.
2. Application instance of object adapter mode
- Application example description: take the charger in life as an example to explain the Adapter. The charger itself is equivalent to the Adapter, 220V AC is equivalent to src (i.e. the Adaptee), and our DST (i.e. the target) is 5V DC, which is completed using the object Adapter mode.
- Train of thought analysis (class diagram): just modify the adapter, as follows:
- code implementation
//Adapted class public class Voltage220V { //Output 220V voltage public int output220V() { int src = 220; System.out.println("Voltage=" + src + "Crouch"); return src; } }
//Adapter interface public interface IVoltage5V { public int output5V(); }
//adapter class public class VoltageAdapter implements IVoltage5V { private Voltage220V voltage220V; // Association - aggregation //Pass in a Voltage220V instance through the constructor public VoltageAdapter(Voltage220V voltage220v) { this.voltage220V = voltage220v; } @Override public int output5V() { int dst = 0; if(null != voltage220V) { //Obtain 220V voltage int src = voltage220V.output220V(); System.out.println("Using object adapters,Adapt~~"); dst = src / 44; System.out.println("Adaptation complete,The output voltage is=" + dst); } return dst; } }
public class Phone { //charge public void charging(IVoltage5V iVoltage5V) { if(iVoltage5V.output5V() == 5) { System.out.println("The voltage is 5 V, Can charge~~"); } else if (iVoltage5V.output5V() > 5) { System.out.println("Voltage greater than 5 V, Can't charge~~"); } } }
public class Client { public static void main(String[] args) { System.out.println(" === Object Adapter Pattern ===="); Phone phone = new Phone(); phone.charging(new VoltageAdapter(new Voltage220V())); } }
3. Precautions and details of class adapter mode
- 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 no longer requires dst to be an interface.
- Lower cost and more flexible.
4, Interface adapter mode
1. Introduction to interface adapter mode
- Some books are called: default adapter pattern or default adapter pattern.
- When you do not need to implement all the methods provided by the interface, you can first design an abstract class implementation interface and provide a default implementation (empty method) for each method in the interface. Then the subclass of the abstract class can selectively override some methods of the parent class to meet the requirements.
- This applies when an interface does not want to use all its methods.
2. Application example of interface adapter mode
- Train of thought analysis (class diagram)
- code implementation
public interface Interface4 { public void m1(); public void m2(); public void m3(); public void m4(); }
//In AbsAdapter, we implement the method of Interface4 by default public abstract class AbsAdapter implements Interface4 { //Default implementation @Override public void m1() {}; @Override public void m2() {} @Override public void m3() {} @Override public void m4() {} }
public class Client { public static void main(String[] args) { AbsAdapter absAdapter = new AbsAdapter() { //Just override the interface methods we need to use @Override public void m1() { System.out.println("Used m1 Method of"); } }; absAdapter.m1(); } }
4, Considerations and details of adapter mode
- The three naming methods are named according to the form in which src is given to the Adapter (the form in 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 the three classic forms we explain.
summary
This article only briefly introduces the adapter pattern in the design pattern.