class offFanState{
constructor(fan){
this
.fan=fan;
}
clickButton(){
console.log("Keep fan on low speed");
this
.fan.setState(
this
.fan.lowSpeedStateOfFan);
}
}
class HighSpeedFanState{
constructor(fan){
this
.fan=fan;
}
clickButton(){
console.log("Switch Off Fan");
this
.fan.setState(
this
.fan.offFanStateOfFan);
}
}
class LowSpeedFanState{
constructor(fan){
this
.fan = fan;
}
clickButton(){
console.log("Switch using state method to Medium speed");
this
.fan.setState(
this
.fan.mediumSpeedStateOfFan);
}
}
class MediumSpeedFanState{
constructor(fan){
this
.fan=fan;
}
clickButton() {
console.log("Switch using state method to High speed");
this
.fan.setState(
this
.fan.highSpeedStateOfFan);
}
}
class Fan{
constructor(){
this
.offFanState=
new
offFanState(
this
);
this
.highSpeedStateOfFan=
new
HighSpeedFanState(
this
);
this
.lowSpeedStateOfFan=
new
LowSpeedFanState(
this
);
this
.mediumSpeedStateOfFan=
new
MediumSpeedFanState(
this
);
this
.presentState=
this
.offFanState;
}
setState(presentState){
this
.presentState=presentState;
}
clickButton(){
this
.presentState.clickButton();
}
}
let fanStates=
new
Fan();
fanStates.clickButton();
fanStates.clickButton();
fanStates.clickButton();
fanStates.clickButton();