Sitemap
Amit Dhiman

Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS

Adapter Design Pattern

2 min readJul 5, 2023

Commonly used design pattern

The Adapter design pattern is a structural pattern that allows incompatible classes to work together by converting the interface of one class into another interface that clients expect. It acts as a bridge between two incompatible interfaces. The Adapter pattern is useful when you want to reuse existing code without making significant changes to it.

Here’s an example of implementing the Adapter pattern in Kotlin:

// Target interface
interface MediaPlayer {
fun play(audioType: String, fileName: String)
}

// Adaptee interface
interface AdvancedMediaPlayer {
fun playVlc(fileName: String)
fun playMp4(fileName: String)
}

// Adaptee implementation
class VlcPlayer : MediaPlayer {
override fun play(fileName: String) {
println("Playing vlc file: $fileName")
}
}

// Adaptee implementation
class Mp4Player : MediaPlayer {

override fun play(fileName: String) {
println("Playing mp4 file: $fileName")
}
}

// Adapter class
class MediaAdapter(private val audioType: String) : MediaPlayer {
private val advancedMediaPlayer: MediaPlayer = when (audioType) {
"vlc" -> VlcPlayer()
"mp4" -> Mp4Player()
else -> throw IllegalArgumentException("Invalid audio type: $audioType")
}

override fun play(fileName: String) {
advancedMediaPlayer.playVlc(fileName)
}
}

// Client code
fun main() {
val mediaPlayer: MediaPlayer = MediaAdapter("vlc")
mediaPlayer.play("movie.vlc")

val mediaPlayer2: MediaPlayer = MediaAdapter("mp4")
mediaPlayer2.play("movie.mp4")
}

In this example, we have a MediaPlayer interface representing the target interface that our client expects. The MediaPlayer interface has a play method.

Get Amit Dhiman’s stories in your inbox

Join Medium for free to get updates from this writer.

We also have an AdvancedMediaPlayer interface representing the incompatible interface. It has methods playVlc and playMp4.

The VlcPlayer and Mp4Player classes are implementations of the AdvancedMediaPlayer interface.

The MediaAdapter class acts as the adapter. It implements the MediaPlayer interface and internally uses an instance of the appropriate AdvancedMediaPlayer implementation based on the audioType parameter passed to it.

The play method in the MediaAdapter class delegates the calls to the appropriate methods of the AdvancedMediaPlayer instance based on the audioType.

In the main function, we demonstrate how the adapter works. We create an instance of MediaAdapter with the appropriate audioType ("vlc" or "mp4") and call the play method on it, passing the desired audioType and the file name. The adapter internally handles the conversion and calls the appropriate methods of the AdvancedMediaPlayer.

By using the Adapter pattern, we can seamlessly use the MediaPlayer interface to play different types of audio files without modifying the existing code of the MediaPlayer interface and the AdvancedMediaPlayer implementations.

--

--

Amit Dhiman
Amit Dhiman

Written by Amit Dhiman

Sr. Software Developer, Frontend developer with 9+ years of experience. Specializing in Kotlin, Java, Flutter, Angular, Javascript, Typescript, AWS