Adapter
Definition
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.
Class Diagram
Participants
-
Target
- defines the domain-specific interface that Client uses.
-
Client
- collaborates with objects conforming to the Target interface.
-
Adaptee.
- defines an existing interface that needs adapting.
-
Adapter.
- adapts the interface Adaptee to the Target interface.
Benefits
- Allow two or more incompatible objects to communicate and interact.
- Improves reusability of older functionality.
Usage
- When you want to use an existing class, and its interface does not match the interface you need.
- When you want to create a reusable class that cooperates with unrelated or onforeseen classes, classes that don't necesarily have compatible interfaces.
- When you want to use an object in an environment that expects an interface that is diferent from the object's interface.
- When you must ensure interface translation among multiple sources.