Adapter pattern. How to convert the interface of a class into another interface which client expects?

Adapter pattern. How to convert the interface of a class into another interface which client expects?

Hey. Do you want to convert the interface of a class into another interface the client expects. Try me, maybe? :D. -- Adapter pattern


This is a continuation of the software design pattern series. You can checkout the previous posts from my software-design-pattern series


Adapter pattern

The Adapter Pattern converts the interface of a class into another interface the clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.


Ok. So where can I use Adapter pattern?

When you want to
-> Reuse several existing subclasses that lack some common functionality that can’t be added to the superclass.
-> Convert interface of the class to another interface.

Example: Importing a Mobile from US and you try to plug in the charging power cable to the socket and then you realize that the plug doesn't suit Indian socket. Now the adapter comes to the rescue. You introduce the adapter between them and resulting in plugging the power cable to the socket through the adapter. Exactly the same happens in Adapter pattern too.

Cool. So how to implement Adapter pattern?

In Adapter pattern, Three components gets involved excluding the Client.

  • Target interface
  • Adapter class
  • Adaptee concrete class

-> Target interface

The Interface to which the class or subclass must follow to be able to collabrate with the client code

-> Adapter class

The class implementing the target interface and contains an instance of the class it wraps

-> Adaptee concrete class

The class/subclass which should be converted in such a way to follow the target interface. The instance of this class will be acting as a instance variable in the Adapter class.


There are two types of adapter pattern

  • Object adapter pattern
  • Class adapter pattern
The object adapter way implements the target interface by delegating to an adaptee object at run-time (adaptee.specificOperation()).
The class adapter way implements the target interface by inheriting from an adaptee class at compile-time (specificOperation()).

This article is on the object adapter pattern.

Design Structure

image.png

photo courtesy: link


Sample code

Output

Going to connect the IndianPlaystationPlug
Indian playstation plug connector connected
Going to connect the ImportedPlaystationPlugFromUS through the USToIndianPlugConnectorAdapter
Imported playstation plug connector connected
Connected through the USToIndianPlugConnectorAdapter adapter

As seen in the code example, the imported playstation plug from US can be connected to Indian socket through the adapter. Similarly, the design can be scaled to software components too.


Hope this helps. Looking forward to more learning and sharing. Cheers :)

Subscribe to my newsletter if you feel like getting notification when I publish new articles.


Stay connected:

Connect on Twitter

Connect on Linkedin


ps: Article cover courtesy: Photo by Kumpan Electric on Unsplash

Did you find this article valuable?

Support Samuel Johnson R by becoming a sponsor. Any amount is appreciated!