Have you ever thrown up your hands in frustration when after a hectic day of travel, you find your phone’s charger is incompatible with the one in your hotel room?
If you have, we know the feeling as software developers too encounter such issues when developing software, whereby two objects with different interfaces in the code need to interact with each other.
The challenge in both cases is to find a solution that costs lower than, i) buying a new compatible phone charger and ii) reworking the code because the changes broke something.
The answer in both cases is an adapter, one is physical while the other one is virtual.
In this post, we look at the adapter pattern, a software design pattern that helps developers deal with object incompatibility issues. If you don’t know what is a software design pattern, do read the linked post.
What is Adapter Design Pattern?
Adapter design pattern belongs to the structural category of software design patterns. Structural design patterns deal with the composition of classes and objects in code. The adapter pattern, as such, presents a way to convert an object into another object that the client expects.
It does this by adapting one object to another object. This allows for objects with different interfaces to work together seamlessly. Like its physical counterpart, the software adapter pattern doesn’t add any extra functionality to the interfaces of the objects in question (a travel adapter charger doesn’t physically alter either the phone’s charging point or the plug point).
Universal Travel Charger
Rather, it simply converts the interface of one object to that of another object. This makes it different from the decorator design pattern, which adds new functionality to an existing object’s interface. The adapter pattern, as such, doesn’t change the object’s existing interface.
Again, as with real-life adapters, the client (i.e. user) cannot see the complex conversion executed by an adapter as it completely wraps an object. The latter isn’t even aware that it’s wrapped by the adapter. For example, an object that functions in grams and kilograms can be wrapped with an adapter that converts such data to the imperial units of ounces and pounds.
Adapter Design Pattern UML Diagram
As this diagram shows, the Adapter gets an interface that is compatible with an existing object (i.e. the client). The client then uses this interface to call the adapter’s method (i.e. to execute a function). Once it receives this call, the adapter passes on the request to the other object (i.e. Adaptee), but in a manner that it expects.
What are Types of Adapter Design Pattern?
There are two types of adapter patterns,
- i) Object adapter pattern – Based on the composition principle, the adapter wraps one object while implementing the interface of the other object.
- ii) Class adapter pattern – Based on multiple inheritance, the adapter inherits the interfaces from both objects simultaneously.
While object adapter can be used in all programming languages, the class adapter can be used only with languages compatible with multiple inheritance, e.g. C++.
Moreover, composition allows for greater flexibility as it makes it easier to change your existing code without breaking something else.
Both these reasons mean that the object adapter pattern is the more widely used method of the two.
Adapter Design Pattern Case Study: Real-Life Example
As a custom app development company, we had the opportunity to use the adapter design pattern for a very unique real-life problem brought to us by a client.
The client was based in the UK and operated a business of renting parking spaces. Seeing how expensive and full of hassle it was to land a nice parking space, the client approached malls and large shopping centres to rent their unused parking spaces. This win-win situation enabled our client to allocate all the unused parking spaces to their clients.
The task in front of us was to develop a system that reads the parking data from different parking spaces and uses it to bill a client based on their usage.
However, there was a challenge as each parking area used a different MS Excel format. And by different, we don’t mean just different column names. Every little thing from the placement of data to the type of data was completely different for each parking area.
The solution was obvious – use an adapter design pattern.
We used adapters to reconcile all these differences in data, helping create a system that seamlessly read data from different source formats and used it to bill the user.
Pros and Cons of Adapter Design Pattern
Advantages of Adapter Pattern
- Because it adheres to open/closed principle, you can add newer types of adapters to the code without breaking your existing client code.
- As it follows the single responsibility principle, you can separate the interface code from the main business logic code of your application.
- It allows for greater code reusability and code flexibility.
- It comes to your rescue when you have to make things work together after they’ve been designed.
Limitations of Adapter Pattern
- Code can become too complex as the adapter pattern requires the introduction of new classes and interfaces.
- As each client request is forwarded, overall performance might be hit.
When to Use Adapter Design Pattern?
Adapter design pattern can be used when,
- you want to use an existing class whose interface is not compatible with the one you need.
- you want to create a reusable class that can be used with unrelated classes that might have incompatible interfaces.
Over to you
By using the adapter design pattern, you can prevent the menace of duplicate code that often drags a project down. Instead, when confronted with classes that cannot work together because of incompatible interfaces, you can simply use adapters to keep your project on track, fast, and efficient.
However, the challenge in such situations is more about knowing the disease and less about finding the cure. Meaning, you should first recognise the problem affecting your project and only then decide which software design pattern can help you move forward.
As a Web Design & Development Company for close to two decades, we’ve successfully employed the right design patterns to reduce the ‘Go to market’ time for our clients while keeping the underlying code efficient, maintainable, and scalable. To know the right approach for your project, get in touch with us by hiring dedicated developer now.
And if you liked this post, do subscribe to get informative posts about web and mobile development, data analytics, and eCommerce delivered to your inbox. Thank you for reading!