Design mode - adapter mode

Posted by warriors2003 on Tue, 22 Feb 2022 08:29:55 +0100

1, Adapter mode

1. 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. Also called a Wrapper
  2. The adapter mode is a structural mode.
  3. Including: class adapter mode, object adapter mode and interface adapter mode

2. Working principle

  1. Adapter mode: convert the interface of one class into another, so that the classes with incompatible interfaces can be 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 results, users feel that they are only interacting with the target interface.

2, Class adapter

1. Voltage problem

  1. Adapted class
// Adapted class
public class Voltage220V {
    public int output220V(){
        int src = 220;
        System.out.println("Voltage = " + src + "Crouch");
        return src;
    }
}

  1. Adapter interface
// Adapter interface
public interface Voltage5V {
    public int output5V();
}
  1. adapter class
// adapter class 
public class VoltageAdapter extends Voltage220V implements Voltage5V{
    @Override
    public int output5V() {
        // Get the original voltage
        int srcV = output220V();
        // To target voltage - 5V
        int dstV = srcV / 44;
        return dstV;
    }
}
  1. Mobile phones
// Mobile phones
public class Phone {
    // charge
    public void charging(Voltage5V voltage5V){
        if (voltage5V.output5V() == 5){
            System.out.println("The voltage is 5 V Can charge");
        }else {
            System.out.println("Charging failure");
        }
    }
}
  1. Test class
// Test class
public class Client {
    public static void main(String[] args) {
        System.out.println("Class adapter mode");
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter());
    }
}

2. Precautions

  1. Java is a single inheritance mechanism, so the class adapter needs to inherit the src class. All requirements require 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 use cost.
  3. 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.

3, Object adapter

1. Voltage problem

  1. Adapted class
// Adapted class
public class Voltage220V {
    public int output220V(){
        int src = 220;
        System.out.println(" Voltage = " + src + "Crouch");
        return src;
    }
}
  1. Adapter interface
// Adapter interface
public interface Voltage5V {
    public int output5V();
}
  1. adapter class
// adapter class 
public class VoltageAdapter implements Voltage5V {

    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 (voltage220V != null){
            int src = voltage220V.output220V(); // Obtain 220V voltage
            System.out.println(" Use the object adapter for adaptation");
            dst = src / 44;
            System.out.println(" Adaptation completed, output voltage = " + dst);
        }
        return dst;
    }
}

  1. Mobile phones
// Mobile phones
public class Phone {
    // charge
    public void charging(Voltage5V voltage5V){
        if (voltage5V.output5V() == 5){
            System.out.println("The voltage is 5 V Can charge");
        }else {
            System.out.println("Charging failure");
        }
    }
}
  1. Test class
// Test class
public class Client {
    public static void main(String[] args) {
        System.out.println(" Object Adapter Pattern ");
        Phone phone = new Phone();
        phone.charging(new VoltageAdapter(new Voltage220V()));
    }
}

2. Precautions

  1. The implementation methods of object adapter and class adapter are different. According to the principle of composite reuse, composition is used instead of inheritance, so it solves the limitation that class adapter must inherit src, and it no longer requires dst to be an interface.
  2. Lower cost and more flexible.

4, Interface adapter

1. Application examples

  1. Interface
// Interface
public interface Interface3 {

    public void m1();
    public void m2();
    public void m3();
    public void m4();
}
  1. abstract class
// abstract class
public abstract class AbsAdapter implements Interface3{
    @Override
    public void m1() {

    }

    @Override
    public void m2() {

    }

    @Override
    public void m3() {

    }

    @Override
    public void m4() {

    }
}

  1. Test class
// Test class
public class Client {
    public static void main(String[] args) {
        AbsAdapter absAdapter = new AbsAdapter() {
            @Override
            public void m1() {
                System.out.println("rewrite m1 method");
            }
        };

        absAdapter.m1();
    }
}

2. Precautions

  1. 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 for each method in the interface. Then the subclass of the abstract class can selectively override some methods of the parent class to implement the requirements.
  2. This applies when an interface does not want to use all its methods.

Topics: Java