remote undo status
This commit is contained in:
parent
949451a1f1
commit
94690db1ef
|
@ -0,0 +1,36 @@
|
|||
module command.remoteundostatus.app;
|
||||
|
||||
import command.remoteundostatus.remotecontrol;
|
||||
import command.remoteundostatus.lightoncommand;
|
||||
import command.remoteundostatus.lightoffcommand;
|
||||
import command.remoteundostatus.light;
|
||||
import command.remoteundostatus.ceilingfan;
|
||||
import command.remoteundostatus.ceilingfanoffcommand;
|
||||
import command.remoteundostatus.ceilingfanhighcommand;
|
||||
import command.remoteundostatus.ceilingfanmediumcommand;
|
||||
import command.remoteundostatus.ceilingfanlowcommand;
|
||||
import command.remoteundostatus.garagedoorupcommand;
|
||||
import command.remoteundostatus.garagedoordowncommand;
|
||||
import command.remoteundostatus.garagedoor;
|
||||
import command.remoteundostatus.stereoonwithcdcommand;
|
||||
import command.remoteundostatus.stereo;
|
||||
import command.remoteundostatus.stereooffcommand;
|
||||
import std.stdio : writeln;
|
||||
|
||||
void main()
|
||||
{
|
||||
auto remoteControl = new RemoteControl();
|
||||
|
||||
auto ceilingFan = new CeilingFan("Living Room");
|
||||
|
||||
remoteControl.setCommand(0, new CeilingFanMediumCommand(ceilingFan), new CeilingFanOffCommand(ceilingFan));
|
||||
remoteControl.setCommand(1, new CeilingFanHighCommand(ceilingFan), new CeilingFanOffCommand(ceilingFan));
|
||||
|
||||
remoteControl.onButtonWasPushed(0);
|
||||
remoteControl.offButtonWasPushed(0);
|
||||
writeln(remoteControl);
|
||||
remoteControl.undoButtonWasPushed();
|
||||
remoteControl.onButtonWasPushed(1);
|
||||
writeln(remoteControl);
|
||||
remoteControl.undoButtonWasPushed();
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
module command.remoteundostatus.ceilingfan;
|
||||
|
||||
import std.stdio : writeln;
|
||||
|
||||
class CeilingFan
|
||||
{
|
||||
private string location;
|
||||
private int speed;
|
||||
|
||||
static const int HIGH = 3;
|
||||
static const int MEDIUM = 2;
|
||||
static const int LOW = 1;
|
||||
static const int OFF = 0;
|
||||
|
||||
this(string location)
|
||||
{
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
void high()
|
||||
{
|
||||
speed = HIGH;
|
||||
writeln(location ~ " ceiling fan is on high");
|
||||
|
||||
}
|
||||
|
||||
void medium()
|
||||
{
|
||||
speed = MEDIUM;
|
||||
writeln(location ~ " ceiling fan is on medium");
|
||||
}
|
||||
|
||||
void low()
|
||||
{
|
||||
speed = LOW;
|
||||
writeln(location ~ " ceiling fan is on low");
|
||||
}
|
||||
|
||||
void off()
|
||||
{
|
||||
speed = OFF;
|
||||
writeln(location ~ " ceiling fan is off");
|
||||
}
|
||||
|
||||
int getSpeed()
|
||||
{
|
||||
return speed;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
module command.remoteundostatus.ceilingfanhighcommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.ceilingfan;
|
||||
|
||||
class CeilingFanHighCommand : Command
|
||||
{
|
||||
CeilingFan ceilingFan;
|
||||
int prevSpeed;
|
||||
|
||||
this(CeilingFan ceilingFan)
|
||||
{
|
||||
this.ceilingFan = ceilingFan;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
prevSpeed = ceilingFan.getSpeed();
|
||||
ceilingFan.high();
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
switch (prevSpeed)
|
||||
{
|
||||
case CeilingFan.HIGH:
|
||||
ceilingFan.high();
|
||||
break;
|
||||
case CeilingFan.MEDIUM:
|
||||
ceilingFan.medium();
|
||||
break;
|
||||
case CeilingFan.LOW:
|
||||
ceilingFan.low();
|
||||
break;
|
||||
case CeilingFan.OFF:
|
||||
ceilingFan.off();
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
module command.remoteundostatus.ceilingfanlowcommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.ceilingfan;
|
||||
|
||||
class CeilingFanLowCommand : Command
|
||||
{
|
||||
CeilingFan ceilingFan;
|
||||
int prevSpeed;
|
||||
|
||||
this(CeilingFan ceilingFan)
|
||||
{
|
||||
this.ceilingFan = ceilingFan;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
prevSpeed = ceilingFan.getSpeed();
|
||||
ceilingFan.low();
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
switch (prevSpeed)
|
||||
{
|
||||
case CeilingFan.HIGH:
|
||||
ceilingFan.high();
|
||||
break;
|
||||
case CeilingFan.MEDIUM:
|
||||
ceilingFan.medium();
|
||||
break;
|
||||
case CeilingFan.LOW:
|
||||
ceilingFan.low();
|
||||
break;
|
||||
case CeilingFan.OFF:
|
||||
ceilingFan.off();
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
module command.remoteundostatus.ceilingfanmediumcommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.ceilingfan;
|
||||
|
||||
class CeilingFanMediumCommand : Command
|
||||
{
|
||||
CeilingFan ceilingFan;
|
||||
int prevSpeed;
|
||||
|
||||
this(CeilingFan ceilingFan)
|
||||
{
|
||||
this.ceilingFan = ceilingFan;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
prevSpeed = ceilingFan.getSpeed();
|
||||
ceilingFan.medium();
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
switch (prevSpeed)
|
||||
{
|
||||
case CeilingFan.HIGH:
|
||||
ceilingFan.high();
|
||||
break;
|
||||
case CeilingFan.MEDIUM:
|
||||
ceilingFan.medium();
|
||||
break;
|
||||
case CeilingFan.LOW:
|
||||
ceilingFan.low();
|
||||
break;
|
||||
case CeilingFan.OFF:
|
||||
ceilingFan.off();
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
module command.remoteundostatus.ceilingfanoffcommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.ceilingfan;
|
||||
|
||||
class CeilingFanOffCommand : Command
|
||||
{
|
||||
CeilingFan ceilingFan;
|
||||
int prevSpeed;
|
||||
|
||||
this(CeilingFan ceilingFan)
|
||||
{
|
||||
this.ceilingFan = ceilingFan;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
prevSpeed = ceilingFan.getSpeed();
|
||||
ceilingFan.off();
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
switch (prevSpeed)
|
||||
{
|
||||
case CeilingFan.HIGH:
|
||||
ceilingFan.high();
|
||||
break;
|
||||
case CeilingFan.MEDIUM:
|
||||
ceilingFan.medium();
|
||||
break;
|
||||
case CeilingFan.LOW:
|
||||
ceilingFan.low();
|
||||
break;
|
||||
case CeilingFan.OFF:
|
||||
ceilingFan.off();
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
module command.remoteundostatus.command;
|
||||
|
||||
interface Command
|
||||
{
|
||||
void execute();
|
||||
void undo();
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
module command.remoteundostatus.garagedoor;
|
||||
|
||||
import std.stdio : writeln;
|
||||
|
||||
class GarageDoor
|
||||
{
|
||||
private string location;
|
||||
|
||||
this(string location)
|
||||
{
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
void up()
|
||||
{
|
||||
writeln(location ~ " garage Door is Up");
|
||||
}
|
||||
|
||||
void down()
|
||||
{
|
||||
writeln(location ~ " garage Door is Down");
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
writeln(location ~ " garage Door is Stopped");
|
||||
}
|
||||
|
||||
void lightOn()
|
||||
{
|
||||
writeln(location ~ " garage light is on");
|
||||
}
|
||||
|
||||
void lightOff()
|
||||
{
|
||||
writeln(location ~ " garage light is off");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
module command.remoteundostatus.garagedoordowncommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.garagedoor;
|
||||
|
||||
class GarageDoorDownCommand : Command
|
||||
{
|
||||
GarageDoor garageDoor;
|
||||
|
||||
this(GarageDoor garageDoor)
|
||||
{
|
||||
this.garageDoor = garageDoor;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
garageDoor.down();
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
garageDoor.up();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
module command.remoteundostatus.garagedoorupcommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.garagedoor;
|
||||
|
||||
class GarageDoorUpCommand : Command
|
||||
{
|
||||
GarageDoor garageDoor;
|
||||
|
||||
this(GarageDoor garageDoor)
|
||||
{
|
||||
this.garageDoor = garageDoor;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
garageDoor.up();
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
garageDoor.down();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
module command.remoteundostatus.light;
|
||||
|
||||
import std.stdio : writeln;
|
||||
|
||||
class Light
|
||||
{
|
||||
private string location;
|
||||
|
||||
this(string location)
|
||||
{
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
void on()
|
||||
{
|
||||
writeln(location ~ " light is On");
|
||||
}
|
||||
|
||||
void off()
|
||||
{
|
||||
writeln(location ~ " light is Off");
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
module command.remoteundostatus.lightoffcommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.light;
|
||||
|
||||
class LightOffCommand : Command
|
||||
{
|
||||
Light light;
|
||||
|
||||
this(Light light)
|
||||
{
|
||||
this.light = light;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
light.off();
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
light.on();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
module command.remoteundostatus.lightoncommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.light;
|
||||
|
||||
class LightOnCommand : Command
|
||||
{
|
||||
Light light;
|
||||
|
||||
this(Light light)
|
||||
{
|
||||
this.light = light;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
light.on();
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
light.off();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
module command.remoteundostatus.nocommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
|
||||
class NoCommand : Command
|
||||
{
|
||||
void execute() {}
|
||||
void undo() {}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
module command.remoteundostatus.remotecontrol;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.nocommand;
|
||||
import std.conv : to;
|
||||
import std.stdio : writeln;
|
||||
import std.algorithm.mutation : fill;
|
||||
import std.array : split, back;
|
||||
import std.format;
|
||||
|
||||
class RemoteControl
|
||||
{
|
||||
Command[] onCommands;
|
||||
Command[] offCommands;
|
||||
Command undoCommand;
|
||||
|
||||
this()
|
||||
{
|
||||
onCommands = new Command[7];
|
||||
offCommands = new Command[7];
|
||||
Command noCommand = new NoCommand();
|
||||
fill(onCommands, noCommand);
|
||||
fill(offCommands, noCommand);
|
||||
undoCommand = noCommand;
|
||||
}
|
||||
|
||||
void setCommand(int slot, Command onCommand, Command offCommand)
|
||||
{
|
||||
onCommands[slot] = onCommand;
|
||||
offCommands[slot] = offCommand;
|
||||
}
|
||||
|
||||
void onButtonWasPushed(int slot)
|
||||
{
|
||||
onCommands[slot].execute();
|
||||
undoCommand = onCommands[slot];
|
||||
}
|
||||
|
||||
void offButtonWasPushed(int slot)
|
||||
{
|
||||
offCommands[slot].execute();
|
||||
undoCommand = offCommands[slot];
|
||||
}
|
||||
|
||||
void undoButtonWasPushed()
|
||||
{
|
||||
undoCommand.undo();
|
||||
}
|
||||
|
||||
override string toString() const
|
||||
{
|
||||
string s = "\n------ Remote Control -------\n";
|
||||
for (int i = 0; i < 7; ++i)
|
||||
{
|
||||
s ~= "[slot " ~ i.to!string ~ "] "
|
||||
~ format("%23s", (cast(Object)onCommands[i]).classinfo.name.split(".").back())
|
||||
~ format("%23s", (cast(Object)offCommands[i]).classinfo.name.split(".").back())
|
||||
~ "\n";
|
||||
}
|
||||
|
||||
return s ~ "[undo] " ~ (cast(Object)undoCommand).classinfo.name.split(".").back() ~ "\n";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
module command.remoteundostatus.stereo;
|
||||
|
||||
import std.stdio : writeln;
|
||||
import std.conv : to;
|
||||
|
||||
class Stereo
|
||||
{
|
||||
string location;
|
||||
|
||||
this(string location)
|
||||
{
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
void on()
|
||||
{
|
||||
writeln(location ~ " stereo is on");
|
||||
}
|
||||
|
||||
void off()
|
||||
{
|
||||
writeln(location ~ " stereo is off");
|
||||
}
|
||||
|
||||
void setCD()
|
||||
{
|
||||
writeln(location ~ " stereo is set for CD input");
|
||||
}
|
||||
|
||||
void setDVD()
|
||||
{
|
||||
writeln(location ~ " stereo is set for DVD input");
|
||||
}
|
||||
|
||||
void setRadio()
|
||||
{
|
||||
writeln(location ~ " stereo is set for Radio");
|
||||
}
|
||||
|
||||
void setVolume(int volume)
|
||||
{
|
||||
writeln(location ~ " stereo volume set to " ~ volume.to!string);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
module command.remoteundostatus.stereooffcommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.stereo;
|
||||
|
||||
class StereoOffCommand : Command
|
||||
{
|
||||
Stereo stereo;
|
||||
|
||||
this(Stereo stereo)
|
||||
{
|
||||
this.stereo = stereo;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
stereo.off();
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
stereo.on();
|
||||
stereo.setCD();
|
||||
stereo.setVolume(11);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
module command.remoteundostatus.stereoonwithcdcommand;
|
||||
|
||||
import command.remoteundostatus.command;
|
||||
import command.remoteundostatus.stereo;
|
||||
|
||||
class StereoOnWithCDCommand : Command
|
||||
{
|
||||
Stereo stereo;
|
||||
|
||||
this(Stereo stereo)
|
||||
{
|
||||
this.stereo = stereo;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
stereo.on();
|
||||
stereo.setCD();
|
||||
stereo.setVolume(11);
|
||||
}
|
||||
|
||||
void undo()
|
||||
{
|
||||
stereo.off();
|
||||
}
|
||||
}
|
Reference in New Issue