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 power adapter in your hotel room?
Then you can understand the feeling of a developer when issues arise during 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 less.
i) In your case- buying a new compatible power adapter
ii) In the developers’ case- reworking the code because the changes broke something.
The real-time solution in each case is the power adapter; one is physical while the other is virtual.
In this blog, we will understand the adapter design pattern and how it deals with incompatible class issues.
What is Adapter Design Pattern?
The 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 objects with different class 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 USB adapter charger doesn’t physically alter either the phone’s charging point or the plug point).
Let’s understand the adapter design pattern with the help of a real-world example of a 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 the adapter wraps it. 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 of class 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 the manner it expects.
What are the Types of Adapter Design Patterns?
There are two types of adapter patterns,
- Object adapter pattern
The object adapter can be used in all programming languages. Based on the composition principle, the adapter wraps one object while implementing the interface of the other object.
- Class adapter pattern
The class adapter can be used only with languages compatible with multiple inheritances, e.g., C++. The adapter inherits the interfaces from both objects simultaneously based on multiple inheritances.
Moreover, composition allows for greater flexibility, making it easier to change your existing code without breaking something else. Hence the object adapter pattern is the more widely used method of the two.
Case Study of Adapter Design Pattern
As a custom web application 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 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 centers to rent their unused parking spaces. This win-win situation enabled our clients 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 data placement to the data type, was entirely 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
Pros of Adapter Design Pattern
- Because it adheres to the 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 your application’s main business logic code.
- It allows for more excellent code reusability and codes flexibility.
- It comes to your rescue when you have to make things work together after they’ve been designed.
Cons of Adapter Design Pattern
- Code can become too complex as the adapter pattern requires the introduction of new classes and interfaces.
- As each client request is forwarded, it might affect the overall performance.
When to Use Adapter Design Pattern?
Adapter design patterns can be used when,
- you want to use an existing interface of class that 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.
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. Means, you should first recognize 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 correct 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.
And if you liked this post, subscribe to get informative posts about web and mobile development, data analytics, and eCommerce delivered to your inbox. Thank you for reading!
Digital Marketing Manager
Responsible for developing and managing web presence, Sarah has been associated with eLuminous Technologies for 7+ years. Strategic and innovative with a passion for Content Marketing and enhancing brand awareness. Administered all business marketing operations and advertisement campaigns that eventually increased web traffic. She works under the motto “Think like a Publisher, not a Marketer.”