Skip to content

Instantly share code, notes, and snippets.

@rxluz
Last active January 18, 2019 02:44
Show Gist options
  • Save rxluz/f9d90860e1c64f928887e29032021883 to your computer and use it in GitHub Desktop.
Save rxluz/f9d90860e1c64f928887e29032021883 to your computer and use it in GitHub Desktop.
JS Design Patterns: State, see more at: https://medium.com/p/4a8e1f30b62d
/* WITHOUT STATE PATTERN EXAMPLE */
class TVRemoteWithoutStatePattern {
constructor() {
this.channel = 1;
}
setChannel(channel) {
this.channel = channel;
}
watch() {
if (this.channel === 1) {
console.log("Hello, watch sports on channel 1");
}
if (this.channel === 2) {
console.log("Hello, watch a serie on channel 2");
}
if (this.channel === 3) {
console.log("Hello, watch the daily news on channel 3");
}
}
}
const TVRemoteInstance0 = new TVRemoteWithoutStatePattern();
TVRemoteInstance0.setChannel(1);
TVRemoteInstance0.watch();
TVRemoteInstance0.setChannel(2);
TVRemoteInstance0.watch();
TVRemoteInstance0.setChannel(3);
TVRemoteInstance0.watch();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment