How to implement Adapter Design Pattern in Java with a Real World scenario ?.

Program to demonstrate how to implement Adapter Design Pattern in Java with a Real World scenario.





Video tutorial to demonstrate how to implement Adapter Design Pattern in Java.







Click here to download source code 








Real World Scenario :- Mobiles

Let suppose we have three mobiles as :

1. Nokia Asha
2. Nokia Lumia
3. Samsung S3

We want to charge all the three mobiles but we only have Nokia charger. In order to charge Samsung mobile we have a adapter plug which will connect to Nokia charger and allow charging of Samsung mobile.

Solution :




Design :- Adapter Design Pattern
Lets implement above diagram with following classes and interfaces:-

Interface 1 :- Chargeable

package com.hubberspot.designpatterns.adapter.example;

// Common interface for charging 
// the mobiles
public interface Chargeable {

 public void charge();

}




Class 1 :- NokiaAsha

package com.hubberspot.designpatterns.adapter.example;


public class NokiaAsha implements Chargeable {

 @Override
 public void charge() {

  System.out.println("Charging Nokia Asha .....");

 }

}




Class 2 :- NokiaLumia

package com.hubberspot.designpatterns.adapter.example;

public class NokiaLumia implements Chargeable {

 @Override
 public void charge() {

  System.out.println("Charging Nokia Lumia ..... ");

 }

}




Class 3 :- SamsungS3

package com.hubberspot.designpatterns.adapter.example;


public class SamsungS3 {

 public void samsungCharge() {
  System.out.println("Charging Samsung S3 ....... ");
 }

}




Class 4 :- SamsungAdapterPlug

package com.hubberspot.designpatterns.adapter.example;

// This is our main implementation class for 
// Adapter Design Pattern
public class SamsungAdapterPlug implements Chargeable {

 SamsungS3 samsungS3;

 public SamsungAdapterPlug(SamsungS3 samsungS3) {
  this.samsungS3 = samsungS3;
 }

 // Calling the same charge method
 // but internally its making a call 
 // to samsung charge method. Thus 
 // adapting it to use Nokia charger.
 @Override
 public void charge() {

  samsungS3.samsungCharge();

 }

 public String toString() {
  return "Samsung Mobile pretending to be Nokia Mobile ......";
 }

}




Class 5 :- main class - RunCharger

package com.hubberspot.designpatterns.adapter.example;


public class RunCharger {

 public static void main(String[] args) {

  System.out.println("Let's Charge our mobiles one-by-one .... ");

  // Create a object of each mobile
  NokiaAsha nokiaAsha = new NokiaAsha();
  NokiaLumia nokiaLumia = new NokiaLumia();
  SamsungS3 samsungS3 = new SamsungS3();

  // As we dont have samsung charger as per design
  // we will create a new Samsung Adapter Plug which will
  // map interface of Nokia charger to the interface of 
  // SamsungS3 charging face.
  SamsungAdapterPlug samsungAdapterPlug = new SamsungAdapterPlug(samsungS3);

  // Calling Nokia charging method
  nokiaAsha.charge();
  nokiaLumia.charge();

  // Calling Samsung Adapter Plug charge
  // method , which will internally call
  // SamsungS3 charge method.
  samsungAdapterPlug.charge();

 }

}



Output of the program :