Adapter Design Pattern in JavaThe Adapter design pattern in Java is a way to make two objects with different interfaces work together. Sometimes, we have objects that we want to use together, but their interfaces are not compatible. In such cases, we can use the Adapter pattern. The adapter pattern acts as a middleman or wrapper between these two objects. It takes the calls made to one object and translates them into a format that the other object can understand. This way, the two objects can collaborate seamlessly, even though they have incompatible interfaces. Advantages of Using the Adapter Design Pattern
Real Life Example of Adapter Design PatternOne of the great real-life example of adapter design pattern is mobile charger. Mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So, the mobile charger works as an adapter between mobile charging socket and the wall socket. We will try to implement multi-adapter using adapter design pattern in this section. So, first of all we will have two classes - Volt (to measure volts) and Socket (producing constant volts of 120V). Volt.java Now we want to build an adapter that can produce 3 volts, 12 volts and default 120 volts. In order to achieve this, we will create an adapter interface with these methods. Two Way Adapter PatternWhile implementing Adapter pattern, there are two approaches class adapter and object adapter. However, both these approaches produce same result. Class AdapterThe Class Adapter pattern uses Java inheritance to extend the source interface and provide adapter functionality. The adapter class inherits from the source interface and implements the target interface. It then overrides the methods of the source interface to delegate to the target interface. Advantages:
Disadvantages:
Object AdapterThe Object Adapter pattern uses Java composition to wrap the source object and provide adapter functionality. The adapter class contains an instance of the source object and implements the target interface. It then delegates call to the target interface to the source object. Advantages:
Disadvantages:
Class Adapter ImplementationHere is the class adapter approach implementation for adapter. SocketClassAdapterImpl.java Object Adapter ImplementationHere is the Object adapter implementation for adapter. SocketObjectAdapterImpl.java Note that both the adapter implementations are almost same and they implement the SocketAdapter interface. The adapter interface can also be an abstract class. Here, is a test program to consume our adapter design pattern implementation. AdapterPatternTest.java Output: v3 volts using Class Adapter=3 v12 volts using Class Adapter=12 v120 volts using Class Adapter=120 v3 volts using Object Adapter=3 v12 volts using Object Adapter=12 v120 volts using Object Adapter=120 Overall, the Adapter design pattern is a powerful tool that can be used to solve a variety of problems. It is especially useful when working with incompatible interfaces or legacy code. Next TopicBest Automation Frameworks for Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India