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

Requirements

What to ask, and the promises of the target interface.

The point: the target interface is a list of promises. Find them before you look at the libraries, or the libraries will write them for you.

Ask

Questions that change the design

QuestionAnswer hereWhat it decides
Can the libraries be changed?NoAdapter, not rename
Which unit does the user see?SecondsThe target uses seconds
Start in the middle?Yes, resumeA second parameter
What if a file is broken?Show one messageOne error type of ours
Two songs at once?NoOne decoder per player is enough
Can one library be replaced later?LikelyNo type of the library may leave the adapter

Promises

The contract of MediaPlayer

  1. 1

    Same input, same meaning

    start_seconds is in seconds from the beginning, for every format.

  2. 2

    All or nothing

    Either the file plays, or PlaybackError. Never an old file, never silence.

  3. 3

    One error family

    No number, no exception of a library passes the border.

  4. 4

    No leftovers

    A call does not depend on the call before it.

Gap list

Compare the promises with each library

PromiseVlcEngineMp4Decoder
Nameplay_vlcload, seek, run
Unitmillisecondsmilliseconds
Errornone that we know ofreturn code of load
Statenonekeeps the loaded file
Adapter mustrename, times 1000order the calls, check the code, times 1000
Fill in this table for every adaptee before writing code. Each row becomes a line in the adapter and a test.