The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, a client (such as MediaPlayer) can play media in advanced formats (such as VLC), although it itself only supports common media formats (such as MP3).
The wonderful use of the adapter pattern in Java design patterns
Introduction
Adapter pattern Is a structural design pattern that allows one object to work with another object that has an incompatible interface. In other words, it converts one interface into another, allowing two otherwise incompatible objects to interact smoothly.
Structure
The structure of the adapter pattern is as shown below:
+--------------------+ | Target | +--------------------+ ^ | +--------------------+ | Adaptee(被适配) | +--------------------+ ^ | +----------------------------+ | Object Adapter (对象适配器) | +----------------------------+ | | \_______________/
Object Adapter
Object adapter is a simple way to implement the adapter pattern. It creates a new adapter object that contains the Adaptee object and implements the Target interface.
// Target 接口 interface Target { int operation(); } // Adaptee 接口 interface Adaptee { int specificOperation(); } // Adapter (对象适配器) class Adapter implements Target { private final Adaptee adaptee; public Adapter(Adaptee adaptee) { this.adaptee = adaptee; } @Override public int operation() { return adaptee.specificOperation(); } }
Practical case
Suppose we have two classes: MediaPlayer and AdvancedMediaPlayer. The MediaPlayer class is responsible for playing media in common formats (such as MP3), while the AdvancedMediaPlayer class is responsible for playing media in advanced formats (such as VLC). We are going to use the adapter pattern to enable the MediaPlayer class to play advanced format media.
class MediaPlayer { public void playMP3(String filename) { System.out.println("Playing MP3: " + filename); } } class AdvancedMediaPlayer { public void playVLC(String filename) { System.out.println("Playing VLC: " + filename); } } class MediaAdapter implements MediaPlayer { private final AdvancedMediaPlayer advancedMediaPlayer; public MediaAdapter(AdvancedMediaPlayer advancedMediaPlayer) { this.advancedMediaPlayer = advancedMediaPlayer; } @Override public void playMP3(String filename) { advancedMediaPlayer.playVLC(filename); } } // 客户调用 MediaPlayer audioPlayer = new MediaAdapter(new AdvancedMediaPlayer()); audioPlayer.playMP3("FileName.vlc");
Output:
Playing VLC: FileName.vlc
By using the Adapter pattern, we extend the functionality of the MediaPlayer class to enable it to play advanced format media without modifying its source code.
The above is the detailed content of The wonderful use of the adapter pattern in Java design patterns. For more information, please follow other related articles on the PHP Chinese website!