Adapter Pattern – Scenario 1

Lets say, we have two printers, 1. Epson Printer and 2. HP Printer.

Epson Printer can print text.
HP Printer prints text and also lets the user know how many documents are in the print queue.

This can be coded as follows.

Now I have the Epson Printer but I want to use the DocumentsInQueue method which is of course a HP Printer function. This is the scenario where Adapter pattern comes in handy. Adapter pattern is nothing but making a incompatible interface compatible using an class which is referred as adapter class. Just think of a OTG adapter, Your phone can play videos from an USB pendrive but you can’t plug the pendrive directly to your phone, because of the incompatible interfaces. In this case, you can use a OTG adapter which can connect your phone on one end and pendrive on other end.

So lets fix this incompatibility using an adapter class

Now use this adapter in our main program

Result:

Share your thoughts