Adapter Pattern - Design Patterns

, ,

When To Go for Adapter Pattern

  • A class that would be useful to your application does not implement the interface you require
  • You are designing a class or framework and you want to ensure it is usable by a wide variety of as yet unwritten classes and applicaations


Scenario for Adapter Pattern


Adapter Pattern Implemented


Intent of the Adapter Pattern

  • Convert the interface of a class into another interface clients expect
  • Allow classes to work together that couldn't otherwise due to incompatible interfaces
  • Future-proof client implementations by having them depend on Adapter Interfaces, rather than concrete classes directly


Use the Adapter Pattern when:

  • You want to use an existing class, but its interface does not match the one you require
  • You want to create a reusable class that cooperates with unrelated or unforeseen classes (i.e. classes that won't necessarily share the same interface)
  • You need to use several existing sub-classes, but it's impractical to adapt their interface by sub-classing everyone


Structure of Adapter Pattern


Other Patterns that use Adapter Pattern

  • Repository - Repository pattern is a very common use of the Adapter pattern
  • Strategy - Adapter pattern is often passed into a class that depends on it, thus implementing the strategy pattern
  • Facade - Adapter and Facade are both wrappers. The facade pattern attempts to simplify the interface and often wraps many classes, while the Adapter typically wraps a single Adaptee, and is not generally concerned with simplifying the interface