Adapter Pattern
The Adapter Pattern acts as a bridge between two incompatible interfaces, belonging to the structural design pattern category. It converts the interface of a class into another interface expected by the client through an intermediate piece (the adapter), enabling classes that originally could not work together to cooperate.
This pattern involves a single class that is responsible for adding independent or incompatible interface functionality. As a real example, a card reader acts as an adapter between a memory card and a laptop. You insert the memory card into the card reader, then insert the card reader into the laptop, so that the memory card can be read through the laptop.
Suppose there is an audio player that can only play MP3 files. Now, we need to play VLC and MP4 files, which can be achieved by creating an adapter:
- Target interface: define an audio player interface that can play files in multiple formats.
- Adaptee class: the existing audio player, which can only play MP3 files.
- Adapter class: create a new class that implements the target interface and internally uses the adaptee class to play MP3 files, while adding support for VLC and MP4 files.
Overview
The adapter pattern is a software design pattern aimed at solving compatibility problems between different interfaces.
Purpose: convert the interface of a class into another interface, so that originally incompatible classes can work together.
Main problem solved: in a software system, existing objects need to be placed in a new environment, but the interface required by the new environment does not match the existing objects.
Use Cases
- Need to use an existing class, but its interface does not meet the system's requirements.
- Want to create a reusable class that works with multiple unrelated classes (including classes that may be introduced in the future), which may not have a unified interface.
- Through interface conversion, integrate one class into another class system.
Implementation
- Inheritance or dependency: dependency is recommended rather than inheritance, to maintain flexibility.
Key code
The adapter inherits from or depends on the existing object and implements the required target interface.
Application Example
- Voltage adapter: convert 110V voltage to 220V to suit electrical appliance standards in different countries.
- Interface conversion: for example, convert the Enumeration interface of Java JDK 1.1 to the Iterator interface of 1.2.
- Cross-platform: run Windows programs on Linux.
- Database connection: JDBC in Java interacts with different types of databases through the adapter pattern.
Advantages
- Promotes cooperation between classes, even if they have no direct association.
- Improves class reusability.
- Increases class transparency.
- Provides good flexibility.
Disadvantages
- Overusing adapters can lead to a chaotic system structure, difficult to understand and maintain.
- In Java, since you can only inherit from one class, you can only adapt one class, and the target class must be abstract.
Usage recommendation
- The adapter pattern should be used with caution, especially in the detailed design phase; it is more used to solve problems in existing systems.
- When considering modifying the interface of a normally functioning system, the adapter pattern is a suitable choice.
In this way, the adapter pattern can clearly express its core concepts and applications while avoiding unnecessary complexity.
structures
The adapter pattern contains the following main roles:
- Target interface (Target): define the interface required by the client.
- Adaptee class (Adaptee): define an existing interface that needs to be adapted.
- Adapter class (Adapter): implement the target interface and call methods in the adaptee class through composition or inheritance, thereby implementing the target interface.
Create a class that implements
We have an MediaPlayer interface and a concrete class that implements the MediaPlayer interface AudioPlayer. By default,AudioPlayer can play audio files in mp3 format.
We also have another interface AdvancedMediaPlayer and a concrete class that implements the AdvancedMediaPlayer interface. This class can play files in vlc and mp4 formats.
We want to make AudioPlayer play audio files in other formats. To achieve this, we need to create an adapter class that implements the MediaPlayer interface MediaAdapter, and use AdvancedMediaPlayer object to play the desired format.
AudioPlayer Use the adapter class MediaAdapter passing the required audio type, without needing to know the actual class that can play the desired format.AdapterPatternDemo Class uses AudioPlayer class to play various formats.
Step 1
Create interfaces for the media player and the more advanced media player.
MediaPlayer.java
public
interface
MediaPlayer
{
public
void
play
(
String
audioType
,
String
fileName
)
;
}
AdvancedMediaPlayer.java
public
interface
AdvancedMediaPlayer
{
public
void
playVlc
(
String
fileName
)
;
public
void
playMp4
(
String
fileName
)
;
}
Step 2
Create concrete classes that implement the AdvancedMediaPlayer interface.
VlcPlayer.java
public
class
VlcPlayer
implements
AdvancedMediaPlayer
{
@
Override
public
void
playVlc
(
String
fileName
)
{
System
.
out
.
println
(
"
Playing vlc file. Name:
"
fileName
)
;
}
@
Override
public
void
playMp4
(
String
fileName
)
{
//
Do nothing
}
}
Mp4Player.java
public
class
Mp4Player
implements
AdvancedMediaPlayer
{
@
Override
public
void
playVlc
(
String
fileName
)
{
//
Do nothing
}
@
Override
public
void
playMp4
(
String
fileName
)
{
System
.
out
.
println
(
"
Playing mp4 file. Name:
"
fileName
)
;
}
}
Step 3
Create concrete classes that implement the MediaPlayer adapter class of the interface.
MediaAdapter.java
public
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
(
)
;
}
}
@
Override
public
void
play
(
String
audioType
,
String
fileName
)
{
if
(
audioType
.
equalsIgnoreCase
(
"
vlc
"
)
)
{
advancedMusicPlayer
.
playVlc
(
fileName
)
;
}
else
if
(
audioType
.
equalsIgnoreCase
(
"
mp4
"
)
)
{
advancedMusicPlayer
.
playMp4
(
fileName
)
;
}
}
}
Step 4
Create concrete classes that implement the MediaPlayer interface.
AudioPlayer.java
public
class
AudioPlayer
implements
MediaPlayer
{
MediaAdapter
mediaAdapter
; @
Override
public
void
play
(
String
audioType
,
String
fileName
)
{
//
Built-in support for playing mp3 music files
if
(
audioType
.
equalsIgnoreCase
(
"
mp3
"
)
)
{
System
.
out
.
println
(
"
Playing mp3 file. Name:
"
fileName
)
;
}
//
mediaAdapter provides support for playing other file formats
else
if
(
audioType
.
equalsIgnoreCase
(
"
vlc
"
)
||
audioType
.
equalsIgnoreCase
(
"
mp4
"
)
)
{
mediaAdapter
=
new
MediaAdapter
(
audioType
)
;
mediaAdapter
.
play
(
audioType
,
fileName
)
;
}
else
{
System
.
out
.
println
(
"
Invalid media.
"
audioType
"
format not supported
"
)
;
}
}
}
Step 5
Use AudioPlayer to play different types of audio formats.
AdapterPatternDemo.java
public
class
AdapterPatternDemo
{
public
static
void
main
(
String
[
]
args
)
{
AudioPlayer
audioPlayer
=
new
AudioPlayer
(
)
;
audioPlayer
.
play
(
"
mp3
"
,
"
beyond the horizon.mp3
"
)
;
audioPlayer
.
play
(
"
mp4
"
,
"
alone.mp4
"
)
;
audioPlayer
.
play
(
"
vlc
"
,
"
far far away.vlc
"
)
;
audioPlayer
.
play
(
"
avi
"
,
"
mind me.avi
"
)
;
}
}
Step 6
Run the program, output result:
Playing mp3 file. Name: beyond the horizon.mp3
Playing mp4 file. Name: alone.mp4
Playing vlc file. Name: far far away.vlc
Invalid media. avi format not supported