Adapter Pattern

 

Definition

An adapter helps two incompatible interfaces to work together. This is the real world definition for an adapter. Interfaces may be incompatible but the inner functionality should suit the need. The Adapter design pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.

 

In software engineering, the adapter pattern is a software design pattern that allows the interface of an existing class to be used from another interface.[1] It is often used to make existing classes work with others without modifying their source code.

 

Structure

 

There are two types of adapter patterns:

 

 

Object Adapter pattern

 

In this type of adapter pattern, the adapter contains an instance of the class it wraps. In this situation, the adapter makes calls to the instance of the wrapped object.

 

object adapter

 

Class Adapter pattern


This type of adapter uses multiple polymorphic interfaces implementing or inheriting both the interface that is expected and the interface that is pre-existing. It is typical for the expected interface to be created as a pure interface class, especially in languages such as Java that do not support multiple inheritance of classes.

 

class adapter

 

The adapter pattern is useful in situations where an already existing class provides some or all of the services needed but does not use the interface needed.

 

A good real life example is an adapter that converts the interface of a Document Object Model of an XML document into a tree structure that can be displayed.