Introduction to the Adapter pattern: The Adapter pattern has a high usage rate in development. The Adapter is a combination of two incompatible classes. It is a bit like an adhesive. It makes different things work together through a transformation. For example, we often encounter interaction between two unrelated classes. The first solution is to modify the interfaces of their respective classes, but what if there is no source code or we are unwilling to modify their interfaces for an application? In this case, we often use an Adapter to create a "hybrid" interface between the two interfaces, which will be compatible with the two interfaces and meet the requirements without modifying the source code.
Definition of the adapter pattern: The adapter pattern transforms the interface of one class into another interface expected by the client so that two classes that could not work together because of interface mismatch can work together.
Adapter usage scenarios:
1 Systems need to use existing classes, and such interfaces do not meet the needs of the system, that is, interface incompatibility.
2. Want to create a reusable class for working with some classes that are not too closely related to each other, including some that may be introduced in the future
3. A unified output interface is required, and the type of input is unpredictable.
Taking power interface as an example, the power supply of notebook computers is generally 5V voltage, but the voltage of wires in our life is generally 220V. At this time, there is a mismatch. In the development of software, we call it interface incompatibility. At this time, adapter is needed to carry out an interface conversion. In the software development, the power supply of notebook computers is usually 220V. There is a saying that reflects this point: any problem can be solved by adding a middle layer. This layer can be understood as the Adapter layer here, through which an interface transformation can achieve compatibility.
There are two types of adapter modes:
1) Class adapter mode
2) Object adapter pattern
First, the first type of adapter mode:
The interface of 5V voltage is as follows:
1 /** 2 * Target The role is to be converted to 5V 3 */ 4 public interface FiveVolt { 5 int getVolt5(); 6 }
220V voltage, need to be converted
1 /** 2 * 220V Voltage, converted class 3 */ 4 public class Volt220 { 5 public int getVolt220(){ 6 return 220; 7 } 8 }
Here's the Adapter role, which converts 220V voltage to 5V voltage
1 /** 2 * The adapter, which has 5V and 220V, is compatible on both sides, just like the switch head. 3 */ 4 public class ClassVoltAdapter extends Volt220 implements FiveVolt { 5 @Override 6 public int getVolt5() { 7 return 5; 8 } 9 }
See, there are 220V and 5V in the above lassVolt Adapter. It can also be used if it needs 5V or 220V.
The following are the test classes for the class adapter pattern:
1 /** 2 * Here are the test classes for the class adapter pattern 3 */ 4 public class ClassAdapterTest { 5 public static void main(String[] args){ 6 test(); 7 } 8 9 public static void test(){ 10 11 ClassVoltAdapter voltAdapter = new ClassVoltAdapter(); 12 System.out.println(voltAdapter.getVolt5()); 13 14 } 15 16 }
Let's look at the second adapter pattern: the object adapter pattern.
First, the front 5V interface is the same as the 220V class. This is just to post the code for easy viewing.
The interface of 5V voltage is as follows:
1 /** 2 * Target The role is to be converted to 5V 3 */ 4 public interface FiveVolt { 5 int getVolt5(); 6 }
220V Voltage Class
/** * 220V Voltage, converted class */ public class Volt220 { public int getVolt220(){ return 220; } }
Let's take a look at the object adapter class. It mainly implements the target interface and saves a reference to the object of a transformed class. The code is as follows:
1 /** 2 * Object adapter, which implements the target interface and saves a reference to the converted object 3 */ 4 public class ObjectVoltAdapter implements FiveVolt{ 5 //Save a reference to the converted object 6 Volt220 volt220; 7 8 public ObjectVoltAdapter(Volt220 volt220){ 9 this.volt220 = volt220; 10 } 11 12 @Override 13 public int getVolt5() { 14 return 5; 15 } 16 17 public int getVolt220(){ 18 return volt220.getVolt220(); 19 } 20 }
The following is the object adapter pattern test class:
1 /** 2 * Test classes for object adapters 3 */ 4 public class ObjectVoltAdapterTest { 5 public static void main(String[] args){ 6 test(); 7 } 8 9 public static void test(){ 10 //Create a 220 V The object of the voltage class, that is, the object of the transformed class 11 Volt220 volt220 = new Volt220(); 12 //Create an object of an object adapter class and save the object of a transformed class 13 ObjectVoltAdapter voltAdapter = new ObjectVoltAdapter(volt220); 14 System.out.println(voltAdapter.getVolt5()); 15 } 16 }