Computer Science Design Patterns/Adapter

{{Computer Science Design Patterns/Page 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:
 * mode={{{mode|reading}}}
 * page={{{page|{{SUBPAGENAME}}}}}
 * stage=25
 * stage date=21 May 2013
 * stage comment=Add more illustrations.
 * previous=Abstract Factory
 * next=Bridge
 * content=

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

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

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

{{TODO|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.

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

One list of employees is available in a LDAP you can access via the  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, :

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

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

Class Adapter
}}