Adapter pattern
easy 50 minThe 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
1/5What the player wants
One interface, one method. AudioPlayer holds a map from extension to MediaPlayer.
has (lives on)implements
Decide
Is this a class at all?
| Candidate | Class? | Why |
|---|---|---|
| MediaPlayer | interface | The player must not care who plays |
| VlcAdapter, Mp4Adapter | yes | Each has its own gap list |
| One common MediaAdapter | no | It would change for two reasons |
| Format (enum) | no | A text key in a map is enough, and it can grow |
| PlayerFactory | not yet | The map is filled in one place. With plug-ins, yes: Factory |
| AdvancedMediaPlayer with playVlc and playMp4 | no | A VLC player would need an empty playMp4, see interface segregation |
Open a VLC film at 90 seconds