remote macrocommand

This commit is contained in:
Alexander Zhirov 2022-11-15 23:25:25 +03:00
parent 94690db1ef
commit ecfd117aba
17 changed files with 517 additions and 0 deletions

View File

@ -0,0 +1,54 @@
module command.remoteundostatusmacro.app;
import command.remoteundostatusmacro.remotecontrol;
import command.remoteundostatusmacro.lightoncommand;
import command.remoteundostatusmacro.lightoffcommand;
import command.remoteundostatusmacro.light;
import command.remoteundostatusmacro.tv;
import command.remoteundostatusmacro.tvoffcommand;
import command.remoteundostatusmacro.tvoncommand;
import command.remoteundostatusmacro.hottub;
import command.remoteundostatusmacro.hottuboffcommand;
import command.remoteundostatusmacro.hottuboncommand;
import command.remoteundostatusmacro.stereoonwithcdcommand;
import command.remoteundostatusmacro.stereo;
import command.remoteundostatusmacro.stereooffcommand;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.macrocommand;
import std.stdio : writeln;
void main()
{
auto remoteControl = new RemoteControl();
auto light = new Light("Living Room");
auto tv = new TV("Living Room");
auto stereo = new Stereo("Living Room");
auto hottub = new Hottub();
Command[] partyOn = cast(Command[])[
new LightOnCommand(light),
new StereoOnWithCDCommand(stereo),
new TVOnCommand(tv),
new HottubOnCommand(hottub)
];
Command[] partyOff = cast(Command[])[
new LightOffCommand(light),
new StereoOffCommand(stereo),
new TVOffCommand(tv),
new HottubOffCommand(hottub)
];
remoteControl.setCommand(0, new MacroCommand(partyOn), new MacroCommand(partyOff));
writeln(remoteControl);
writeln("--- Pushing Macro On ---");
remoteControl.onButtonWasPushed(0);
writeln("--- Pushing Macro Off ---");
remoteControl.offButtonWasPushed(0);
writeln(remoteControl);
remoteControl.undoButtonWasPushed();
writeln(remoteControl);
remoteControl.onButtonWasPushed(0);
writeln(remoteControl);
}

View File

@ -0,0 +1,7 @@
module command.remoteundostatusmacro.command;
interface Command
{
void execute();
void undo();
}

View File

@ -0,0 +1,58 @@
module command.remoteundostatusmacro.hottub;
import std.stdio : writeln;
import std.conv : to;
class Hottub
{
private bool isOn;
private int temperature;
void on()
{
isOn = true;
}
void off()
{
isOn = false;
}
void circulate()
{
if (isOn)
{
writeln("Hottub is bubbling!");
}
}
void jetsOn()
{
if (isOn)
{
writeln("Hottub jets are on");
}
}
void jetsOff()
{
if (isOn)
{
writeln("Hottub jets are off");
}
}
void setTemperature(int temperature)
{
if (temperature > this.temperature)
{
writeln("Hottub is heating to a steaming " ~ temperature.to!string ~ " degrees");
}
else
{
writeln("Hottub is cooling to " ~ temperature.to!string ~ " degrees");
}
this.temperature = temperature;
}
}

View File

@ -0,0 +1,25 @@
module command.remoteundostatusmacro.hottuboffcommand;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.hottub;
class HottubOffCommand : Command
{
Hottub hottub;
this(Hottub hottub)
{
this.hottub = hottub;
}
void execute()
{
hottub.setTemperature(98);
hottub.off();
}
void undo()
{
hottub.on();
}
}

View File

@ -0,0 +1,26 @@
module command.remoteundostatusmacro.hottuboncommand;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.hottub;
class HottubOnCommand : Command
{
Hottub hottub;
this(Hottub hottub)
{
this.hottub = hottub;
}
void execute()
{
hottub.on();
hottub.setTemperature(104);
hottub.circulate();
}
void undo()
{
hottub.off();
}
}

View File

@ -0,0 +1,23 @@
module command.remoteundostatusmacro.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");
}
}

View File

@ -0,0 +1,24 @@
module command.remoteundostatusmacro.lightoffcommand;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.light;
class LightOffCommand : Command
{
Light light;
this(Light light)
{
this.light = light;
}
void execute()
{
light.off();
}
void undo()
{
light.on();
}
}

View File

@ -0,0 +1,24 @@
module command.remoteundostatusmacro.lightoncommand;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.light;
class LightOnCommand : Command
{
Light light;
this(Light light)
{
this.light = light;
}
void execute()
{
light.on();
}
void undo()
{
light.off();
}
}

View File

@ -0,0 +1,29 @@
module command.remoteundostatusmacro.macrocommand;
import command.remoteundostatusmacro.command;
class MacroCommand : Command
{
Command[] commands;
this(Command[] commands)
{
this.commands = commands;
}
void execute()
{
foreach (val; commands)
{
val.execute();
}
}
void undo()
{
foreach (val; commands)
{
val.undo();
}
}
}

View File

@ -0,0 +1,9 @@
module command.remoteundostatusmacro.nocommand;
import command.remoteundostatusmacro.command;
class NoCommand : Command
{
void execute() {}
void undo() {}
}

View File

@ -0,0 +1,63 @@
module command.remoteundostatusmacro.remotecontrol;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.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";
}
}

View File

@ -0,0 +1,44 @@
module command.remoteundostatusmacro.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);
}
}

View File

@ -0,0 +1,26 @@
module command.remoteundostatusmacro.stereooffcommand;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.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);
}
}

View File

@ -0,0 +1,26 @@
module command.remoteundostatusmacro.stereoonwithcdcommand;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.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();
}
}

View File

@ -0,0 +1,30 @@
module command.remoteundostatusmacro.tv;
import std.stdio : writeln;
class TV
{
private string location;
private int channel;
this(string location)
{
this.location = location;
}
void on()
{
writeln(location ~ " TV is on");
}
void off()
{
writeln(location ~ " TV is off");
}
void setInputChannel()
{
this.channel = 3;
writeln(location ~ " TV channel is set for DVD");
}
}

View File

@ -0,0 +1,24 @@
module command.remoteundostatusmacro.tvoffcommand;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.tv;
class TVOffCommand : Command
{
TV tv;
this(TV tv)
{
this.tv = tv;
}
void execute()
{
tv.off();
}
void undo()
{
tv.on();
}
}

View File

@ -0,0 +1,25 @@
module command.remoteundostatusmacro.tvoncommand;
import command.remoteundostatusmacro.command;
import command.remoteundostatusmacro.tv;
class TVOnCommand : Command
{
TV tv;
this(TV tv)
{
this.tv = tv;
}
void execute()
{
tv.on();
tv.setInputChannel();
}
void undo()
{
tv.off();
}
}