Adapter Pattern with an easy example

Design patterns, as their name implies, are models, paradigms, strategies on how to solve certain recurring design problems. Adapter pattern is a design pattern that lets you use classes with incompatible interfaces or a method that requires or returns a different interface than the one being sent.

The adapter pattern rely on a construct called an adapter. This adapter will permit to transform the data that is being transported from one class/object with a particular interface to another class/object with a different, incompatible interface. This adapter adds no additional functionalities, it serves only as a form of transportation and transformation to coincide (or adapt) one interface to another one.

The advantages of using the adapter pattern is that both classes/objects need no alterations to their original interface, the adapter is the one in charge of making sure each class/object receives the required information as indicated by the interface. It also helps with re usability and flexibility by making an adapter that isn’t tied to a specific class/object, but rather will make it easier to intercommunicate the 2 incompatible interfaces.

The downsides of the adapter pattern is that some adaptions may need more than 1 adapter to transform between incompatible interfaces. Also, since the two classes/objects don’t interact directly, there’s an intermediate step, making the process longer and prone to errors.

Nevertheless, using the adapter pattern in the correct situations can prove very useful. To start things off and give a more meaningful description to adapter pattern, let’s propose the following situation:

Situation:

Since you’re being hired to make a local system for a library, your first task was to start and include all authors with their nationality. That’s no problem at all, but local laws require you to submit your registers to an external source, which controls all the registers for all libraries in the area. Since the requirements for the local system differ from the requirements the external source requires, it would require substantially different interfaces.

Planning:

To keep things as simple as possible, you decide early on that two interfaces would only hinder readability and make the code less efficient. With that thought in mind, the solution of using design patterns pops in while researching in the internet and you decide that Adapter Pattern would be a great fit for this particular problem.

Implementing:

To start, you create a console app to test how it would be implemented. This console app will start by asking if the user would like to add a new author, show information about the author or exit.

Basic selection for adding or consulting an author

Once a correct number has been given, it will go into a switch statement to decide what action it will do. For simplicity sake, when asking for a new author, only the name and the nationality will be requested. In case the user selects to show an author information, they’ll be prompted to select which user by the position in the array. Again, since this is an easy example, it’ll only take numbers from 1–9. And of course, the third option is to quit the system

Switch statement

As you can see, commented are 2 pairs of methods, one pair accessed by the local object and one by the external object. This local object will be used to access the local database of the library and the external object to access the external registry.

The head of the library asked for a specific way to insert the authors into the system, separate the first and last names. This way, they can show both in whatever order is needed. For that reason, you create an interface to represent the needs of the client

Base interface for interacting with authors

To make things simpler, instead of asking for the GUID, you’ll ask for the position in the array when retrieving an author’s information, which is a number.

Starting with the local database insertion and consulting, you define a new class called local. This class will have an array where you’ll test out the insertion and retrieval of the author’s information. The base class for each author (as requested by the library’s owner) will be the unique ID, the first name, last name and nationality. When inserting into the array, you convert those into an object and insert it into the array. To show the author’s information, you’ll first check if the index is available and if so, return the information of that author, otherwise, show an error message

Local Class

Having fulfilled the library’s expectation, you proceed to connect to the connection for the external source. While working on it, you find there’s a catch, the interface is different from what the local expects, as well as the names of the methods to insert and retrieve the information. It uses full name instead of first name and last name and the methods have a different name

External Class representing the External Source

Since the external source uses a different interface and methods from the local one, an adapter is needed. The function of this adapter is to transform the existing interface to the one accepted by the other piece of code (in this case, the external source). Its purpose is not to add new functionality, only adapt, transform; to make the interfaces coincide.

This adapter will have the class which needs the adapting (in this case called c) and instantiate it in the constructor of the adapter. Since this adapter uses the interface created earlier, we’d need to create the two methods, the insert and show authors. This two methods will invoke the methods of the external source with its respective interface. The transformation will occur inside the adapters so that in the main program, we can call using the same interface, but being able to use both the local and external sources to store and retrieve information

Adapter Class

Now that the adapter is in place, it’ll take care of invoking the correct methods from the external source with the correct interface using the data we send it. All that’s left is the instancing of both the local and the adapter to send or retrieve the information of any author

Instancing both classes

and finally uncomment the lines to make everything work as expected. The console when initially run should look like this:

Initial Console

When inserting a new author should look as follows:

New Author

When showing an author’s info, it should look like:

And when an author is not in the array, this should show up:

Error

To access the project use the following link: https://github.com/AnthonyWRodriguez/StrategyPattern

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store