๐ What is the Adapter Pattern?
The Adapter Pattern allows incompatible classes to work together by converting the interface of one class into an interface expected by the client.
✅ When Should You Use It?
- When you want to reuse an existing class but its interface doesn’t match what your code expects.
- When you want to create a bridge between legacy code and modern systems.
- When you want to avoid modifying source code of existing (3rd-party or legacy) classes.
๐ง Real-World Analogy
Imagine you’re traveling from India to the USA, and you want to plug in your charger ๐ชซ. But the sockets don’t match — so you use a socket adapter. It converts the plug type so your charger can still work!
๐งฑ Structure
+--------------------+ +----------------------+
| Client |----->| Target |
+--------------------+ +----------------------+
^
|
+-------------------+
| Adapter |
+-------------------+
|
v
+---------------+
| Adaptee |
+---------------+
๐ก Example: Media Player Adapter
Let’s say you have a MediaPlayer
interface that supports .mp3
files, but now you want to add support for .vlc
, .mp4
using an AdvancedMediaPlayer
. The interfaces don’t match — so we’ll use an Adapter.
✅ 1. Target Interface
public interface MediaPlayer {
void play(String audioType, String fileName);
}
✅ 2. Adaptee Class (Incompatible Interface)
public interface AdvancedMediaPlayer {
void playVlc(String fileName);
void playMp4(String fileName);
}
public class VlcPlayer implements AdvancedMediaPlayer {
public void playVlc(String fileName) {
System.out.println("Playing vlc file: " + fileName);
}
public void playMp4(String fileName) {
// Do nothing
}
}
public class Mp4Player implements AdvancedMediaPlayer {
public void playMp4(String fileName) {
System.out.println("Playing mp4 file: " + fileName);
}
public void playVlc(String fileName) {
// Do nothing
}
}
✅ 3. Adapter Class
public class MediaAdapter implements MediaPlayer {
AdvancedMediaPlayer advancedMediaPlayer;
public MediaAdapter(String audioType) {
if (audioType.equalsIgnoreCase("vlc")) {
advancedMediaPlayer = new VlcPlayer();
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMediaPlayer = new Mp4Player();
}
}
public void play(String audioType, String fileName) {
if (audioType.equalsIgnoreCase("vlc")) {
advancedMediaPlayer.playVlc(fileName);
} else if (audioType.equalsIgnoreCase("mp4")) {
advancedMediaPlayer.playMp4(fileName);
}
}
}
✅ 4. Concrete Target Class (Using the Adapter)
public class AudioPlayer implements MediaPlayer {
MediaAdapter mediaAdapter;
public void play(String audioType, String fileName) {
if (audioType.equalsIgnoreCase("mp3")) {
System.out.println("Playing mp3 file: " + fileName);
} else if (audioType.equalsIgnoreCase("vlc") || audioType.equalsIgnoreCase("mp4")) {
mediaAdapter = new MediaAdapter(audioType);
mediaAdapter.play(audioType, fileName);
} else {
System.out.println("Invalid media format: " + audioType + " not supported.");
}
}
}
๐ป Client Code
public class AdapterDemo {
public static void main(String[] args) {
AudioPlayer audioPlayer = new AudioPlayer();
audioPlayer.play("mp3", "song.mp3");
audioPlayer.play("mp4", "movie.mp4");
audioPlayer.play("vlc", "concert.vlc");
audioPlayer.play("avi", "series.avi");
}
}
๐งช Output
Playing mp3 file: song.mp3
Playing mp4 file: movie.mp4
Playing vlc file: concert.vlc
Invalid media format: avi not supported.
๐ฏ Benefits
✅ Promotes code reusability
✅ Acts as a bridge between legacy and new systems
✅ Adds flexibility without modifying existing classes
✅ Helps follow the Single Responsibility Principle
๐ Java Libraries Using Adapter
java.util.Arrays#asList()
— wraps an array into a Listjavax.xml.bind.annotation.adapters.XmlAdapter
— to map between XML and Java objects
๐ง Summary Table
Aspect | Description |
---|---|
Pattern Type | Structural |
Problem Solved | Interface incompatibility |
Key Benefit | Reusability & flexibility |
Common Use Cases | Integrating with 3rd party libraries, supporting legacy systems |
๐บ️ UML Diagram (Text Format)
+--------------------+
| MediaPlayer | <---------+
+--------------------+ |
| +play(type, name) | |
+--------------------+ |
^ |
| |
+----------------+ +---------------------+
| AudioPlayer | | MediaAdapter |
+----------------+ +---------------------+
| -AdvancedMediaPlayer |
| +play(...) |
+---------------------+
|
+--------------------------+
| VlcPlayer Mp4Player |
+--------------------------+
๐ก Pro Tip: The Adapter pattern is often used when integrating with third-party APIs whose interface you cannot change.
๐ Up Next for Day 7: Let’s keep the streak going! Want to explore:
- Facade
- Chain of Responsibility
- State
Let me know what you’re feeling next, and I’ll brew the next blog for you!
Tidak ada komentar:
Posting Komentar