Adapter (aka Wrapper)
Intent
Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Problem
How can a class be reused that does not have the interface clients require?
How can classes that have incompatible interfaces work together?
Solution
Define a separate
adapter
class that converts the incompatible interface of a class adaptee into another interface target clients require.Work through an adapter to work with (reuse) classes that do not have the required interface.
Advantages
✅ Supports reusability.
Note
There are two ways of implementing the adapter pattern
Object Adapter
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.
Uses object composition to implement a
Target
interface in terms of (by delegating to) an Adaptee object.
Class Adapter
This adapter pattern uses multiple polymorphic interfaces implementing or inheriting both the interface that is expected and the interface that is pre-existing.
Uses inheritance to implement a
Target
interface in terms of (by inheriting from) an Adaptee class.
Last updated