Skip to content

Instantly share code, notes, and snippets.

@stemmlerjs
Last active April 6, 2019 17:13
Show Gist options
  • Select an option

  • Save stemmlerjs/f12d8ee16f8cec62087b1d30be7d7c7e to your computer and use it in GitHub Desktop.

Select an option

Save stemmlerjs/f12d8ee16f8cec62087b1d30be7d7c7e to your computer and use it in GitHub Desktop.
class AudioDevice {
constructor () {
this.isPlaying = false;
this.currentTrack = null;
}
play (track) {
this.currentTrack = track;
this.isPlaying = true;
this.handlePlayCurrentAudioTrack();
}
handlePlayCurrentAudioTrack () {
throw new Error(`Subclasss responsibility error`)
}
}
class Boombox extends AudioDevice {
constructor () {
super()
}
handlePlayCurrentAudioTrack () {
// Play through the boombox speakers
}
}
class IPod extends AudioDevice {
constructor () {
super()
}
handlePlayCurrentAudioTrack () {
// Ensure headphones are plugged in
// Play through the ipod
}
}
const AudioDeviceType = {
Boombox: 'Boombox',
IPod: 'Ipod'
}
const AudioDeviceFactory = {
create: (deviceType) => {
switch (deviceType) {
case AudioDeviceType.Boombox:
return new Boombox();
case AudioDeviceType.IPod:
return new IPod();
default:
return null;
}
}
}
const boombox = AudioDeviceFactory
.create(AudioDeviceType.Boombox);
const ipod = AudioDeviceFactory
.create(AudioDeviceType.IPod);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment