Design Patterns - Adapter Pattern

adapter-pattern

Convert the interface of a class into another interface that the client expect. Adapter lets class work together that could not otherwise because of incompatibility interfaces. ~ Gang of Four

   
Intent Match an existing object beyond your control to particular interface
Problem Unable to use certain classes because of their interface mismatch
Solution The Adapter provides a wrapper with the desired interface
Participants and Collaborators The Adapter adapts the interface of an Adaptee to match that one of the Adapter’s Target, which allows the Client to use the Adaptee as it were a type of Target
Consequences The Adapter pattern allows for preexisting objects to fit into new class structure without being limited by their interface
Implementation Contain the existing class in another class. Have the containing class match the required interface and call the methods of the contained class

adapter-pattern

Summary

  • When you need to use an existing class and it’s interface is not the one you need, use Adapter
  • It is implemented by creating a new class with the desired interface and then wrap the original class methods to effectively contain the adapted object.
  • An adapter wraps an object to change its interface, a decorator wraps an object to add new behavior and responsibilities and facade wraps a set of objects to simply.