The Adapter pattern acts as an intermediary between two classes, converting the interface of one class so that it can be used with the other. This enables classes with incompatible interfaces to work together. The Adapter pattern implements an interface known to its clients and provides access to an instance of a class not known to its clients. An adapter object provides the functionality of an interface without having to know the class used to implement that interface.
The following lists the benefits of using the Adapter pattern:
- Allows two or more incompatible objects to communicate and interact
- Improves reusability of older functionality
You should use the Adapter pattern when:
- You want to use an existing class, and its interface does not match the interface you need.
- You want to create a reusable class that cooperates with unrelated or unforeseen classes—that is, classes that don’t necessarily have compatible interfaces.
- You want to use an object in an environment that expects an interface that is different from the object’s interface.
- Interface translation among multiple sources must occur.