Adapter Design Pattern – Tutorial and Example

What is Adapter Design Pattern

The Adapter Design pattern is use when a client expects interfaceA but is provided with interfaceB. The adapter maps the methods from interfaceB to interfaceA. This pattern is used when the client expects certain methods in an interface or class(adaptee) but the adaptee does not provide those methods (Note that the adaptee does provide the required business logic but the methods that it exposes is different from what a client expects.).

Example of Adapter Design Pattern

We can understand Adapter design pattern using the real life example of travel adapters. If you have a British plug but an American socket then you are in trouble. The client (British plug) expects the socket (adaptee) to provide electrical connection. The socket does that but unfortunately the client (plug) cannot ‘connect to'(call methods of) the socket. In comes the Adapter to the rescue! The adapter has two characteristics. First, it can accept British plugs and second it can connect to American sockets. Its only role is to connect to both the client (plug) and the adaptee (socket) and to pass electricity between them (method call)

Lets develop this into an example. Here are the various classes

The Client is the British Plug with British Pin

The Target is the British Socket.

The Adaptee is the American Socket

The adapter to adapt between American And British Pins

The actual implementation of the American Socket Interface

Heres’s the class diagram of adapter pattern.
Adapter Design Pattern

keywords and variations in the adapter pattern
  • In The example above the Adapter implements the Adaptee interface and hence its referred to as a class adapter.
  • It would be good if the adapter can also be used for American plugs. Then we dont have to remove the adapter from the socket everytime we want to use an American plug after a British one. To be able to do that we can have the adapter implement both the American Socket as well as the British Socket. This is referred to as a Two way adapter
  • The Adapter can also extend the AmericanSocketImpl class instead of implementing the AmericanSocket interface. This would be an Object adapter.
  • Sometimes the Adapter does not need to know the adaptee. That is, it does not need to extend or implement adaptee interface or its classes. We could just pass the adaptee to the adaptor and provide a map that says that method A from the client needs to be forwarded to method B from the adaptee. Of course, to get access to the method B the adapter will have to use the reflection API. This is known as the pluggable adapter.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to top
Bitnami