The Adapter pattern converts the interface of a class into another interface that the customer wants. So that those classes that could not work together due to interface incompatibility can work together.
The adapter pattern has two different forms: class adapter and object adapter.
2, Structure of adapter mode
1. Target
Define a specific interface used by a client.
2. Client
Use the target interface to cooperate with objects consistent with the target interface.
3. Adaptee
An existing interface that needs to be adapted.
4. Adapter
Be responsible for converting the interface of Adaptee to the interface of Target. The adapter is a concrete class, which is the core of the pattern.
3, Advantages and disadvantages of adapter mode
1. Advantages
-
You can let any two classes that are not associated run together;
-
Increased class transparency;
-
The reusability of classes is improved;
-
Very flexible;
2. Shortcomings
-
Excessive use of adapters will make the system very messy;
-
Because java is single inheritance, only one adapter class can be adapted, and the target class must be abstract;
-
Only when the original design and code cannot be changed, the adapter mode will be considered;
4, Usage scenarios of adapter mode
1. The system needs to use existing classes, so the class interface does not meet the needs of the system.
2. I want to create a reusable class for some classes that are not closely related to each other.
3. Insert one class into another through interface transformation.
5, Class adapter
Adaptation through inheritance (inter class inheritance). The URL structure is as follows:
1,Target
Target target role, which defines the role to convert other classes into the desired interface. Usually, it is an interface or an abstract class, and generally it will not be an implementation class.
public interface Target { public void request(); }
2,Adaptee
Adaptee source role. The "who" you want to convert into the target role is the source role. It is an existing and well running class or object.
public class Adaptee { public void specificRequest() { System.out.println("Special request"); } }
3,Adapter
The Adapter role is the core role of the Adapter pattern. Its responsibility is to convert the source role into the target role by inheritance or class association.
public class Adapter extends Adaptee implements Target { @Override public void request() { super.specificRequest(); } }
4,ConcreteTarget
package designMode.adapter.sxn; public class ConcreteTarget implements Target { @Override public void request() { System.out.println("General request"); } }
5. Test class
package designMode.adapter.sxn; public class Client { public static void main(String[] args) { //Original business logic Target target = new ConcreteTarget(); target.request(); //Business logic after adding adapter Target target2 = new Adapter(); target2.request(); } }
6. Console output
6, Object adapter
Delegate through the association relationship at the object level (object composition relationship / association relationship). UML structure diagram is as follows:
1,Target
The interface expected by the customer. The target can be a concrete or abstract class or an interface.
package designMode.adapter.sxn; public class Target { public void request() { System.out.println("General request"); } }
2,Adaptee
Class that needs to be adapted.
package designMode.adapter.sxn; public class Adaptee { public void specificRequest() { System.out.println("Special request"); } }
3,Adapter
Convert the source interface into the target interface by wrapping an Adaptee object inside.
package designMode.adapter.sxn; public class Adapter extends Target{ private Adaptee adaptee = new Adaptee(); @Override public void request() { adaptee.specificRequest(); } }
4. Test class
package designMode.adapter.sxn; public class Client { public static void main(String[] args) { Target target = new Adapter(); target.request(); } }
5. Console output
7, Source code analysis of the application of adapter pattern in spring MVC framework
1. The HandlerAdapter in spring MVC uses the adapter pattern;
2. Reason analysis of using HandlerAdapter
We can see that the types of processors are different and there are multiple implementation methods, so the calling method is not determined. If you need to call the controller method directly, you have to constantly use ifelse to judge which sub class it is and then execute it. If you want to extend the controller later, you have to modify the original code, which violates the opening and closing principle.
end
Redis is based on memory and is often used as a technology for caching, and redis is stored in the form of key value. Redis is the most widely used cache in today's Internet technology architecture, which is often used in work. Redis is also one of the most popular questions asked by interviewers in the technical interview of middle and senior back-end engineers. Therefore, as a Java developer, redis is something we must master.
Redis is a leader in the field of NoSQL database. If you need to know how redis realizes high concurrency and massive data storage, this redis source code log note written by Tencent experts will be your best choice.
If you need to call the controller method directly, you have to constantly use ifelse to judge which subclass it is and then execute it. If you want to extend the controller later, you have to modify the original code, which violates the opening and closing principle.
end
Redis is based on memory and is often used as a technology for caching, and redis is stored in the form of key value. Redis is the most widely used cache in today's Internet technology architecture, which is often used in work. Redis is also one of the most popular questions asked by interviewers in the technical interview of middle and senior back-end engineers. Therefore, as a Java developer, redis is something we must master.
Redis is a leader in the field of NoSQL database. If you need to know how redis realizes high concurrency and massive data storage, this redis source code log note written by Tencent experts will be your best choice.
[external chain picture transferring... (img-eQM2b7YY-1630928363854)]