Last active
June 7, 2020 11:29
-
-
Save DoRightt/57df763e0d2bcbfce86c36f7904308ce to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Command { | |
execute(): void; | |
undo(): void; | |
} | |
class Light { | |
room: string; | |
constructor(room: string) { | |
this.room = room; | |
} | |
on() { | |
console.log("Light on in " + this.room); | |
} | |
off() { | |
console.log("Light off in" + this.room); | |
} | |
} | |
class GarageDoor { | |
up() { | |
console.log("door opened"); | |
} | |
down() { | |
console.log("door closed"); | |
} | |
stop() {} | |
lightOn() {} | |
lightOff() {} | |
} | |
class Stereo { | |
room: string; | |
constructor(room: string) { | |
this.room = room; | |
} | |
on() { | |
console.log("Stereo on"); | |
} | |
off() { | |
console.log("Stereo closed"); | |
} | |
setCd() {} | |
setDvd() {} | |
setRadio() {} | |
setVolume(val: number) {} | |
} | |
class CeilingFan { | |
static HIGH = 3; | |
static MEDIUM = 2; | |
static LOW = 1; | |
static OFF = 0; | |
private location: string; | |
private speed: number; | |
constructor(location: string) { | |
this.location = location; | |
this.speed = CeilingFan.OFF; | |
} | |
on() { | |
console.log("Fan: ON"); | |
} | |
off() { | |
this.speed = CeilingFan.OFF; | |
console.log("Fan: OFF"); | |
} | |
high() { | |
this.speed = CeilingFan.HIGH; | |
console.log("Set high speed"); | |
} | |
medium() { | |
this.speed = CeilingFan.MEDIUM; | |
console.log("Set medium speed"); | |
} | |
low() { | |
this.speed = CeilingFan.LOW; | |
console.log("Set low speed"); | |
} | |
public getSpeed() { | |
return this.speed; | |
} | |
} | |
class NoCommand implements Command { | |
execute() {} | |
undo() {} | |
} | |
class RemoteControl { | |
onCommands: Command[] = new Array(7); | |
offCommands: Command[] = new Array(7); | |
noCommand = new NoCommand(); | |
undoCommand: Command; | |
constructor() { | |
for (let i = 0; i < 7; i++) { | |
this.onCommands[i] = this.noCommand; | |
this.offCommands[i] = this.noCommand; | |
} | |
this.undoCommand = this.noCommand; | |
} | |
setCommand(slot: number, onCommand: Command, offCommand: Command): void { | |
this.onCommands[slot] = onCommand; | |
this.offCommands[slot] = offCommand; | |
} | |
onButtonWasPushed(slot: number): void { | |
this.onCommands[slot].execute(); | |
this.undoCommand = this.onCommands[slot]; | |
} | |
offButtonWasPushed(slot: number): void { | |
this.offCommands[slot].execute(); | |
this.undoCommand = this.offCommands[slot]; | |
} | |
undoButtonWasPushed() { | |
this.undoCommand.undo(); | |
} | |
} | |
class LightOnCommand implements Command { | |
light: Light; | |
constructor(light: Light) { | |
this.light = light; | |
} | |
public execute(): void { | |
this.light.on(); | |
} | |
public undo(): void { | |
this.light.off(); | |
} | |
} | |
class LightOffCommand implements Command { | |
light: Light; | |
constructor(light: Light) { | |
this.light = light; | |
} | |
execute(): void { | |
this.light.off(); | |
} | |
public undo(): void { | |
this.light.on(); | |
} | |
} | |
class GarageDoorOpenCommand implements Command { | |
door: GarageDoor; | |
constructor(door: GarageDoor) { | |
this.door = door; | |
} | |
public execute() { | |
this.door.up(); | |
} | |
public undo(): void { | |
this.door.down(); | |
} | |
} | |
class GarageDoorCloseCommand implements Command { | |
door: GarageDoor; | |
constructor(door: GarageDoor) { | |
this.door = door; | |
} | |
public execute() { | |
this.door.down(); | |
} | |
public undo(): void { | |
this.door.up(); | |
} | |
} | |
class StereoOnWithCDCommand implements Command { | |
stereo: Stereo; | |
constructor(stereo: Stereo) { | |
this.stereo = stereo; | |
} | |
execute() { | |
this.stereo.on(); | |
this.stereo.setCd(); | |
this.stereo.setVolume(11); | |
} | |
public undo(): void { | |
this.stereo.off(); | |
} | |
} | |
class StereoOffCommand implements Command { | |
stereo: Stereo; | |
constructor(stereo: Stereo) { | |
this.stereo = stereo; | |
} | |
execute() { | |
this.stereo.off(); | |
} | |
public undo(): void { | |
this.stereo.on(); | |
this.stereo.setCd(); | |
this.stereo.setVolume(11); | |
} | |
} | |
class CeilingFanOnCommand implements Command { | |
fan: CeilingFan; | |
constructor(fan: CeilingFan) { | |
this.fan = fan; | |
} | |
execute() { | |
this.fan.on(); | |
} | |
undo() { | |
this.fan.off(); | |
} | |
} | |
class CeilingFanOffCommand implements Command { | |
fan: CeilingFan; | |
prevSpeed: number; | |
constructor(fan: CeilingFan) { | |
this.fan = fan; | |
} | |
execute() { | |
this.prevSpeed = this.fan.getSpeed(); | |
this.fan.off(); | |
} | |
undo() { | |
switch (this.prevSpeed) { | |
case CeilingFan.HIGH: | |
this.fan.high(); | |
break; | |
case CeilingFan.MEDIUM: | |
this.fan.medium(); | |
break; | |
case CeilingFan.LOW: | |
this.fan.low(); | |
break; | |
case CeilingFan.OFF: | |
this.fan.off(); | |
break; | |
} | |
} | |
} | |
class CeilingFanHighCommand implements Command { | |
fan: CeilingFan; | |
prevSpeed: number; | |
constructor(fan: CeilingFan) { | |
this.fan = fan; | |
} | |
execute() { | |
this.prevSpeed = this.fan.getSpeed(); | |
this.fan.high(); | |
} | |
undo() { | |
switch (this.prevSpeed) { | |
case CeilingFan.HIGH: | |
this.fan.high(); | |
break; | |
case CeilingFan.MEDIUM: | |
this.fan.medium(); | |
break; | |
case CeilingFan.LOW: | |
this.fan.low(); | |
break; | |
case CeilingFan.OFF: | |
this.fan.off(); | |
break; | |
} | |
} | |
} | |
class CeilingFanMediumCommand implements Command { | |
fan: CeilingFan; | |
prevSpeed: number; | |
constructor(fan: CeilingFan) { | |
this.fan = fan; | |
} | |
execute() { | |
this.prevSpeed = this.fan.getSpeed(); | |
this.fan.medium(); | |
} | |
undo() { | |
switch (this.prevSpeed) { | |
case CeilingFan.HIGH: | |
this.fan.high(); | |
break; | |
case CeilingFan.MEDIUM: | |
this.fan.medium(); | |
break; | |
case CeilingFan.LOW: | |
this.fan.low(); | |
break; | |
case CeilingFan.OFF: | |
this.fan.off(); | |
break; | |
} | |
} | |
} | |
class CeilingFanLowCommand implements Command { | |
fan: CeilingFan; | |
prevSpeed: number; | |
constructor(fan: CeilingFan) { | |
this.fan = fan; | |
} | |
execute() { | |
this.prevSpeed = this.fan.getSpeed(); | |
this.fan.low(); | |
} | |
undo() { | |
switch (this.prevSpeed) { | |
case CeilingFan.HIGH: | |
this.fan.high(); | |
break; | |
case CeilingFan.MEDIUM: | |
this.fan.medium(); | |
break; | |
case CeilingFan.LOW: | |
this.fan.low(); | |
break; | |
case CeilingFan.OFF: | |
this.fan.off(); | |
break; | |
} | |
} | |
} | |
const remoteControl = new RemoteControl(); | |
const livingRoomLight = new Light("Living room"); | |
const kitchenRoomLight = new Light("Kitchen"); | |
const ceilingFan = new CeilingFan("Living room"); | |
const garageDoor = new GarageDoor(); | |
const stereo = new Stereo("Living room"); | |
const livingRoomLightOn = new LightOnCommand(livingRoomLight); | |
const livingRoomLightOff = new LightOffCommand(livingRoomLight); | |
const kitchenLightOn = new LightOnCommand(kitchenRoomLight); | |
const kitchenLightOff = new LightOffCommand(kitchenRoomLight); | |
const ceilingFanOn = new CeilingFanOnCommand(ceilingFan); | |
const ceilingFanHigh = new CeilingFanHighCommand(ceilingFan); | |
const ceilingFanMedium = new CeilingFanMediumCommand(ceilingFan); | |
const ceilingFanLow = new CeilingFanLowCommand(ceilingFan); | |
const ceilingFanOff = new CeilingFanOffCommand(ceilingFan); | |
const garageDoorUp = new GarageDoorOpenCommand(garageDoor); | |
const garageDoorDown = new GarageDoorCloseCommand(garageDoor); | |
const stereoOnWithCd = new StereoOnWithCDCommand(stereo); | |
const stereoOff = new StereoOffCommand(stereo); | |
// Various commands | |
// remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff); | |
// remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff); | |
// remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff); | |
// remoteControl.setCommand(3, garageDoorUp, garageDoorDown); | |
// remoteControl.setCommand(4, stereoOnWithCd, stereoOff); | |
// remoteControl.onButtonWasPushed(0); | |
// remoteControl.offButtonWasPushed(0); | |
// remoteControl.onButtonWasPushed(1); | |
// remoteControl.offButtonWasPushed(1); | |
// remoteControl.onButtonWasPushed(2); | |
// remoteControl.offButtonWasPushed(2); | |
// remoteControl.onButtonWasPushed(3); | |
// remoteControl.offButtonWasPushed(3); | |
// remoteControl.onButtonWasPushed(4); | |
// remoteControl.offButtonWasPushed(4); | |
// remoteControl.undoButtonWasPushed(); | |
// Fan commands | |
remoteControl.setCommand(0, ceilingFanHigh, ceilingFanOff); | |
remoteControl.setCommand(1, ceilingFanMedium, ceilingFanOff); | |
remoteControl.setCommand(2, ceilingFanLow, ceilingFanOff); | |
remoteControl.onButtonWasPushed(1); | |
remoteControl.onButtonWasPushed(0); | |
remoteControl.undoButtonWasPushed(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment