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?
Yes!
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.ย
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.ย ย
Client Backgroundย ย
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. ย
Challengesย ย
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.ย
Also read : What is Observer Design Pattern A Brief Guide with a Case Study
Conclusionย
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.โ