Adapter pattern
From Wikipedia, licensed under CC BY-SA 4.0. View the original article on Wikipedia.
In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface.[1]: 244 It is often used to make existing classes work with others without modifying their source code.
An 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.
Contents
Overview
The adapter[2] design pattern is one of the twenty-three well-known Gang of Four design patterns that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse.
The adapter design pattern solves problems like:[3]
- How can a class be reused that does not have an interface that a client requires?
- How can classes that have incompatible interfaces work together?
- How can an alternative interface be provided for a class?
Often an (already existing) class can not be reused only because its interface does not conform to the interface clients require.
The adapter design pattern describes how to solve such problems:
- Define a separate
adapterclass that converts the (incompatible) interface of a class (adaptee) into another interface (target) clients require. - Work through an
adapterto work with (reuse) classes that do not have the required interface.
The key idea in this pattern is to work through a separate adapter that adapts the interface of an (already existing) class without changing it.
Clients don't know whether they work with a target class directly or through an adapter with a class that does not have the target interface.
See also the UML class diagram below.
Definition
An adapter allows 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.
Usage
An adapter can be used when the wrapper must respect a particular interface and must support polymorphic behavior. Alternatively, a decorator pattern makes it possible to add or alter behavior of an interface at run-time, and a facade pattern is used when an easier or simpler interface to an underlying object is desired.[1]: 243, 252, 258, 260
| Pattern | Intent |
|---|---|
| Adapter or wrapper | Converts one interface to another so that it matches what the client is expecting |
| Decorator | Dynamically adds responsibility to the interface by wrapping the original code |
| Delegation | Support "composition over inheritance" |
| Facade | Provides a simplified interface |
Structure
UML class diagram

In the above UML class diagram, the client class that requires a target interface cannot reuse the adaptee class directly because its interface doesn't conform to the target interface.
Instead, the client works through an adapter class that implements the target interface in terms of adaptee:
- The
object adapterway implements thetargetinterface by delegating to anadapteeobject at run-time (adaptee.specificOperation()). - The
class adapterway implements thetargetinterface by inheriting from anadapteeclass at compile-time (specificOperation()).
Object adapter pattern
In this 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 adapter pattern 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 (before JDK 1.8) that do not support multiple inheritance of classes.[1]: 244


Implementation of the adapter pattern
When implementing the adapter pattern, for clarity, one can apply the class name [ClassName]To[Interface]Adapter to the provider implementation; for example, DAOToProviderAdapter. It should have a constructor method with an adaptee class variable as a parameter. This parameter will be passed to an instance member of [ClassName]To[Interface]Adapter. When the client method is called, it will have access to the adaptee instance that allows for accessing the required data of the adaptee and performing operations on that data that generates the desired output.
See also
- Adapter Java Design Patterns - Adapter
- Delegation, strongly relevant to the object adapter pattern.
- Dependency inversion principle, which can be thought of as applying the adapter pattern, when the high-level class defines its own (adapter) interface to the low-level module (implemented by an adaptee class).
- Ports and adapters architecture
- Shim
- Wrapper function
- Wrapper library
References
- ^ a b c Freeman, Eric; Freeman, Elisabeth; Sierra, Kathy; Bates, Bert (2004). Head First Design Patterns. O'Reilly Media. p. 244. ISBN 978-0-596-00712-6. OCLC 809772256. Archived from the original (paperback) on 2013-05-04. Retrieved 2013-04-30.
- ^ Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 139ff. ISBN 0-201-63361-2.
- ^ "The Adapter design pattern - Problem, Solution, and Applicability". w3sDesign.com. Archived from the original on 2017-08-28. Retrieved 2017-08-12.
- ^ "The Adapter design pattern - Structure and Collaboration". w3sDesign.com. Archived from the original on 2017-08-28. Retrieved 2017-08-12.
| Gang of Four patterns |
| ||||||
|---|---|---|---|---|---|---|---|
| Concurrency patterns | |||||||
| Architectural patterns | |||||||
| Other patterns | |||||||
| Books | |||||||
| People | |||||||
| Communities | |||||||
| See also | |||||||
Text is from Wikipedia, licensed under CC BY-SA 4.0. A link to the original article is included on every page. Original article. Search Wikipedia on nomadlouie.com.
