Writing in the front

  • Take notes on learning design patterns
  • Improve flexibility in the use of design patterns

Learning to address

https://www.bilibili.com/vide…

https://www.bilibili.com/vide…

Refer to the article

http://c.biancheng.net/view/1…

Project source https://gitee.com/zhuang-kang/DesignPattern

10, Adapter pattern

10.1 Definition and characteristics of adapter patterns

The Adapter pattern is defined as converting the interface of one class to another interface desired by the customer, enabling classes that would otherwise not work together due to incompatible interfaces to work together. Adapter patterns can be divided into class structural patterns and object structural patterns, with the former having a higher degree of coupling between classes than the latter.

The main advantages of this pattern are as follows:

  • The client can call the target interface transparently through the adapter.
  • Reusing existing classes allows programmers to reuse existing adapter classes without modifying the original code.
  • The problem of inconsistent interface between target class and adaptor class is solved by decoupling the target class and adaptor class.
  • It follows the Open Closed Principle in many business scenarios.

Its disadvantages are:

  • The process of writing adapters requires comprehensive consideration in the context of business scenarios, which may increase the complexity of the system.
  • Make the code more difficult to read, reduce the code readability, too much use of adapters can make the system code messy.

10.2 Structure and implementation of the adapter pattern

10.2.1 Structure of the adapter pattern

  1. Target interface: The interface expected by the current system business, which can be an abstract class or interface.
  2. The Adaptee class: This is the component interface in an existing component library that is being accessed and adapted.
  3. Adapter class: An Adapter that converts an Adapter’s interface to a target interface by inheriting or referring to the Adapter’s object, allowing the client to access the Adapter in the format of the target interface.

10.2.2 Code implementation

10.2.2.1 Class Adapter Pattern

Voltage5V target interface

package com.zhuang.adapter.classadapter; /** * @ClassName Voltage5V * @Description * @Date 2021/3/21 14:14 * @Created by Dell */ public interface Voltage5V {// Define a standard charger to implement public int output5V(); }

Voltage220V

package com.zhuang.adapter.classadapter; /** * @className Voltage220V * @description create AC * @date 2021/3/21 14:13 * @created by Dell */ public class Voltage220V {public int output220V() {system.out.println ("voltage 220V "); return 220; }}

VoltageAdapter

package com.zhuang.adapter.classadapter; /** * @ClassName VoltageAdapter * @Description creates the adapter * @Date 2021/3/21 14:14 * @Created by Dell */ public class VoltageAdapter extends Voltage220V implements Voltage5V {@Override public int output5V() {int output220V =  output220V(); Int output5V = output220V / 44; System.out.println("VoltageAdapter output 5 volts "); return output5V; }}

Phone

package com.zhuang.adapter.classadapter; /** * @ClassName Phone * @Description Phone * @Date 2021/3/21 14:15 * @Created by Dell */ public class Phone {public void () Charging (Voltage5V Voltage5V) {if (Voltage5V. output5V() == 5) {System.out.println(" I can charge you "); } else if (Voltage5v.output5v () > 5) {System.out.println(Voltage5v.output5v () > 5);} else if (Voltage5v.output5v () > 5) { }}}

Client

package com.zhuang.adapter.classadapter; /** * @ClassName Client * @Description Client * @Date 2021/3/21 14:15 * @Created by Dell */ public class Client {public Static void main(String[] args) {System.out.println("== Class Adapter =="); Phone phone = new Phone(); phone.charging(new VoltageAdapter()); }}

Class adapter pattern considerations and details

  • Java is a single inheritance mechanism, so class adapters need to inherit an Adaptee (Voltage220v) class, which is a drawback. In addition to this, there are limitations in requiring the Target (Voltage5v) to be an interface.
  • The methods of the adapter Voltage220v class are all exposed in the adapter VoltageAdapter class, increasing the cost of use. But because it inherits from the adapter Voltage220v class, it can override the class’s methods as needed, making the adapter VoltageAdapter class more flexible.

10.2.2.2 Object Adapter Pattern

Voltage5V

package com.zhuang.adapter.objectadapter; /** * @ClassName Voltage5V * @Description charging 5V * @Date 2021/3/21 14:32 * @Created by Dell */ public interface Voltage5V {// Define a standard charger to implement public int output5V(); }

Voltage220V

package com.zhuang.adapter.objectadapter; /** * @ClassName Voltage220v * @Description 220V * @Date 2021/3/21 14:32 * @Created by Dell */ public class Public int output220V() {system.out.println ("voltage 220V "); voltage220V {public int output220V() {system.out.println ("voltage 220V "); return 220; }}

VoltageAdapter

package com.zhuang.adapter.objectadapter; /** * @ClassName VoltageAdapter * @Description Adapter class * @Date 2021/3/21 14:33 * @Created by Dell */ public class VoltageAdapter implements Voltage5V { private Voltage220V voltage220V; public VoltageAdapter(Voltage220V voltage220V) { this.voltage220V = voltage220V; } @Override public int output5V() {@Override public int output220V = voltage220V.output220V(); Int output5V = output220V / 44; System.out.println("VoltageAdapter output 5 volts "); return output5V; }}

Phone

package com.zhuang.adapter.objectadapter; /** * @ClassName Phone * @Description Phone * @Date 2021/3/21 14:33 * @Created by Dell */ public class Phone {public void () Charging (Voltage5V Voltage5V) {if (Voltage5V. output5V() == 5) {System.out.println(" I can charge you "); } else if (Voltage5v.output5v () > 5) {System.out.println(Voltage5v.output5v () > 5);} else if (Voltage5v.output5v () > 5) { }}}

Client

package com.zhuang.adapter.objectadapter; /** * @ClassName Client * @Description object adapter * @Date 2021/3/21 14:33 * @Created by Dell */ public class Client { Public static void main(String[] args) {System.out.println("== Object Adapter =="); Phone phone = new Phone(); phone.charging(new VoltageAdapter(new Voltage220V())); }}

Object adapter pattern considerations and details

  • Object adapters and class adapters are really the same idea, but implemented differently.
  • Based on the principle of composite reuse, it uses composite substitution inheritance, so it solves the limitation that VoltageAdapter must inherit Voltage220v in class adapters, and it no longer forces Voltage5v to be an interface. Lower cost of use, more flexible. Therefore, the object adapter pattern is a common one for the adapter pattern.

10.2.2.3 Interface adapter pattern

Animation

package com.zhuang.adapter.interfaceadapter; /** * @classname Animation * @description * @date 2021/3/21 14:47 * @created by dell */ public interface Animation { public void method1(); public void method2(); public void method3(); public void method4(); public void method5(); }

AnimationAdapter

package com.zhuang.adapter.interfaceadapter; /** * @ClassName AnimationAdapter * @Description * @Date 2021/3/21 14:48 * @Created by Dell */ public class AnimationAdapter implements Animation {// Override public void method1() {} @Override public void method2() {}  @Override public void method3() { } @Override public void method4() { } @Override public void method5() { } }

JFrameAnimation

package com.zhuang.adapter.interfaceadapter; /** * @ClassName JframeAnimation * @Description Adapter subclass * @Date 2021/3/21 14:49 * @Created by Dell */ public class JFrameAnimation extends AnimationAdapter {@Override public void method1() {System.out.println("method1() is called...") {System.out.println(); ); } @Override public void method2() {System.out.println("method2() is called...");} @Override public void method2() {System.out.println(); ); }}

Client

package com.zhuang.adapter.interfaceadapter;

/**
 * @Classname Client
 * @Description  客户端类
 * @Date 2021/3/21 14:50
 * @Created by dell
 */

public class Client {
    public static void main(String[] args) {
        JFrameAnimation animation = new JFrameAnimation();
        animation.method1();
        animation.method2();
    }
}

10.3 SpringMVC source code parsing

Controller

package com.zhuang.adapter.springmvc; /** * @ClassName Controller * @Description SpringMVC Controller source * @Date 2021/3/21 14:53 * @Created by Dell */ Public interface Controller {} class HttpController implements Controller {public void implements Controller doHttpHandler() { System.out.println("http..." ); } } class SimpleController implements Controller { public void doSimplerHandler() { System.out.println("simple..." ); } } class AnnotationController implements Controller { public void doAnnotationHandler() { System.out.println("annotation..." ); }}

DispatchServlet

package com.zhuang.adapter.springmvc; import java.util.ArrayList; import java.util.List; /** * @ClassName DispatchServlet * @Description SpringMVC DispatchServlet source * @Date 2021/3/21 14:52 * @Created by Dell */ public class DispatchServlet { public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>(); public DispatchServlet() { handlerAdapters.add(new AnnotationHandlerAdapter()); handlerAdapters.add(new HttpHandlerAdapter()); handlerAdapters.add(new SimpleHandlerAdapter()); } public void doDispatch() {public void doDispatch() {public void doDispatch(); // Adapter can get the desired Controller httpController Controller = new httpController (); // AnnotationController controller = new AnnotationController(); //SimpleController controller = new SimpleController(); // Get the corresponding adapter handlerAdapter = getHandler(Controller); Adapter.handle (controller); // Adapter.handle (controller); } public HandlerAdapter getHandler(Controller Controller) { Returns the corresponding adapter for (HandlerAdapter Adapter: this.handlerAdapters) { if (adapter.supports(controller)) { return adapter; } } return null; } public static void main(String[] args) { new DispatchServlet().doDispatch(); // http... }}

HandlerAdapter

package com.zhuang.adapter.springmvc; /** * @ClassName handlerAdapter * @Description SpringMVC handlerAdapter source * @Date 2021/3/21 14:53 * @Created by Dell */ Public interface handlerAdapter {public Boolean supports(Object handler); public void handle(Object handler); } class SimpleHandlerAdapter implements HandlerAdapter {@Override public void handle(Object handler) { ((SimpleController) handler).doSimplerHandler(); } @Override public boolean supports(Object handler) { return (handler instanceof SimpleController); } } class HttpHandlerAdapter implements HandlerAdapter { @Override public void handle(Object handler) { ((HttpController) handler).doHttpHandler(); } @Override public boolean supports(Object handler) { return (handler instanceof HttpController); } } class AnnotationHandlerAdapter implements HandlerAdapter { @Override public void handle(Object handler) { ((AnnotationController) handler).doAnnotationHandler(); } @Override public boolean supports(Object handler) { return (handler instanceof AnnotationController); }}

Write in the last

  • If my article is useful to you, please click 👍, thank you!
  • Let me know in the comments section if you have any questions! 💪