Design pattern - adapter pattern

Posted by seikan on Wed, 20 Oct 2021 07:08:16 +0200

Adapter mode

Definition: convert an interface to another interface that the customer wants. The adapter pattern allows some classes with incompatible interfaces to work together. It mainly solves that in the software system, some "existing objects" are often put into the new environment, and the interface required by the new environment can not be met by the existing objects.

Role:
The adapter mode has the following roles:

  • Target abstract class: defines the domain specific interface that customers need to use
  • Adapter class: adapt the target abstract class and adapter class
  • Adapter class: defines the existing interfaces and contains the actual business logic methods.
  • Client class: program for the target abstract interface

Note: give a scenario: now the method name defined in the Target class is method1(), and the Client has programmed for this method. Now we need to use the method2() method of class B to meet the needs of the Client, that is, to replace the method1() method. However, the interface method1() required by the Client is different from the interface method2() in class B, which requires the adapter mode.

Approach: write an adapter class to serve as a bridge between Client and class B.

Classification:

  • Class adapter: there is inheritance and implementation relationship between adapter and adapter. At this time, Target is the interface.
/**
 * adapter class 
 * Convert the input 220V voltage into 5V that can be used by the mobile phone
 */
public class Adapter220 extends Voltage_220 implements Target{

    @Override
    public void need5V() {
        System.out.println("I am the adapter, and the input voltage is:"+super.getV()+"V The output voltage is:"+super.getV()/44+"V");

    }
}

Object adapter: there is a combination and aggregation relationship between adapter and adapter. At this point, the Target is an abstract class or interface.

/**
 * adapter class 
 * Convert the input 220V voltage into 5V that can be used by the mobile phone
 */
public class Adapter220 extends Target{
    private Voltage_220 voltage_220=new Voltage_220();
    @Override
    public void need5V() {
        System.out.println("I am the adapter, and the input voltage is:"+voltage_220.getV()+"V The output voltage is:"+voltage_220.getV()/44+"V");

    }
}

advantage:

Public benefits:

  1. Decouple the target class from the adapter class, and reuse the existing adapter class by introducing an adapter class without modifying the original code.
  2. Flexible and expandable, in line with the "opening and closing principle". For different logical services, you only need to write a new adapter and replace it without modifying the original code.
  3. Good transparency and reusability. The specific implementation is encapsulated in the adapter class, which is transparent to the client. The adapter class improves the reuse of the adapter class.

Separate advantages of class adapter mode:

The adapter class is a subclass of the adapter class. You can replace some methods of the adapter class in the adapter class to make the adapter flexible.

Individual advantages of object adapter mode:

Multiple different adapter classes can be adapted to the same target, and the same adapter can adapt the adapter class and its subclasses to the target interface.

Disadvantages:

Disadvantages of class adapter mode: it does not support multiple inheritance and has certain limitations, so it can not adapt multiple classes in the target interface.

The single disadvantage of the object adapter pattern is that the method replacement of the adapter class is extremely complex to implement.

Applicable scenarios:

  • The system needs to use existing classes, but the interfaces of these classes do not meet the needs of the system.
  • Want to improve the reusability of this class.

Example description:
Structure: using object adapter pattern
Scenario: the household electricity is 220V, but the mobile phones are charged under 5V. At this time, the charger needs to be adapted.

Target

/**
 * Target Target abstract interface
 *
 * Provide mobile phone charging under standard 5V voltage
 */
public class Target {
    public void need5V(){};
}

Voltage_220

/**
 * Voltage_220 Adapter class
 * Provide 220V voltage
 */
public class Voltage_220 {
    private final int v=220;

    public int getV() {
        return v;
    }

}

Adapter220

/**
 * adapter class 
 * Convert the input 220V voltage into 5V that can be used by the mobile phone
 * object adapter 
 */
public class Adapter220 extends Target{
    private Voltage_220 voltage_220=new Voltage_220();
    @Override
    public void need5V() {
        System.out.println("I am the adapter, and the input voltage is:"+voltage_220.getV()+"V The output voltage is:"+voltage_220.getV()/44+"V");

    }
}
/**
 * Phone Customer class
 * Programming according to abstract classes
 * Charge only at 5V
 */
public class Phone {
    public static void main(String[] args) {
        Target target=new Adapter220();
        target.need5V();
        /**The results are as follows:
        I am the adapter. The input voltage is 220V and the output voltage is 5V
        */
    }
}

Reference books:
Design pattern. Liu Wei, Hu Zhigang, Guo Kehua. Tsinghua University Press

Topics: Java Design Pattern mvc