Related links:
[design mode] column: [design mode] column
Relevant examples can be downloaded: Examples of common Java design patterns
Adapter mode
preface
In real life, there are often instances where two objects cannot work together due to incompatible interfaces. At this time, a third party needs to adapt. For example, a Chinese speaker needs an interpreter when talking to an English speaker, a power adapter when using a DC laptop to connect to an AC power supply, a card reader when using a computer to access the SD memory card of the camera, etc.
Adapter Pattern is a bridge between two incompatible interfaces. This type of design pattern belongs to structural pattern, which combines the functions of two independent interfaces.
-
Convert the interface of a class into another interface desired by the customer, so that those classes that cannot work together due to incompatible interfaces can work together.
-
Adapter patterns are divided into class structured patterns and object structured patterns. The former has a higher degree of coupling between classes than the latter, and requires programmers to understand the internal structure of relevant components in the existing component library, so there are relatively few applications
Take a chestnut
The above figure illustrates the function of the adapter
advantage
-
The client can call the target interface transparently through the adapter
-
It improves the reuse of classes and reuses existing classes. Programmers do not need to modify the original code and reuse the existing adapter classes
-
The target class and adapter class are decoupled to solve the problem of inconsistent interface between target class and adapter class
-
Good flexibility
-
Comply with the "open close principle"
shortcoming
-
The adapter writing process needs to be comprehensively considered in combination with the business scenario, which may increase the complexity of the system
-
Increase the difficulty of code reading and reduce the readability of code
-
Excessive use of adapters will make the system very messy and difficult to grasp as a whole
Role of adapter
The Adapter pattern contains the following main roles.
Target interface
The interface expected by the current system business, which can be an abstract class or interface.
Adapter class
It is the component interface in the existing component library that is accessed and adapted
Adapter class
It is a converter that converts the adapter interface into the target interface by inheriting or referencing the adapter object, so that customers can access the adapter according to the format of the target interface
Sample code
Target interface
/** * Adapter mode - target interface * */ public interface Target { /** * Normal request */ public void request(); }
Adapter class
/** * Adapter mode - adapter * */ public class Adaptee { /** * Special request */ public void specialRequest() { System.out.println("The business code in the adapter is called"); } }
adapter class
Structure of class adapter pattern
/** * Adapter mode - class adapter * */ public class ClassAdapter extends Adaptee implements Target { /** * Override normal request method */ @Override public void request() { // Call special request specialRequest(); } }
/** * Adapter mode - class adapter mode */ public class ClassAdapterDemo { public static void main(String[] args) { // Create target object through adapter Target target = new ClassAdapter(); target.request(); } }
Structure of object adapter pattern
/** * Adapter mode - object adapter * */ public class ObjectAdapter implements Target { /** Create adapter object */ private Adaptee adaptee; /** * Construction method - specify the adapter * @param adaptee */ public ObjectAdapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public void request() { // Call special request adaptee.specialRequest(); } }
/** * Adapter mode - object adapter mode * */ public class ObjectAdapterDemo { public static void main(String[] args) { // Create adapter object Adaptee adaptee = new Adaptee(); // Create target object through adapter Target target = new ObjectAdapter(adaptee); target.request(); } }
Conclusion
1. See more about design patterns [design mode] column
2. Relevant example codes can be downloaded: Examples of common Java design patterns
PS: [ Examples of common Java design patterns Included in] [design mode] column The code involved in the code can be downloaded by students who need the code. If you have downloaded it before, you don't need to download it again~
If the above contents are incorrect or need to be supplemented, please consult more and update and correct them in time~
Welcome to comment ~ thank you for your praise~