Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Adapter Pattern

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:

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

Implementation

Key code

The adapter inherits from or depends on the existing object and implements the required target interface.

Application Example

Advantages

Disadvantages

Usage recommendation

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:

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