8 - Design Mode - adapter mode

Posted by marijn on Tue, 01 Mar 2022 00:09:48 +0100

1, What is an adapter

1) Adapter pattern transforms the interface of a class into another interface 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

2, Code

Class adapter mode

Basic introduction: the Adapter class implements the dst interface by inheriting the src class and completes the adaptation of src - > dst.

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 Adapter), and our DST (i.e. the target) is 5V DC

UML diagram

Voltage220V assigned

public class Voltage220V {

    public int output22V(){
        int src = 220;
        System.out.println("Voltage = " + src);
        return src;
    }
}

IVoltage5V configuration interface

public interface IVoltage5V {
    /**
     * Output 5 volts
     * @return
     */
    public int output5V();
}

VoltageAdapter configurator class

public class VoltageAdapter extends Voltage220V implements IVoltage5V {

    @Override
    public int output5V() {
        int output = super.output22V();
        return output / 44;
    }
}

Phone

public class Phone {

    public void charging(IVoltage5V voltage5V){
        if (voltage5V.output5V() == 5) {
            System.out.println(" The output voltage is 5V, which can be charged ");
        }else if (voltage5V.output5V() > 5){
            System.out.println(" If the voltage is greater than 5 volts, it cannot be charged ");
        }
    }
}

test

public class AdapterClient {
    public static void main(String[] args) {
        new Phone().charging(new VoltageAdapter());  
    }
}

Class adapter pattern considerations and details

1) 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.

2) The methods of src class will be exposed in the Adapter, which also increases the cost of use.

3) Because it inherits the src class, it can rewrite the methods of the src class according to the requirements, and the flexibility of using the Adapter is enhanced.

Object Adapter Pattern

Basic introduction

1) The basic idea is the same as the Adapter mode of the class, except that the Adapter class is modified instead of inheriting the src class, but * * holding the instance of the src class * * to solve the compatibility problem. That is: hold src class, implement dst class interface, and complete the adaptation of src - > dst.

2) According to the "composite Reuse Principle", we try to use association relationship instead of inheritance relationship in the system, so most structural patterns are object structural patterns.

3) Object adapter pattern is a common pattern of adapter pattern

UML diagram
Here, you only need to modify the code of VoltageAdapter, and others do not need to be changed
VoltageAdapter

public class VoltageAdapter implements IVoltage5V {

    private Voltage220V voltage220V;

    public VoltageAdapter(Voltage220V voltage220V) {
        this.voltage220V = voltage220V;
    }

    @Override
    public int output5V() {
        int output = voltage220V.output22V();
        return output / 44;
    }
}

test

public class AdapterClient {
    public static void main(String[] args) {
        new Phone().charging(new VoltageAdapter2(new Voltage220V()));
    }
}

Object adapter pattern considerations and details

1) Object adapter and class adapter are actually the same idea, but they are implemented in different ways.

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.

2) Lower cost and more flexible.

Interface adapter mode

Basic introduction
1) Some books are called: Default Adapter Pattern or Default Adapter Pattern.

2) Core idea: when you do not need to implement all the methods provided by the interface, you can first design an abstract class to implement the interface and provide a default implementation (empty method) for each method in the interface. Then the subclass of the abstract class can selectively override some methods of the parent class to meet the requirements

3) This applies when an interface does not want to use all its methods.

UML diagram

Interface4

public interface Interface4 {

    void m1();
    void m2();
    void m3();
}

AbsAdapter null implementation method

public class AbsAdapter implements Interface4{

    @Override
    public void m1() {

    }

    @Override
    public void m2() {

    }

    @Override
    public void m3() {

    }
}

test

public class AdapterClient {
    public static void main(String[] args) {
		AbsAdapter adapter = new AbsAdapter(){
            @Override
            public void m1() {
                System.out.println(" rewrite m1 method");
            }
        };
        adapter.m1();
    }
}

3, Summary

1) The three naming methods are named according to the form src gives to the Adapter (the form in the Adapter).

2) 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

3) The biggest function of Adapter mode is to integrate incompatible interfaces.

Topics: Design Pattern