An overview,

  • Adapter Pattern Transforms the interface of a class into another interface representation expected by the client. The main purpose is compatibility, so that two classes whose interfaces do not match and do not work can work together, nicknamed Wrapper classes.

Second, the class adapter pattern

  • Adapter class, by inheriting SRC class, achieve DST interface, complete SRC -> DST adaptation
  • Power adapter Example: Power adapterVoltageAdapterinheritanceVoltage220VTo realizeVoltage5V.

public class Voltage220V {
  private final static int voltage = 220;

  public static int output220V(a) {
    returnvoltage; }}Copy the code
public interface Voltage5V {
  int output5V(a);
}
Copy the code
public class VoltageAdapter extends Voltage220V implements Voltage5V {
  @Override
  public int output5V(a) {
    int vol = output220V();
    int output = vol / 44;
    if(output ! =5) throw new RuntimeException("Input voltage does not match");
    returnoutput; }}Copy the code
public interface Phone {
  void charge(Voltage5V voltage5V);
}
Copy the code
  • Java is a single-inheritance mechanism, so the fact that a class adapter needs to inherit SRC classes is a disadvantage because it requires DST to be an interface, which has some limitations
  • SRC class methods are exposed in Adapter, which increases the cost of using them
  • Because it inherits the SRC class, it can override the methods of the SRC class as needed, making the Adapter more flexible

Object adapter pattern

  • Instead of inheritance, associations (aggregations) are used, and SRC classes are aggregated into Adapter classes instead of inheritance

public class VoltageAdapter implements Voltage5V {
  private Voltage220V voltage220V;

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

  @Override
  public int output5V(a) {
    int vol = voltage220V.output220V();
    int output = vol / 44;
    if(output ! =5) throw new RuntimeException("Input voltage does not match");
    returnoutput; }}Copy the code
  • Lower cost, more flexibility

4. Interface adapter mode

  • Also called adapter pattern or default adapter pattern
  • When it is not necessary to implement all the methods provided by the interface, first design an abstract class implementation 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 achieve the requirements.
  • For situations where you don’t want to implement all the methods of the interface. The interface is narrowed
public interface Interface0 {
  void m1(a);
  void m2(a);
  void m3(a);
  void m4(a);
}
Copy the code
public abstract class AbsClass implements Interface0{
  @Override
  public void m1(a) {
    throw new UnsupportedOperationException("Haven't overrider this method!");
  }
  @Override
  public void m2(a) {
    throw new UnsupportedOperationException("Haven't overrider this method!");
  }
  @Override
  public void m3(a) {
    throw new UnsupportedOperationException("Haven't overrider this method!");
  }
  @Override
  public void m4(a) {
    throw new UnsupportedOperationException("Haven't overrider this method!"); }}Copy the code
public class Interface0Impl extends AbsClass {
  @Override
  public void m1(a) {
    System.out.println("Call method m1");
  }

  public static void main(String[] args) {
    Interface0 interface0 = new Interface0Impl();
    interface0.m1();  // Call m1
    interface0.m2();  // UnsupportedOperationException}}Copy the code