Adapter pattern of Java design pattern
- Basic introduction
(1) Adapter pattern converts the interface of a class into another interface representation expected by the client. The main purpose is compatibility, so that two classes that cannot work together due to interface mismatch can work together. Its alias is wrapper
(2) The adapter mode is a structural mode
(3) It is mainly divided into three categories: class adapter mode, object adapter mode and interface adapter mode - working principle
(1) Adapter mode: convert the interface of one class to another Make the classes whose original interfaces are incompatible compatible
(2) From the user's point of view, there is no Adaptee, which is decoupled
(3) The user calls the target interface method transformed by the adapter, and then the adapter calls the relevant interface method of the Adaptee
(4) After receiving the feedback result, the user feels that he is only interacting with the target interface, as shown in the figure
- Class adapter mode
(1) Basic introduction
Adapter class, by inheriting src class, implements dst class interface and completes the adaptation of src - > dst.
(2) Application instance of class adapter
① Problem description
Take the example of charger in life to explain the Adapter. The charger itself is equivalent to the Adapter, 220V AC is equivalent to src (i.e. the Adaptee), and our target DST (i.e. the target) is 5V DC
② Train of thought analysis class diagram
③ Code implementation
public class Client { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(" === Class adapter mode ===="); Phone phone = new Phone(); phone.charging(new VoltageAdapter()); } } //Adapter interface public interface IVoltage5V { public int output5V(); } public class Phone { //charge public void charging(IVoltage5V iVoltage5V) { if(iVoltage5V.output5V() == 5) { System.out.println("The voltage is 5 V, Can charge~~"); } else if (iVoltage5V.output5V() > 5) { System.out.println("Voltage greater than 5 V, Can't charge~~"); } } } //Adapted class public class Voltage220V { //Output 220V voltage public int output220V() { int src = 220; System.out.println("Voltage=" + src + "Crouch"); return src; } } //adapter class public class VoltageAdapter extends Voltage220V implements IVoltage5V { @Override public int output5V() { // TODO Auto-generated method stub //220V voltage obtained int srcV = output220V(); int dstV = srcV / 44 ; //Convert to 5v return dstV; } }
④ Class adapter pattern considerations and details
-Java is a single inheritance mechanism, so the class adapter needs to inherit src class, which is a disadvantage, because it requires dst to be an interface, which has certain limitations;
-The methods of src class will be exposed in the Adapter, which also increases the cost of use.
-Because it inherits the src class, it can rewrite the methods of the src class according to the requirements, which enhances the flexibility of the Adapter.
4. Object adapter mode
(1) Basic introduction
① The basic idea is to hold the instance of src class in the Adapter class to solve the problem of compatibility. That is: hold src class, implement dst class interface, and complete the adaptation of src - > dst
② According to the "composite Reuse Principle", try to use association relationship (aggregation) in the system to replace inheritance relationship
③ Object adapter pattern is a common pattern of adapter pattern
(2) Application case code
① Train of thought analysis class diagram
② Code implementation
public class Client { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(" === Object Adapter Pattern ===="); Phone phone = new Phone(); phone.charging(new VoltageAdapter(new Voltage220V())); } } //Adapter interface public interface IVoltage5V { public int output5V(); } public class Phone { //charge public void charging(IVoltage5V iVoltage5V) { if(iVoltage5V.output5V() == 5) { System.out.println("The voltage is 5 V, Can charge~~"); } else if (iVoltage5V.output5V() > 5) { System.out.println("Voltage greater than 5 V, Can't charge~~"); } } } //Adapted class public class Voltage220V { //Output 220V voltage, unchanged public int output220V() { int src = 220; System.out.println("Voltage=" + src + "Crouch"); return src; } } //adapter class public class VoltageAdapter implements IVoltage5V { private Voltage220V voltage220V; // Association - aggregation //Pass in a Voltage220V instance through the constructor public VoltageAdapter(Voltage220V voltage220v) { this.voltage220V = voltage220v; } @Override public int output5V() { int dst = 0; if(null != voltage220V) { int src = voltage220V.output220V();//Obtain 220V voltage System.out.println("Use the object adapter for adaptation~~"); dst = src / 44; System.out.println("After adaptation, the output voltage is=" + dst); } return dst; } }
③ Object adapter pattern considerations and details
-In fact, it is the same way to implement the adapter as the object. According to the principle of composite reuse, composition is used to replace inheritance, so it solves the limitation that class adapters must inherit src, and it no longer requires dst to be an interface
-Lower cost and more flexible.
5. Interface adapter mode
(1) Basic introduction
① Some books are called: default adapter pattern or default adapter pattern
② When there is a way to implement some of the abstract methods of the parent class, you need to provide a method to implement all the abstract methods of the interface by default
③ This applies when an interface does not want to use all its methods
(2) Application case
① The attribute animation ValueAnimator class in Android can add listeners through the addListener(AnimatorListener listener) method
② Sometimes we don't want to implement animator All methods of animatorlistener interface. We just want to listen to onAnimationStart
③ AnimatorListenerAdapter class is a
Interface adapter, the code is shown in the right figure: it is empty
Animator. All methods of animatorlistener class (src)
④ AnimatorListener is an interface
⑤ The anonymous inner class in the program is the Listener implementation class
⑥ Case description
⑦ Code implementation
public interface Interface4 { public void m1(); public void m2(); public void m3(); public void m4(); } //In AbsAdapter, we implement the method of Interface4 by default public abstract class AbsAdapter implements Interface4 { //Default implementation public void m1() {} public void m2() {} public void m3() {} public void m4() {} } public class Client { public static void main(String[] args) { AbsAdapter absAdapter = new AbsAdapter() { //We just need to override. We need to use interface methods @Override public void m1() { // TODO Auto-generated method stub System.out.println("Used m1 Method of"); } }; absAdapter.m1(); } }
- Application of adapter pattern in the source code of spring MVC framework
(1) The HandlerAdapter in spring MVC uses the adapter pattern
(2) Code analysis + Debug source code
explain:
① spring defines an adapter interface so that each controller has a corresponding adapter implementation class
② The adapter executes the corresponding method instead of the controller
③ To extend the controller, you only need to add an adapter
(3) Class diagram
- Adapter mode considerations and details
(1) The three naming methods are named according to the form src gives to the Adapter (the form in the Adapter)
Class Adapter: given by class, in the Adapter, src is regarded as a class and inherited
Object Adapter: given by object, src is regarded as an object and held in the Adapter
Interface Adapter: it is given by interface. In the Adapter, src is used as an interface to implement
② The biggest function of Adapter mode is to integrate incompatible interfaces