Number of possible triangles from given array of numbers
January 3, 2013
Print first non-repeating character in the string
January 7, 2013

Adapter design pattern (wrapper)

We talked about the Facade design pattern yesterday which provides a wrapper over a sub-system (or a set of functionality.
Adapter design pattern also provides a wrapper over the functionality of a class, the difference is that Adapter convert the interface of a class into another interface which clients expect.
Adapter, the structural design pattern, lets classes work together that could not otherwise because of incompatible interfaces. Hence it is an absolute wrapper over a class (and not a wrapper over a functionality as Facade pattern).

This of a real life example from where the name of this pattern has come. Suppose you are from America. You power adapters will be like

adapter_1

(with rectangular pins, unlike the round pin adapters in India). You come to India, and all the plugs have round pins like shown below.

adapter_2

This means that the interface of library is not compatible with the way our code can call it. Hence, we need an adapter.

adapter_3

Pictures say a lot more than words, I hope the concept of Adapter is clear. Now lets get back to computer science:
Adapter is about creating an intermediary abstraction that translates, or maps, the old component to the new system. Clients call methods on the Adapter object which redirects them into calls to the legacy component.

adapter_4

Please note the difference between Adapter design pattern and Facade design pattern which is a wrapper over the functionality, i.e a subsystem and not just a wrapper to change the interface.

Leave a Reply

Your email address will not be published. Required fields are marked *