AlgoViz

Adapter pattern

easy 50 min

The Adapter pattern makes an existing class fit the interface that its client expects, without changing either of them.

Watch lesson4:39

UML design

Which classes, who knows whom, and how a call travels.

The point: draw the border. On the left only our words, on the right only theirs. The adapters are the only classes that touch both.

Audio player with adapters

one per formatAudioPlayer- players: Map+ open(file, start)«interface»MediaPlayer+ play(file, start)

1/5What the player wants

One interface, one method. AudioPlayer holds a map from extension to MediaPlayer.

has (lives on)implements

In this version the player holds every adapter from the start. Creating them on first use is possible too, then the map holds factories.

Decide

Is this a class at all?

CandidateClass?Why
MediaPlayerinterfaceThe player must not care who plays
VlcAdapter, Mp4AdapteryesEach has its own gap list
One common MediaAdapternoIt would change for two reasons
Format (enum)noA text key in a map is enough, and it can grow
PlayerFactorynot yetThe map is filled in one place. With plug-ins, yes: Factory
AdvancedMediaPlayer with playVlc and playMp4noA VLC player would need an empty playMp4, see interface segregation

Open a VLC film at 90 seconds

The player decides which object, the adapter decides which words.