Adapter

Singleton Computer Science Design Patterns
Adapter
Bridge

The adapter pattern is used when a client class has to call an incompatible provider class. Let's imagine a MailingClient class that needs to call a method on the LegacyEmployee class:

MailingClient
+firstName: String
+lastName: String
 
IEmployee
+mailEmployee(firstName: String, lastName: String)
 
LegacyEmployee
+mailEmployee(fullName: String)

MailingClient already calls classes that implement the IEmployee interface but the LegacyEmployee doesn't implement it. We could add a new method to LegacyEmployee to implement the IEmployee interface but LegacyEmployee is legacy code and can't be changed. We could modify the MailingClient class to call LegacyEmployee but it needs to change every call. The formatting code would be duplicated everywhere. Moreover, MailingClient won't be able to call other provider class that implement the IEmployee interface any more.

So the solution is to code the formatting code in another independent class, an adapter, also called a wrapper class:

IEmployee
+mailEmployee(firstName: String, lastName: String)
UML Open arrow up.svg
MailingClient
+firstName: String
+lastName: String
Pfeil2.svg
EmployeeAdapter
+mailEmployee(firstName: String, lastName: String)
Pfeil2.svg
LegacyEmployee
+mailEmployee(fullName: String)

EmployeeAdapter implements the IEmployee interface. MailingClient calls EmployeeAdapter. EmployeeAdapter formats the data and calls LegacyEmployee. This type of adapter is called an object adapter. The other type of adapter is the class adapter.

Clipboard

To do:
Describe the class adapter.

Examples

The WebGL-2D is a JavaScript library that implements the adapter pattern. This library is used for the HTML5 canvas element. The canvas element has two interfaces: 2d and WebGL. The first one is very simple to use and the second is much more complex but optimized and faster. The WebGL-2D 'adapts' the WebGL interface to the 2d interface, so that the client calls the 2d interface only.

Cost

Think twice before implementing this pattern. This pattern should not be planned at design time. If you plan to use it for a project from scratch, this means that you don't understand this pattern. It should be used only with legacy code. It is the least bad solution.

Creation

Its implementation is easy but can be expensive. You should not have to refactor the code as the client and the provider should not be able to work together yet.

Maintenance

This is the worst part. Most of the code has redundancies (but less than without the pattern). The modern interface should always provide as much information as the legacy interface needs to work. If one information is missing on the modern interface, it can call the pattern into question.

Removal

This pattern can be easily removed as automatic refactoring operations can easily remove its existence.

Advices

  • Put the adapter term in the name of the adapter class to indicate the use of the pattern to the other developers.

Implementation

Object Adapter

Implementation in Java

Our company has been created by a merger. One list of employees is available in a database you can access via the CompanyAEmployees class:

  1. /**
    
  2.  * Employees of the Company A.
    
  3.  */
    
  4. public class CompanyAEmployees {
    
  5.   /**
    
  6.    * Retrieve the employee information from the database.
    
  7.    *
    
  8.    * @param sqlQuery The SQL query.
    
  9.    * @return The employee object.
    
  10.    */
    
  11.   public Employee getEmployee(String sqlQuery) {
    
  12.       Employee employee = null;
    
  13.  
    
  14.       // Execute the request.
    
  15.  
    
  16.       return employee;
    
  17.     }
    
  18. }
    

One list of employees is available in a LDAP you can access via the CompanyBEmployees class:

  1. /**
    
  2.  * Employees of the Company B.
    
  3.  */
    
  4. public class CompanyBEmployees {
    
  5.   /**
    
  6.    * Retrieve the employee information from the LDAP.
    
  7.    *
    
  8.    * @param sqlQuery The SQL query.
    
  9.    * @return The employee object.
    
  10.    */
    
  11.   public Employee getEmployee(String distinguishedName) {
    
  12.       Employee employee = null;
    
  13.  
    
  14.       // Call the LDAP.
    
  15.  
    
  16.       return employee;
    
  17.     }
    
  18. }
    


To access both to the former employees of the company A and the former employees of the company B, we define an interface that will be used by two adapters, EmployeeBrowser:

  1. /**
    
  2.  * Retrieve information about the employees.
    
  3.  */
    
  4. interface EmployeeBrowser {
    
  5.   /**
    
  6.    * Retrieve the employee information.
    
  7.    *
    
  8.    * @param direction The employee direction.
    
  9.    * @param division The employee division.
    
  10.    * @param departement The employee departement.
    
  11.    * @param service The employee service.
    
  12.    * @param firstName The employee firstName.
    
  13.    * @param lastName The employee lastName.
    
  14.    *
    
  15.    * @return The employee object.
    
  16.    */
    
  17.   Employee getEmployee(String direction, String division, String department, String service, String firstName, String lastName);
    
  18. }
    

We create an adapter for the code of the former company A, CompanyAAdatper:

  1. /**
    
  2.  * Adapter for the company A legacy code.
    
  3.  */
    
  4. public class CompanyAAdatper implements EmployeeBrowser {
    
  5.   /**
    
  6.    * Retrieve the employee information.
    
  7.    *
    
  8.    * @param direction The employee direction.
    
  9.    * @param division The employee division.
    
  10.    * @param department The employee department.
    
  11.    * @param service The employee service.
    
  12.    * @param firstName The employee firstName.
    
  13.    * @param lastName The employee lastName.
    
  14.    *
    
  15.    * @return The employee object.
    
  16.    */
    
  17.   public Employee getEmployee(String direction, String division, String department, String service, String firstName, String lastName) {
    
  18.     String distinguishedName = "SELECT *"
    
  19.       + " FROM t_employee as employee"
    
  20.       + " WHERE employee.company= 'COMPANY A'"
    
  21.       + " AND employee.direction = " + direction
    
  22.       + " AND employee.division = " + division
    
  23.       + " AND employee.department = " + department
    
  24.       + " AND employee.service = " + service
    
  25.       + " AND employee.firstName = " + firstName
    
  26.       + " AND employee.lastName = " + lastName;
    
  27.  
    
  28.     CompanyAEmployees companyAEmployees = new CompanyAEmployees();
    
  29.     return companyAEmployees.getEmployee(distinguishedName);
    
  30.   }
    
  31. }
    

We create an adapter for the code of the former company B, CompanyBAdatper:

  1. /**
    
  2.  * Adapter for the company B legacy code.
    
  3.  */
    
  4. public class CompanyBAdatper implements EmployeeBrowser {
    
  5.   /**
    
  6.    * Retrieve the employee information.
    
  7.    *
    
  8.    * @param direction The employee direction.
    
  9.    * @param division The employee division.
    
  10.    * @param department The employee department.
    
  11.    * @param service The employee service.
    
  12.    * @param firstName The employee firstName.
    
  13.    * @param lastName The employee lastName.
    
  14.    *
    
  15.    * @return The employee object.
    
  16.    */
    
  17.   public Employee getEmployee(String direction, String division, String department, String service, String firstName, String lastName) {
    
  18.     String distinguishedName = "ov1 = " + direction
    
  19.       + ", ov2 = " + division
    
  20.       + ", ov3 = " + department
    
  21.       + ", ov4 = " + service
    
  22.       + ", cn = " + firstName + lastName;
    
  23.  
    
  24.     CompanyBEmployees companyBEmployees = new CompanyBEmployees();
    
  25.     return companyBEmployees.getEmployee(distinguishedName);
    
  26.   }
    
  27. }
    
Implementation in Ruby

Ruby

class Adaptee
  def specific_request
    # do something
  end
end
 
class Adapter
  def initialize(adaptee)
    @adaptee = adaptee
  end
 
  def request
    @adaptee.specific_request
  end
end
 
client = Adapter.new(Adaptee.new)
client.request
Implementation in Python
class Adaptee:
    def specific_request(self):
        return 'Adaptee'
 
class Adapter:
    def __init__(self, adaptee):
        self.adaptee = adaptee
 
    def request(self):
        return self.adaptee.specific_request()
 
client = Adapter(Adaptee())
print client.request()
Implementation in Scala
trait Socket220V {
  def plug220()
}
 
trait Socket19V {
  def plug19()
}
 
class Laptop extends Socket19V {
  def plug19() {
    println("Charging....")
  }
}
 
class LaptopAdapter(laptop: Laptop) extends Socket220V {
  def plug220() {
    println("Transform1...")
    laptop.plug19()
  }
}
 
object Test {
  def main(args: Array[String]) {
    //you can do it like this:
    new LaptopAdapter(new Laptop).plug220()
 
    //or like this (doesn't need LaptopAdapter)
    new Laptop with Socket220V {
      def plug220() {
        println("Transform2...")
        this.plug19()
      }
    } plug220()
  }
}

Class Adapter

Implementation in Python
class Adaptee1:
    def __init__(self, *args, **kw):
        pass
 
    def specific_request(self):
        return 'Adaptee1'
 
class Adaptee2:
    def __init__(self, *args, **kw):
        pass
 
    def specific_request(self):
        return 'Adaptee2'
 
class Adapter(Adaptee1, Adaptee2):
    def __init__(self, *args, **kw):
        Adaptee1.__init__(self, *args, **kw)
        Adaptee2.__init__(self, *args, **kw)
 
    def request(self):
        return Adaptee1.specific_request(self), Adaptee2.specific_request(self)
 
client = Adapter()
print client.request()


Clipboard

To do:
Add more illustrations.


Singleton Computer Science Design Patterns
Adapter
Bridge


You have questions about this page?
Ask it here:


Create a new page on this book:

Last modified on 19 January 2014, at 06:18