Adapter pattern
Encyclopedia
In computer programming
, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern
that translates one interface
for a class
into a compatible interface. An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface. The adapter translates calls to its interface into calls to the original interface, and the amount of code necessary to do this is typically small. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).
to achieve its goal. The adapter is created by 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
.
The adapter pattern is useful in situations where an already existing class provides some or all of the services you need but does not use the interface
you need. 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. A link to a tutorial that uses the adapter design pattern is listed in the links below.
It is desired for classA to supply classB with some data, let us suppose some String data. A compile time solution is:
However, suppose that the format of the string data must be varied. A compile time solution is to use inheritance:
and perhaps create the correctly "formatting" object at runtime by means of the Factory pattern.
A solution using "adapters" proceeds as follows:
(i) define an intermediary "Provider" interface, and write an implementation of that Provider interface which wraps the source of the data, ClassA in this example, and outputs the data formatted as appropriate:
(ii) Write an Adapter class which returns the specific implementation of the Provider:
(iii) Register the Adapter with a global registry, so that the Adapter can be looked up at runtime:
(iv) In your code, when you wish to transfer data from ClassA to ClassB, write:
or more concisely:
(v) The advantage can be seen in that, if it is desired to transfer the data in a second format, then look up the different adapter/provider:
(vi) And if it is desired to output the data from ClassA as, say, image data in Class C:
(vii) In this way, the use of adapters and providers allows multiple "views" by ClassB and ClassC into ClassA without having to alter the class hierarchy. In general, it permits a mechanism for arbitrary data flows between objects which can be retrofitted to an existing object hierarchy.
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...
, the adapter pattern (often referred to as the wrapper pattern or simply a wrapper) is a design pattern
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...
that translates one interface
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...
for a class
Class (computer science)
In object-oriented programming, a class is a construct that is used as a blueprint to create instances of itself – referred to as class instances, class objects, instance objects or simply objects. A class defines constituent members which enable these class instances to have state and behavior...
into a compatible interface. An adapter allows classes to work together that normally could not because of incompatible interfaces, by providing its interface to clients while using the original interface. The adapter translates calls to its interface into calls to the original interface, and the amount of code necessary to do this is typically small. The adapter is also responsible for transforming data into appropriate forms. For instance, if multiple boolean values are stored as a single integer (i.e. flags) but your client requires a 'true'/'false', the adapter would be responsible for extracting the appropriate values from the integer value. Another example is transforming the format of dates (e.g. YYYYMMDD to MM/DD/YYYY or DD/MM/YYYY).
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.Class Adapter pattern
This type of adapter uses multiple polymorphic interfacesSubtype polymorphism
In programming language theory, subtyping or subtype polymorphism is a form of type polymorphism in which a subtype is a datatype that is related to another datatype by some notion of substitutability, meaning that program constructs, typically subroutines or functions, written to operate on...
to achieve its goal. The adapter is created by 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
Interface (Java)
An interface in the Java programming language is an abstract type that is used to specify an interface that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signature and constant declarations...
class, especially in languages
Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely....
such as Java
Java (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
that do not support multiple inheritance
Multiple inheritance
Multiple inheritance is a feature of some object-oriented computer programming languages in which a class can inherit behaviors and features from more than one superclass....
.
The adapter pattern is useful in situations where an already existing class provides some or all of the services you need but does not use the interface
Interface (computer science)
In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software...
you need. A good real life example is an adapter that converts the interface of a Document Object Model
Document Object Model
The Document Object Model is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML and XML documents. Aspects of the DOM may be addressed and manipulated within the syntax of the programming language in use...
of an XML
XML
Extensible Markup Language is a set of rules for encoding documents in machine-readable form. It is defined in the XML 1.0 Specification produced by the W3C, and several other related specifications, all gratis open standards....
document into a tree structure that can be displayed. A link to a tutorial that uses the adapter design pattern is listed in the links below.
A further form of runtime Adapter pattern
There is a further form of runtime Adapter pattern as follows:It is desired for classA to supply classB with some data, let us suppose some String data. A compile time solution is:
However, suppose that the format of the string data must be varied. A compile time solution is to use inheritance:
and perhaps create the correctly "formatting" object at runtime by means of the Factory pattern.
A solution using "adapters" proceeds as follows:
(i) define an intermediary "Provider" interface, and write an implementation of that Provider interface which wraps the source of the data, ClassA in this example, and outputs the data formatted as appropriate:
(ii) Write an Adapter class which returns the specific implementation of the Provider:
(iii) Register the Adapter with a global registry, so that the Adapter can be looked up at runtime:
(iv) In your code, when you wish to transfer data from ClassA to ClassB, write:
or more concisely:
(v) The advantage can be seen in that, if it is desired to transfer the data in a second format, then look up the different adapter/provider:
(vi) And if it is desired to output the data from ClassA as, say, image data in Class C:
(vii) In this way, the use of adapters and providers allows multiple "views" by ClassB and ClassC into ClassA without having to alter the class hierarchy. In general, it permits a mechanism for arbitrary data flows between objects which can be retrofitted to an existing object hierarchy.
Implementation of Adapter pattern
When implementing the adapter pattern, for clarity use the class name [AdapteeClassName]Adapter. It should have a constructor method with adaptee class variable as parameter. This parameter will be passed to the instance member of [AdapteeClassName]Adapter.See also
- Wrapper functionWrapper functionA wrapper function is a function in a computer program whose main purpose is to call a second function with little or no additional computation. This is also known as method delegation. Wrapper functions can be used for a number of purposes....
- DelegationDelegation (programming)In object-oriented programming, there are two related notions of delegation.* Most commonly, it refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement...
, strongly relevant to the object adapter pattern. - Dependency inversion principleDependency inversion principleIn object-oriented programming, the dependency inversion principle refers to a specific form of decoupling where conventional dependency relationships established from high-level, policy-setting modules to low-level, dependency modules are inverted for the purpose of rendering high-level modules...
, which can be thought of as applying the Adapter pattern, when the high-level class defines their own (adapter) interface to the low-level module (implemented by an Adaptee class).
External links
- Adapter in UML and in LePUS3 (a formal modelling language)
- Description in Portland Pattern Repository's Wiki
- Adapter Design Pattern in CodeProject
- PerfectJPattern Open Source Project, Provides componentized implementation of the Adapter Pattern in Java
- The Adapter Pattern in Python (detailed tutorial)
- The Adapter Pattern in Eclipse RCP
- Adapter Design Pattern