This is the 28th day of my participation in Gwen Challenge
👉 Design mode directory
preface
Adapter pattern belongs to the structural model, in the previous article also talked about the proxy pattern, the proxy pattern and structural pattern, the rest of the structural model of the bridge models, filters, combination mode, decorator pattern, appearance, the flyweight pattern, and create the model of already all finished introduction, including the factory pattern, singleton pattern, construction model, prototype model.
What is an Adapter pattern
concept
The Adapter pattern is defined as follows: Converts the interface of one class into another interface that the user expects, so that classes that would otherwise not work together can work together due to interface incompatibations.
In order for class A to be able to use class B, create A new class C to convert class B into A class that class A can use.
This C class is an adapter.
Just like the Chinese who don’t know foreign languages want to communicate with foreigners, they need to bring a translator to be able to communicate. Phones that now support charging ports, for example, need a connector to plug in their wired headphones. The translation just translates for the Chinese, and the adapter just turns the head of the headset into the head of the proper charging port, and doesn’t do anything else with them.
Class structure adapters versus object structure adapters
To subdivide, it can be divided into two types: class structure mode and object structure mode. The former is to create adapter classes through inheritance, with a high degree of coupling, while the latter is to create adapter classes through combination, with a low degree of coupling, and generally use the object structure mode.
From the point of view of implementation, the development difficulty of both is similar, but because the class structure adapter is implemented in the way of inheritance, can also be said to be a static implementation, it and the parent class will have a high degree of coupling, it will be more troublesome to replace the adapted class. The object structure adapter is implemented by referring to the object, which can also be said to be a dynamic way of implementation. The coupling degree is low and the flexibility is high. If the internal member object is referenced by the interface type, the object can be flexibly replaced.
advantages
- In line with the open and closed principle, decoupling. Changes to requirements can be made without modifying classes A and B.
- Improved class reuse.
- Improved system flexibility.
disadvantages
- Cluttered the system. Excessive use of adapters can lead to a loose system structure and should be used according to specific business requirements.
- Reduce code readability.
- The implementation process is tedious.
The principle of
“+” for compliance, “-” for non-compliance or irrelevant
The principle of | Open the closed | Single responsibility | Di milt | Replacement on the Richter scale | Dependency inversion | Interface segregation | Synthesis of reuse |
---|---|---|---|---|---|---|---|
+ | + | – | – | + | + | – | |
Applicable Scenario (Motivation)
A system that is already developed and running will not undergo a major change, so if we want to make some changes, we can use the adapter pattern, again depending on the requirements.
- You need to reuse a class that doesn’t quite match the interface you’re defining.
- Encapsulate a flawed interface design.
- Compatible with interfaces of earlier versions. All three cases are defects left over from the original design of the software, but it is not possible to fix them, and this class is used in many places.
- Create an interface that can be used in multiple places to unify the interface design of multiple classes.
- Adapt to different formats of data. The new changes require modifying the return result or incoming parameter of the target class.
How to implement
To implement an adapter, you need three things:
- Target class: The class that needs to be adapted
- Adapter interface: The interface that defines the adapter
- Adapter class: Adaptor target class
Class structure adapter
Here is a simple implementation of a class structure adapter, and you can use your imagination to implement more complex examples.
The class diagram
In the code
Here is a simple implementation of an adaptation of the old class.
Target class: Target
/** * Target class * Created on 2021/5/29@author xuxiaobai
*/
public class Target {
public void call(a){
System.out.println("call"); }}Copy the code
Adapter Interface: Adapter
/** * Adapter interface * Created on 2021/5/29. **@author xuxiaobai
*/
public interface Adapter {
void say(a);
}
Copy the code
Adapter class: TargetAdapter
/** * Target class adapter * Created on 2021/5/29. **@author xuxiaobai
*/
public class TargetAdapter extends Target implements Adapter{
@Override
public void say(a) {
System.out.print("say replace "); call(); }}Copy the code
Test class: AdapterTest
/**
* Created on 2021/5/29.
*
* @author xuxiaobai
*/
public class AdapterTest {
public static void main(String[] args) {
Adapter adapter = new TargetAdapter();
adapter.say();
/** Output: say replace call */}}Copy the code
Object structure adapter
I’m going to do the class structure adapter, the object structure adapter for your homework, not because I’m lazy, but to improve your hands-on skills, and the class diagram is attached below.
The class diagram
As you can see, the Target does not use inheritance. Instead, it refers to the object by creating a member attribute in the TargetAdapter. The object in the TargetAdapter suggests either an interface or an abstract class, instead using this class.
conclusion
The adapter pattern basically follows seven design principles and is a good design pattern, but it is not used very often during development and is generally used as a patch.
As I learned, the adapter pattern, facade pattern, and proxy pattern were similar to each other, but not exactly.
Both these models are to the secondary packaging of a class, the proxy pattern is on the basis of the original class to add some does not involve the function of the business, and the adapter pattern is to let the old class conforms to the interface, a class can be used for another class, did not increase any new function point, and facade pattern is encapsulated within the business logic.
— — — — — — — — — — — — — — —
The more you know, the more you don’t know.
If you have any questions about the content of this article, please comment directly or email me. If you think my writing is good, a “like” is also a sign of support
Shall not be reproduced without permission!