This is the 9th day of my participation in Gwen Challenge

This article is participating in “Java Theme Month – Java Development in Action”, see the activity link for details

To the ninth day, it is time to learn the design mode of SpringBoot design the last adapter mode, today learn to do a summary, and then open the next part of the learning, come on!!

Adapter Pattern Adapter Pattern

Intent: To convert the interface of a class to another interface that the customer wants. The adapter pattern makes it possible for classes to work together that would otherwise not work because of interface incompatibilities.

The adapter has two mode structures

Target (Target Abstract Class) : The Target abstract class defines the interface required by the customer. It can be an abstract class, an interface, or a concrete class. Adapter (Adapter class) : An Adapter can call another interface, acting as a converter, to adapt Adaptee and Target. The Adapter class is the core of the Adapter pattern, and in the object Adapter, it connects the two by inheriting Target and associating them with an Adaptee object.

Adaptee: An Adaptee is a role to be adapted. It defines an existing interface that needs to be adapted. An Adaptee class is usually a concrete class that contains the business methods that the customer wants to use.

classDiagram
Target <|-- Adapter
Adaptee <|-- Adapter
Target : +request
class Adapter{
+request()
}
class Adaptee{
+request()
}

The class adapter

There is a class (source class) Adaptee that is to be adapted, and we define a Target interface (Target), and then call Adaptee’s SpecificRequest() method at Request() in the Target interface

implementation

public class AdapterPatternDemo {
    public static void main(String[] args) {
        Target mAdapter = newAdapter(); mAdapter.request(); }}// Create Target interface
interface Target {

    // This is a method that the source class Adaptee does not have
    public void request(a);
}
// Create the source class
class Adaptee {

    public void SpecificRequest(a){
        System.out.println("The method of the adapted."); }}// Create the adapter class
class Adapter extends Adaptee implements Target {

    // The target interface requires the request() method name to be called. The source class Adaptee has no request() method name
    // The adaptor wraps the SpecificRequest() method into a request() that Target can call
    @Override
    public void request(a) {
        this.SpecificRequest();
    }

Copy the code

The results of

Object adapter

Object adapters differ from class adapters in that class adapters perform adaptation through inheritance, whereas object adapters perform adaptation through association

The results of

public class AdapterPatternDemo {
    public static void main(String[] args) {
        Target mAdapter = newAdapter(); mAdapter.request(); }}// Create Target interface
interface Target {

    // This is a method that the source class Adaptee does not have
    public void request(a);
}
// Create the source class
class Adaptee {

    public void SpecificRequest(a){
        System.out.println("The method of the adapted."); }}// Core implementation of the object adapter
class Adapter implements Target{
    // The adapter is a property of the object adapter
    private Adaptee adaptee = new Adaptee();

    @Override
    public void request(a) {
        / /...
        adaptee.SpecificRequest();
        / /...}}Copy the code

Differences between the adapter pattern and the decorator pattern

The main function of the adapter pattern is to transform the interface of a class into an interface that the client can accept (recognize), while the main function of the decorator pattern is to extend the function of the decorator class

Adaptation mode in Java I/O

JAVA IO class library, the string data into byte data saved in the file, byte data into stream data.

Application in Spring

The adapter pattern is useful in both AOP and MVC.

Use of adapters in AOP

AOP refers to the programming method of dynamically cutting a piece of code into a specified method while the program is running. In AOP, Advice is used to enhance the proxyed class, and Advie has many types (such as BeforeAdvice, AfterReturningAdvice, ThreowSadvice), Each Advie has its own Interceptor. Different types of interceptors provide a unified interface through adapters. Example: Client –> target –> Adapter –> interceptor –> advice. Ultimately, different advice is called to implement the propped class enhancement

Use of adapters in MVC

The doDispatch method in the DispatcherServlet dispatches requests to specific controllers. However, the controller type may vary, and the general handling method is if… else… When a new controller is added, an additional if… else… To judge

How to solve such a problem?

Spring defines an adapter interface so that each Controller has a corresponding adapter implementation class that performs the corresponding methods instead of the Controller. When you extend Controller, you only need to add an adapter class to complete the extension of SpringMVC.

Today’s summary

The 9 design modes designed in Spring Boot are basically a little simpler so far. In the application scenarios of Spring, some modes involve less, and some are more, but they are not comprehensive. Just as the so-called “sharpening the knife without miscutting wood”, I think it is beneficial to understand the concept of design mode first, which is conducive to learning more Spring Cloud in the following. The next article should be the beginning of Spring Cloud-related applications.