What is the adapter pattern?
What is the adapter pattern? I believe that many inexperienced people are at a loss about this, so this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
In computer programming, the adapter pattern adapts the interface of a class to what the user expects, and an adaptation allows classes that usually cannot work together because of interface incompatibility to work together by wrapping the class's own interface in an existing class.
There are two types of adapter patterns:
Object adapter pattern
In this adapter pattern, the adapter holds an instance of the class it wraps. In this case, the adapter invokes the physical entity of the wrapped object.
Class adapter pattern
In this adapter mode, the adapter inherits from the implemented class (usually multiple inheritance).
Explanation:
Convert the interface of one class to another interface that the customer wants. The Adapter pattern allows classes that cannot work together because of interface incompatibility.
Basic concept
Customer: the object that needs to call our code.
The purpose of the Adapter model is to retain the services provided by existing classes and provide customers with interfaces to meet customer expectations.
Main content
(1) Class adapter:
When the customer defines his desired behavior in the interface, we can apply the adapter pattern, provide a class that implements the interface, and extend the existing class to achieve adaptation by creating subclasses.
The following is a UML diagram of the class adapter:
(2) object adapter:
Object adapters reduce poor coupling between code by combining them in addition to meeting the "user-expected interface". Object adaptation is recommended in your work. The following is a UML diagram of the object adapter:
(3) default adapter mode:
The default adapter pattern is a special adapter pattern, but this adapter is implemented by an abstract class, and all the methods specified in the target interface are implemented in the abstract class, but many methods are "mediocre" implementations, that is, these methods are empty methods. All concrete subclasses inherit this abstract class.
After reading the above, have you mastered what the adapter pattern is? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!