Adapter pattern of design patterns

1 Preliminary understanding

Definition of Adapter Pattern

Convert the interface of a class to another interface that the client expects. The adapter pattern enables classes to work together that would otherwise not work together due to interface mismatches.

vernacular

The adapter mode is like a travel socket adapter (picture 1), a Type-c to VGA adapter (picture 4).

 

Figure 1. Image source network

Figure 2. Image source network

Students who have been to Hong Kong, Dubai, etc. know that the sockets used there are different from ours. Their sockets need the plugs shown in the first side of Figure 2, and our commonly used sockets are similar to the second side. Therefore, our laptops and mobile phones cannot be directly charged locally. So we need a socket converter. The first side of the converter is inserted into the local socket, and the second side is for us to charge, so that our plug can be used locally. The same is true for Type-c to VGA to interface.

2 Adapter mode structure diagram

Figure 3. Adaptation Mode Structure Diagram Source Network

As shown in the figure, Client cannot directly access Adaptee. Adapter is an adapter that converts Adaptee into an interface that Client can access. So through the adapter Adapter, the client can access Adaptee.

3 Examples of usage scenarios

The mobile phone wants to project on the projector. Since the mobile phone is a Type-c interface, the projector is a VGA interface. It cannot be directly projected, and an adapter is required to transfer the video signal from the Type-c port to the VGA port, and finally it can be output to the large screen.

4 The specific implementation of the adapter pattern in the usage scenario

Figure 4. typec to vga/hdm converter

 

  As shown in the figure, there is an adapter, the first port is the typec port, and the second port is the vga port. Only by inputting the video signal from the typec port and converting the output to the vga port can it be connected to the projector and realize the projection of the mobile phone screen to the task on the projector. The items involved are: mobile phones, adapters, projectors.

4.1 Define a mobile phone, it has a typec port, which is the video source.

package com.jstao.adapter;

/**
 * @author jstao
 * Define a mobile Phone, which has a Typec interface.
 * 
 */
public class Phone {
    public void typecPhone() {
        System.out.println("information from Typec The mobile phone output of the mouth.");
    }
}

4.2 Define a vga interface

package com.jstao.adapter;

/**
 * @author jstao
 * Define a VGA interface.
 * 
 */
public interface Vga {
    void vgaInterface();
}

4.3 To implement an adapter, there are three types of adapter implementations: class adapter mode, object adapter mode, and interface adapter mode.

4.3.1 Adapter pattern for classes

Principle: Implement adapter functions by inheriting features.

package com.jstao.adapter;

/**
 * 
 * Implement a Type-c to VGA adapter,
 * There are three ways to implement the adapter, this is the first way.
 * @author jstao
 *
 */
public class Typec2Vga1 extends Phone implements Vga{
    @Override
    public void vgaInterface() {
        // TODO Auto-generated method stub
        typecPhone();
        System.out.println("received Type-c mouth information, the information is converted into VGA in the interface...");
        System.out.println("information has been converted into VGA interface, the display can be docked.");
    }
}

4.3.2 Adapter pattern for objects

Principle: The adapter function is realized through combination.

package com.jstao.adapter;

/**
 * 
 * Implement a Type-c to VGA adapter,
 * There are three ways to implement the adapter, this is the second way.
 * @author jstao
 *
 */
public class Typec2Vga2 implements Vga{

    private Phone phone;
    
    public Typec2Vga2(Phone phone) {
        // TODO Auto-generated constructor stub
        this.phone = phone;
    }
    
    @Override
    public void vgaInterface() {
        // TODO Auto-generated method stub
        if(phone != null) {
            phone.typecPhone();
            System.out.println("received Type-c mouth information, the information is converted into VGA in the interface...");
            System.out.println("information has been converted into VGA interface, the display can be docked.");
        }
    }
}

4.3.3 Adapter mode of interface

Principle: Use abstract classes to implement adapter functions.

Define three interfaces

package com.jstao.adapter;

/**
 * define interface
 * @author jstao
 *
 */
public interface Target {
    
    void typec();
    void typec2vga();
    void typec2hdmi();
}

define an abstract class

package com.jstao.adapter;

/**
 * define an abstract class
 * @author jstao
 *
 */
public abstract class Adapter implements Target{

    public void typec() { }
    
    public void typec2vga() { }
    
    public void typec2hdmi() { }
}

Implement a VGA adapter

package com.jstao.adapter;

/**
 * 
 * A VGA adapter can be implemented, and an HDMI adapter can also be implemented in the same way.
 * There are three ways to implement an adapter, and this is the third way to implement it.
 * @author jstao
 *
 */
public class VgaAdapter extends Adapter{
    
    public void typec() {
        System.out.println("information from Typec The mobile phone output of the mouth.");
    }
    
    public void typec2vga() {
        System.out.println("received Type-c mouth information, the information is converted into VGA in the interface...");
        System.out.println("information has been converted into VGA interface, the display can be docked.");
    }
}

4.4 Define a display to test the three adapters implemented above

package com.jstao.adapter;

/**
 * define a display
 * Docking with adapter
 * @author jstao
 *
 */
public class Screen {
    public static void main(String[] args) {
        //The first adapter usage
        System.out.println("-------------first adapter------------");
        Vga vga = new Typec2Vga1();
        vga.vgaInterface();//Adapter converts typec to vga
        System.out.println("Display docking adapter, the phone is successfully projected to the display!");
        
        //Second adapter usage
        System.out.println("-------------second adapter------------");
        Typec2Vga2 typec2Vga1 = new Typec2Vga2(new Phone());
        typec2Vga1.vgaInterface();//Adapter converts typec to vga
        System.out.println("Display docking adapter, the phone is successfully projected to the display!");
        
        //Third adapter usage
        System.out.println("-------------third adapter------------");
        VgaAdapter vgaAdapter = new VgaAdapter();
        vgaAdapter.typec();
        vgaAdapter.typec2vga();//Adapter converts typec to vga
        System.out.println("Display docking adapter, the phone is successfully projected to the display!");
    }
}

4.5 Test Results

Figure 5. Result output graph

It can be seen that the results obtained are the same, but the implementation of the three adapters is different.

5 Summary

5.1 Application of adapter mode in source code:

(1) The IO module of the JDK source code is used, such as java.io.InputStreamReader(InputStream), java.io.OutputStreamWriter(OutputStream).

(2) The mybatis source code log module uses the object adapter mode.

5.1 Adapter mode converts one interface to another. It has three implementations:

(1) When you want to convert a class into a class that satisfies another new interface, you can use the class adapter pattern to create a new class, inherit the original class, and implement the new interface, such as 4.3.1.

  (2) When you want to convert an object into an object that satisfies another new interface, you can create a Typec2Vga2 class and hold an instance of the original class. In the method of the Typec2Vga2 class, just call the method of the instance, such as 4.3.2 Adapter pattern for objects.

(3) When you do not want to implement all the methods in an interface, you can create an abstract class Adapter to implement all the methods. When we write other classes, we can inherit the abstract class, such as the adapter mode of 4.3.3 interface.

Tags: Java

Posted by neonorange79 on Wed, 04 May 2022 20:38:16 +0300