Tuesday, November 19, 2013

Adapter Design Pattern Example

Adapter Design Pattern:  The adapter design pattern is a design pattern that translates one interface for a class into a compatible interface.


Adapter converts the interface of a class into another interface which is required or expect by clients. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

Adapter also known as wrapper, an alternative naming shared with the Decorator pattern according to the GoF Design Patterns book.

Situations: During application development some time we face the following situations 

  • Sometimes a toolkit or class library can not be used because its interface is incompatible with the interface required by an application. 
  • We can not change the library interface, since we may not have its source code. 
  • Even if we did have the source code, we probably should not change the library for each domain-specific application.

When to use: The Adapter pattern can be used when

  • You want to use an existing class, and its interface does not match the one you need
  • You want to create a reusable class that cooperates with unrelated classes with incompatible interfaces
Implementation Issues: How much adapting should be done?
  • Simple interface conversion that just changes operation names and order of arguments
  • Totally different set of operations