Understanding Adapter Design Pattern with a Case Study

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 …


This content originally appeared on DEV Community and was authored by DEV Community

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 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.

View Original: Adapter Design Pattern


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-03-10T06:21:21+00:00) Understanding Adapter Design Pattern with a Case Study. Retrieved from https://www.scien.cx/2022/03/10/understanding-adapter-design-pattern-with-a-case-study/

MLA
" » Understanding Adapter Design Pattern with a Case Study." DEV Community | Sciencx - Thursday March 10, 2022, https://www.scien.cx/2022/03/10/understanding-adapter-design-pattern-with-a-case-study/
HARVARD
DEV Community | Sciencx Thursday March 10, 2022 » Understanding Adapter Design Pattern with a Case Study., viewed ,<https://www.scien.cx/2022/03/10/understanding-adapter-design-pattern-with-a-case-study/>
VANCOUVER
DEV Community | Sciencx - » Understanding Adapter Design Pattern with a Case Study. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/03/10/understanding-adapter-design-pattern-with-a-case-study/
CHICAGO
" » Understanding Adapter Design Pattern with a Case Study." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/03/10/understanding-adapter-design-pattern-with-a-case-study/
IEEE
" » Understanding Adapter Design Pattern with a Case Study." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/03/10/understanding-adapter-design-pattern-with-a-case-study/. [Accessed: ]
rf:citation
» Understanding Adapter Design Pattern with a Case Study | DEV Community | Sciencx | https://www.scien.cx/2022/03/10/understanding-adapter-design-pattern-with-a-case-study/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.