Introduction :
It merely “converts the interface of a class into another interface that a client wants,” according to an adapter pattern.
Stated otherwise, to use a class with a different interface’s services while providing an interface that meets client needs.
Another name for the Adapter Pattern is Wrapper.
Two incompatible interfaces that would normally be unable to be connected directly can be joined via an adapter pattern. Transforming an existing interface into one that the client expects is the primary objective of this design.
This pattern has a structure akin to that of the Decorator. That being said, the Decorator is typically applied with the extension in mind. Normally, the first code to connect incompatible interfaces is created first, and then the Adapter is implemented. Let’s go over the two primary methods for applying this pattern.
Advantage of Adapter Pattern in Java
1. It permits the interaction of two or more previously incompatible things.
2. It permits the reuse of already-existing functionality.
Usage of Adapter pattern in Java:
when a class that already exists but has an incompatible interface has to be used by an object.
In situations where you need to design a reusable class that works with classes that lack compatible interfaces.
In situations where you need to design a reusable class that works with classes that lack compatible interfaces.
Key Concepts:
Client: The person interacting with the adapter is the client.
Target: The client is expecting this interface.
Adaptee: The class that requires modification.
The class that covers the adaptee and implements the target interface so that the client can communicate with it is called an adapter.
When to Use the Adapter Pattern in Java:
When a class already exists but you want to utilize it but its interface doesn’t work with what you require.
When a reusable class that interacts with unexpected or unrelated classes needs to be created.
Why do we need Adapter Design Pattern?
1. Integration of Current Code:
Scenario: When you have components or code that already exists, but their interfaces don’t work with the interfaces that new systems or code expects.
Required: With the Adapter design, you can easily incorporate pre-existing components into new systems without changing the source code.
2. Utilizing Current Functionality Again:
Scenario: When you wish to reuse components or classes that offer useful functionality but don’t follow the ideal interface.
Required: By building an adapter that enables it to work with the interfaces that new code expects, the adapter pattern allows you to reuse old code.
3. Interportability
Scenario: When you have to integrate disparate systems or components, particularly when their interfaces differ, you need to provide interoperability.
Required: The Adapter pattern serves as a bridge to enable efficient collaboration between systems with disparate APIs.
4. Client-Server Communication:
Scenario: When developing client-server software, the server may offer a different interface than what the client requires.
Required: Despite interface differences, adapters provide seamless connection between client and server by translating requests and responses.
5. Third-Party Library Integration:
Scenario: When using third-party libraries or APIs in a project and finding that their user interfaces don’t match those of the system as a whole.
Required: By offering a compliant interface for the remainder of the application, adapters enable the use of external components.
Example
Let’s say you want to use the VideoPlayer class to play movies, but your MyMediaPlayer interface can play audio files. The VideoPlayer class has a method for playing videos even if it does not implement the MyMediaPlayer interface.
Step 1: Define the Target Interface
// Target Interface
public interface MyMediaPlayer {
void play(String myAudioType, String fileName1);
}
Step 2: Create the Adaptee Class
// Adaptee Class
public class VideoPlayer {
public void playVideo(String fileName1) {
System.out.println(“Playing video: ” + fileName1);
}
}
Step 3: Create the Adapter Class
// Adapter Class
public class MediaAdapter implements MyMediaPlayer {
private VideoPlayer videoPlayer;
public MediaAdapter(String myAudioType) {
if(myAudioType.equalsIgnoreCase(“video”)) {
videoPlayer = new VideoPlayer();
}
}
@Override
public void play(String myAudioType, String fileName1) {
if(myAudioType.equalsIgnoreCase(“video”)) {
videoPlayer.playVideo(fileName1);
}
}
}
Step 4: Create the Client
// Client
public class MyAudioPlayer implements MyMediaPlayer {
MediaAdapter mediaAdapter;
@Override
public void play(String myAudioType, String fileName1) {
if(myAudioType.equalsIgnoreCase(“mp3”)) {
System.out.println(“Playing audio: ” + fileName1);
} else if(myAudioType.equalsIgnoreCase(“video”)) {
mediaAdapter = new MediaAdapter(myAudioType);
mediaAdapter.play(myAudioType, fileName1);
} else {
System.out.println(“Media type is invalid: ” + myAudioType);
}
}
public static void main(String[] args) {
MyAudioPlayer myAudioPlayer = new MyAudioPlayer();
myAudioPlayer.play(“mp3”, “song.mp3”);
myAudioPlayer.play(“video”, “movie.mp4”);
}
}
How It Works:
Using the MyMediaPlayer interface, the MyAudioPlayer class functions as a client.
The MyAudioPlayer plays the audio straight away if the audio type is mp3.
In the event that the audio type is video, the MyAudioPlayer can play video files by using the MediaAdapter to modify the VideoPlayer to the MyMediaPlayer interface.
Without changing the VideoPlayer class’s current code, this approach facilitates the integration of the VideoPlayer class into a system that anticipates the MyMediaPlayer interface.
Conclusion :
We examined Java‘ s Adapter design pattern in this article. Working with legacy systems and managing the complexity of the codebase need the use of one of the most crucial patterns. Furthermore, it permits the easy modification of implementations at all times and the reuse of third-party libraries without requiring modifications to the program.
For more information & classes Call: 2048553004
Registration Link: Click Here!
Author: Ambarish Durani
JAVA Trainer
IT Education Centre Placement & Training Institute
© Copyright 2024 | IT Education Centre.