Adapter Design Pattern
The Adapter Design Pattern is a structural design pattern that allows objects with incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces by providing a wrapper, or an adapter, that translates one interface into another. This enables objects that couldn't otherwise collaborate to work together seamlessly.
Let's break down the concept using a simple example in Swift:
Scenario: Imagine you're building a music player application that can play different types of audio files. You have an existing AudioPlayer class that works with a specific audio file format, let's say MP3. However, you want to extend your application to also play WAV audio files. Instead of modifying the existing AudioPlayer class, you can use the Adapter Design Pattern to integrate the new WAV format seamlessly.
Here's how you can do it:
- Define the Existing Audio Player Class (AudioPlayer):
class AudioPlayer {
func playMP3(file: String) {
print("Playing MP3 file: \(file)")
}
}
- Define the Target Interface (AudioPlayerAdapter):
This is the interface that the adapter will implement, which allows the AudioPlayer to work with both MP3 and WAV files.
protocol AudioPlayerAdapter {
func play(file: String)
}
- Create the Adapter (WAVAdapter):
The adapter implements the target interface and wraps around the AudioPlayer to make it compatible with WAV files.
领英推è
class WAVAdapter: AudioPlayerAdapter {
private let audioPlayer: AudioPlayer
init(audioPlayer: AudioPlayer) {
self.audioPlayer = audioPlayer
}
func play(file: String) {
// Convert WAV to MP3 (for illustration purposes)
let mp3File = convertWAVtoMP3(file: file)
audioPlayer.playMP3(file: mp3File)
}
private func convertWAVtoMP3(file: String) -> String {
print("Converting WAV to MP3: \(file)")
// Conversion logic would go here
return "\(file).mp3"
}
}
- Using the Adapter:
Now you can use the WAVAdapter to play WAV files using the existing AudioPlayer.
let audioPlayer = AudioPlayer()
let wavAdapter = WAVAdapter(audioPlayer: audioPlayer)
wavAdapter.play(file: "song.wav")
In this example, the WAVAdapter acts as a bridge between the AudioPlayer class and the new WAV file format. It converts the WAV file to an MP3 file (for illustration purposes) using the conversion logic inside the adapter before passing it to the original AudioPlayer for playback.
By using the Adapter Design Pattern, you've managed to integrate the new WAV format into your existing application without modifying the core AudioPlayer class. This helps maintain code integrity and separation of concerns.
Sharing my TCA expertise to empower your development ?? | ?????? with TCA in apps with 100,000+ monthly active users | iOS Software Engineer @ AVIV
1 å¹´The Adapter Design Pattern is a lifesaver when it comes to bridging the gap between incompatible interfaces, making them work together seamlessly. It's like a universal translator for software components! ??????
iOS, macOS Engineering
1 å¹´It also called as wrapper in old days?