Mastering Java Design Patterns - Day 6: Adapter Pattern
Emmanuel Hadjistratis (he/him)
No more security gaps or inefficient APIs | I optimize your backend infrastructure for maximum performance
Welcome to the second week of our journey through Java Design Patterns! This week, we'll explore Structural Design Patterns, starting with the Adapter Pattern. Last week, we delved into various Creational Patterns like Singleton and Prototype. Now, let’s understand how the Adapter Pattern can help in making incompatible interfaces work together.
What is the Adapter Pattern?
The Adapter Pattern allows incompatible interfaces to work together by converting the interface of a class into another interface that a client expects. This pattern is particularly useful when integrating new components into an existing system.
Why Use the Adapter Pattern?
How to Implement the Adapter Pattern in Java
Here's a simple implementation of the Adapter Pattern:
// Target interface
interface MediaPlayer {
void play(String audioType, String fileName);
}
领英推荐
// Adaptee interface
interface AdvancedMediaPlayer {
void playVlc(String fileName);
void playMp4(String fileName);
}
// Concrete Adaptee
class VlcPlayer implements AdvancedMediaPlayer {
public void playVlc(String fileName) {
System.out.println("Playing vlc file. Name: " + fileName);
}
public void playMp4(String fileName) {
// Do nothing
}
}
class Mp4Player implements AdvancedMediaPlayer {
public void playVlc(String fileName) {
// Do nothing
}
public void playMp4(String fileName) {
System.out.println("Playing mp4 file. Name: " + fileName);
}
}
// Adapter class
class MediaAdapter implements MediaPlayer {
AdvancedMediaPlayer advancedMusicPlayer;
public MediaAdapter(String audioType) {
if(audioType.equalsIgnoreCase("vlc")) {
advancedMusicPlayer = new VlcPlayer();
} else if(audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer = new Mp4Player();
}
}
public void play(String audioType, String fileName) {
if(audioType.equalsIgnoreCase("vlc")) {
advancedMusicPlayer.playVlc(fileName);
} else if(audioType.equalsIgnoreCase("mp4")) {
advancedMusicPlayer.playMp4(fileName);
}
}
}
// Client code
public class AdapterPatternDemo {
public static void main(String[] args) {
MediaPlayer audioPlayer = new MediaAdapter("vlc");
audioPlayer.play("vlc", "song.vlc");
audioPlayer = new MediaAdapter("mp4");
audioPlayer.play("mp4", "song.mp4");
}
}
Discussion:
Have you used the Adapter Pattern to integrate third-party libraries or legacy systems? What were the challenges you faced, and how did the Adapter Pattern help solve them? Share your experiences in the comments below!
?? Call to Action: If you found this post helpful, don't forget to like, share, and comment! Follow #ehadjistratis for more insights into Java Design Patterns and other tech topics. Let's build a community of learners and practitioners together!
Looking forward to your insights!
Stay tuned for tomorrow's topic: Bridge Pattern.
#ehadjistratis #Java #DesignPatterns #AdapterPattern #Programming #Coding #SoftwareDevelopment #LearningJourney #JuniorDevelopers #TechCommunity