Adapter method is a Structural Design Pattern which helps us in making the incompatible objects adaptable to each other. The Adapter method is one of the easiest methods to understand because we have a lot of real-life examples that show the analogy with it. The main purpose of this method is to create a bridge between two incompatible interfaces. This method provides a different interface for a class. We can more easily understand the concept by thinking about the Cable Adapter that allows us to charge a phone somewhere that has outlets in different shapes.
Using this idea, we can integrate the classes that couldn’t be integrated due to interface incompatibility.
Problem without using the Adapter Method
Imagine you are creating an application that shows the data about all different types of vehicles present. It takes the data from APIs of different vehicle organizations in XML format and then displays the information.
But suppose at some time you want to upgrade your application with a Machine Learning algorithms that work beautifully on the data and fetch the important data only. But there is a problem, it takes data in JSON format only.
It will be a really poor approach to make changes in Machine Learning Algorithm so that it will take data in XML format.
Solution using Adapter Method
For solving the problem we defined above, We can use Adapter Method that helps by creating an Adapter object.
To use an adapter in our code:
- Client should make a request to the adapter by calling a method on it using the target interface.
- Using the Adaptee interface, the Adapter should translate that request on the adaptee.
- Result of the call is received the client and he/she is unaware of the presence of the Adapter’s presence.
Class diagram for the Adapter method which is a type of Structural Design pattern:
- Principle of Single Responsibility: We can achieve the principle of Single responsibility with Adapter Method because here we can separate the concrete code from the primary logic of the client.
- Flexibility: Adapter Method helps in achieving the flexibility and reusability of the code.
- Less complicated class: Our client class is not complicated by having to use a different interface and can use polymorphism to swap between different implementations of adapters.
- Open/Closed principle: We can introduce the ne wadapter classes into the code without violating the Open/Closed principle.
- Complexity of the Code: As we have introduced the set of new classes, objects and interfaces, the complexity of or code definitely rises.
- Adaptability: Most of the times, we require many adaptations with the adaptee chain to reach the compatibility what we want.
- To make classes and interfaces compatible : Adapter method is always used when we are in need to make certain classes compatible to communicate.
- Relatable to Inheritance: When we want to reuse some piece of code i.e., classes and interfaces that lack some functionalities, it can be done using the Adapter Method.
Further read: Adapter Pattern in Java
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Factory Method - Python Design Patterns
- Observer method - Python Design Patterns
- Composite Method - Python Design Patterns
- Decorator Method - Python Design Patterns
- Abstract Factory Method - Python Design Patterns
- Builder Method - Python Design Patterns
- Singleton Method - Python Design Patterns
- Prototype Method - Python Design Patterns
- Bridge Method - Python Design Patterns
- Facade Method - Python Design Patterns
- Proxy Method - Python Design Patterns
- Flyweight Method - Python Design Patterns
- Iterator Method - Python Design Patterns
- Memento Method - Python Design Patterns
- Visitor Method - Python Design Patterns
- Strategy Method - Python Design Patterns
- Template Method - Python Design Patterns
- State Method - Python Design Patterns
- Command Method - Python Design Patterns
- Chain of Responsibility - Python Design Patterns
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet