Adapter Pattern

Characteristics of Adapter

Examples of Adapter

Java Implementaion

AndroidToIphoneAdapter.class

/*
Charge Android phone with IPhone charger.
We are passing Android phone into it but it can be charged using an iphone charger.
 */
public class AndroidToIphoneAdapter implements IPhone{

  private AndroidPhone androidPhone;

  public AndroidToIphoneAdapter(AndroidPhone androidPhone) {
    this.androidPhone = androidPhone;
  }

  public void charge() {
    androidPhone.charge();
  }
}

AdapterExample.class

public class AdapterExample {

  public static void main(String[] args) {

    AndroidCharger androidCharger = new AndroidCharger();
    AndroidPhone androidPhone = new Pixel5Phone();
    androidCharger.charge(androidPhone);

    IPhoneCharger iPhoneCharger = new IPhoneCharger();
    IPhone iPhone = new IPhone10();
    iPhoneCharger.charge(iPhone);

    /*
    We will pass Android phone to AndroidToIphoneAdapter and use Iphone Charger to charge Android
    Phone
     */
    AndroidToIphoneAdapter adapter = new AndroidToIphoneAdapter(androidPhone);
    iPhoneCharger.charge(adapter);
  }
}

Disadvantages of Adapter

To access the full working code sample, click here

References

Adapter Design Pattern | Implementation and Disadvantages | Clean Code Series

SUBSCRIBE TO NEW CONTENT

Tutorials on System Design, BlockChain, Design Patterns and more sent out once a week.

Loading comments...