JB Header
Adapter Design Pattern in Java
This article explains adapter design pattern in java with class diagrams and example code.
Introduction Adapter class makes classes with incompatible interfaces work together. Adapter Design Pattern is a structural design pattern among the Gang Of Four(GOF)Article on GOF Patterns & their types Design Patterns. I.e. the adapter pattern deals with how the classes are composed to form larger structures. What is Adapter Design Pattern When the target class has an interface which is different from what the client class expects, then adapter pattern solves this problem by converting the interface of the target into the client expected one. As a result classes which otherwise would not have been able to communicate with each other can now communicate with ease. Variants of Adapter Design Pattern Adapter design pattern has 2 variants -
  1. Class Adapters: Class Adapter works by subclassing both the Target and the Adaptee. Adapter overrides the Target interface methods such that every client call made on the overridden target interface is redirected to the Adaptee's inherited method which does the actual operation. Class Adapter's class diagram is as shown below -
    Adapter Pattern Class Adapters Class Diagram
    • Adapter inherits both from Target and Adaptee.
    • The client invokes requiredMethod() on an instance of Adapter which is also an instance of Target by virtue of being its subclass.
    • Adapter overrides the requiredMethod() of Target and in its implementation internally calls Adaptee’s targetMethod().
    • Thus, Client is able to use targetMethod() functionality from Adaptee through the Adapter without change of the interface which still remains Target for the client.
  2. Object Adapters: Object Adapter works by composing the Adaptee and extending the Target. Adapter overrides the Target interface methods such that every client call made to the overridden target interface's method is redirected to the composed Adaptee object's method which does that operation. Let's have a look at the class diagram for Object Adapters -
    Adapter Pattern Object Adapters Class Diagram
    Explanation of Class Diagram
    • Adapter extends Target.
    • Adapter also keeps an instance of Adaptee as a class variable.
    • When Client calls -
      • The Client invokes requiredMethod() on an instance of Adapter which is also an instance of Target by virtue of being its subclass.
      • Adapter overrides the requiredMethod() of Target and in its implementation internally calls its class variable adaptee's targetMethod().
    • Thus, Client is able to use targetMethod() functionality from Adaptee through the Adapter without change of the interface which still remains Target for the client.
Adapter Design Pattern Example in Java Important Note - Class Adapters cannot be implemented in Java as Java does not have multiple inheritance of classes. The below given class diagram is for Object Adapter implementation in Java.
Adapter Pattern's Object Adapter Variant's Java Class Diagram
Note - In the class diagram above I have focussed on showing the primary participating classes & their methods. I have not included the method parameters and constructors in the same.

Code for the classes drawn in Java Class Diagram
Code for classes shown in Java Class Diagram
//FileCommiter.java
import java.io.File;
public class FileCommiter {
  String diskLocation;
  public FileCommiter(){
    //default constructor
  }
  public FileCommiter(String diskLocation){
    this.diskLocation=diskLocation;
  }
  public void saveFile(File file){
   //Logic for saving the file at the diskLocation goes here
  }
}
//FileEmailer.java
import java.io.File;
public class FileEmailer {
  public String emailAddress;
  public FileEmailer(String emailAddress){
    this.emailAddress=emailAddress;
  }  
  public void emailFile(File file){
   //Logic for emailing the file goes here
  }
}
//FileEmailerAdapter.java
import java.io.File;
public class FileEmailerAdapter extends FileCommiter{
  public FileEmailer fileEmailer;
  public FileEmailerAdapter(FileEmailer fileEmailer){
      this.fileEmailer=fileEmailer;
  }  
  public void saveFile (File file){
    this.fileEmailer.emailFile(file);
  } 
}
//Client.java
import java.io.File;
public class Client{
  public static void main(String args[]){
    File file=new File(args[0]);
    FileCommiter fileCommiter=new FileCommiter("C:\\");
    fileCommiter.saveFile(file);//This will save the file on the disk location C: drive
    //Now we want to email with the same Target class FileCommiter
    fileCommiter=new FileEmailerAdapter(new FileEmailer("abcd@javabrahman.com"));
    fileCommiter.saveFile(file);//This will email the file to abcd@javabrahman.com
  }
}
Explanation of the code & class diagram Java Class Diagram is for file handling classes which take care of committing/emailing of files -
  • FileCommiter class has the responsibility of committing files to the disk location. The disk location is set via a constructor parameter.
  • FileEmailer class has the responsibility of emailing files to the email address. The email address is set via a constructor parameter
  • FileEmailerAdapter extends FileCommiter.
  • FileEmailerAdapter has a class variable of type FileEmailer called fileEmailer. This fileEmailer class variable is set via a constructor parameter.
  • Client first creates a new java.io.File instance based on command line argument passed to it.
  • Client then creates a FileCommiter instance with a disk location value of C drive and saves the file instance it created using the saveFile() method.
  • Next the Client creates a FileEmailerAdapter instance using a FileEmailer with an email address.
  • Client then invokes the saveFileMethod() on fileEmailAdapter. This time instead of commiting the file on the disk location FileEmailerAdapter internally routes the request to emailFile() method of class variable adaptee.
  • Thus, with the same interface of FileCommiter, Client is now able to email a file as well.This is adapter pattern.
Summary This concludes the article on adapter design pattern where we saw what is adapter pattern. Then we learned about its two variants - Class and Object Adapters and saw class diagram of each. Then we saw a Java Implementation of a FileEmailer Adapter through code and then its explanation.