# Computer Science Design Patterns/Adapter

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)
* MailingClient
* +firstName: String +lastName: String
* EmployeeAdapter
* +mailEmployee(firstName: String, lastName: String)
* 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.

## 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

Our company has been created by a merger. One list of employees is available in a database you can access via the CompanyAEmployees class:
One list of employees is available in a LDAP you can access via the CompanyBEmployees class:
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:
We create an adapter for the code of the former company A, CompanyAAdapter:
We create an adapter for the code of the former company B, CompanyBAdapter: