Adapter Design Pattern

The Adapter pattern is a Structural design pattern that works as a bridge between multiple interfaces.

The Adapter Pattern will contain a class that will interface over another class that is not compatible in your application, and will make it compatible. The Adapter pattern is useful for connecting new code to a legacy application. For example, a new data source that contains similar data but different naming conventions and types. The Adapter class will return the data to the client the same way as our original data source.

Here’s another example that might be useful. Let’s say you want to connect an HDMI device to an old Box TV. Box TVs don’t have HDMI inputs, so you would need an adapter to convert HDMI to VGA. This is how this design pattern works. You have an application (TV) and a new class (HDMI) which can’t inherit from the same interface as RCA cables. You would build an Adapter class that interfaces over HDMI that can be used by TV in order to make this connection work.

Let’s look at another example in Java.

Here we have a Company interface that contains 4 methods to be implementedWe have a CompanyDB class that inherits Company. These are essentially Getters for id, name, description and foundedDate. Let’s say we get another data source to pull from in our application CompanyOtherDB (Not a recommended class name) . This contains similar data but different names and types. We can’t directly inherit Company from this class because there are different names and types. The Adapter pattern will take care of this for us. Let’s look at each class step by step.

  1. Create the Company Interface.
public interface Company {
    int getId();
    String getName();
    String getDescription();
    String getFoundedDate();
}
  1. Create the CompanyDB class that inherits Company. Add the private variables for id, name, description and foundDate. Add the constructor as well.
public class CompanyDB implements Company {

    private int id;
    private String name;
    private String description;
    private String foundedDate;

    public CompanyDB(int id, String name, String description, String foundedDate) {
        this.id = id;
        this.name = name;
        this.description = description;
        this.foundedDate = foundedDate;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public String getFoundedDate() {
        return foundedDate;
    }
}
  1. Create the class CompanyOtherDB. This class acts as an interface to other data source we mentioned above. I’m sure when you look at this class, there are ways to make this class inherit Company, but that’s not the point of the pattern. The goal is here to not change any legacy code, because remember, we are adding this class after the fact.
public class CompanyOtherDB {

    private String cid;
    private String companyName;
    private String companyInformation;
    private String dateFounded;

    public CompanyOtherDB(String cid, String companyName, String companyInformation, String dateFounded) {
        this.cid = cid;
        this.companyName = companyName;
        this.companyInformation = companyInformation;
        this.dateFounded = dateFounded;
    }

    public String getCid() {
        return cid;
    }

    public String getCompanyName() {
        return companyName;
    }

    public String getCompanyInformation() {
        return companyInformation;
    }

    public String getDateFounded() {
        return dateFounded;
    }
}
  1. Create CompanyAdapter that inherits Company. In this class, we create a private instance of CompanyOtherDB and set this in the Constructor. In the override methods, we return the properties from CompanyOtherDB as if they work with Company. 
/**
 * Inherits Company
 */
public class CompanyAdapter implements Company {

    //Instance of CompanyOtherDB
    private CompanyOtherDB companyOtherDB;

    //Takes an instance as parameter
    public CompanyAdapter(CompanyOtherDB companyOtherDB) {
        this.companyOtherDB = companyOtherDB;
    }

    /**
     * Returns the cid as an integer instead of a string
     *
     * @return
     */
    @Override
    public int getId() {
        return Integer.parseInt(companyOtherDB.getCid());
    }

    /**
     * Returns companyName as Company.name
     * @return
     */
    @Override
    public String getName() {
        return companyOtherDB.getCompanyName();
    }

    /**
     * Returns companyInformation as Company.description
     * @return
     */
    @Override
    public String getDescription() {
        return companyOtherDB.getCompanyInformation();
    }

    /**
     * Returns dateFounded as Company.foundedDate
     * @return
     */
    @Override
    public String getFoundedDate() {
        return companyOtherDB.getDateFounded();
    }
}
  1. In the CompanyClient, we create an instance of Company and CompanyOtherDB. We pass companyOtherDB into CompanyAdapter and it will return us an instance of Company. That is how the Adapter Design Pattern works.
public class CompanyClient {

    public static void main(String[] args){
        //Instance of company
        Company company = new CompanyDB(1, "Amazon", "Online shopping", "1994");

        //Instance of companyOtherDB
        CompanyOtherDB companyOtherDB = new CompanyOtherDB("2", "Best Buy", "Electronics Retailer", "1966");

        //This won't work!
        //Company company2 = new CompanyOtherDB("3", "Facebook", "Social Media and Technology", "2004");
        //Error: Incompatible Types

        Company newCompany = new CompanyAdapter(companyOtherDB);

        System.out.println("Company Id from Original DB: " + company.getId());
        System.out.println("Company Name from Original DB: " + company.getName());
        System.out.println("Company Description from Original DB: " + company.getDescription());
        System.out.println("Company Founded Date from Original DB: " + company.getFoundedDate());
        System.out.println();
        System.out.println("Company Id from New DB: " + newCompany.getId());
        System.out.println("Company Name from New DB: " + newCompany.getName());
        System.out.println("Company Description from New DB: " + newCompany.getDescription());
        System.out.println("Company Founded Date from New DB: " + newCompany.getFoundedDate());
    }
}
Company Id from Original DB: 1
Company Name from Original DB: Amazon
Company Description from Original DB: Online shopping
Company Founded Date from Original DB: 1994

Company Id from New DB: 2
Company Name from New DB: Best Buy
Company Description from New DB: Electronics Retailer
Company Founded Date from New DB: 1966