simple remote command

This commit is contained in:
Alexander Zhirov 2022-11-14 22:45:04 +03:00
parent 91e4cafaa9
commit 3c374df63d
8 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,25 @@
module command.simpleremotecontrol.app;
import command.simpleremotecontrol.simpleremotecontrol;
import command.simpleremotecontrol.lightoncommand;
import command.simpleremotecontrol.lightoffcommand;
import command.simpleremotecontrol.light;
import command.simpleremotecontrol.garagedooropencommand;
import command.simpleremotecontrol.garagedoor;
void main()
{
auto remote = new SimpleRemoteControl();
auto light = new Light();
auto lightOn = new LightOnCommand(light);
auto lightOff = new LightOffCommand(light);
auto garageDoor = new GarageDoor();
auto garageDoorOpen = new GarageDoorOpenCommand(garageDoor);
remote.setCommand(lightOn);
remote.buttonWasPressed();
remote.setCommand(lightOff);
remote.buttonWasPressed();
remote.setCommand(garageDoorOpen);
remote.buttonWasPressed();
}

View File

@ -0,0 +1,6 @@
module command.simpleremotecontrol.command;
interface Command
{
void execute();
}

View File

@ -0,0 +1,31 @@
module command.simpleremotecontrol.garagedoor;
import std.stdio : writeln;
class GarageDoor
{
void up()
{
writeln("Garage Door is Open");
}
void down()
{
writeln("Garage Door is Closed");
}
void stop()
{
writeln("Garage Door is Stopped");
}
void lightOn()
{
writeln("Garage light is on");
}
void lightOff()
{
writeln("Garage light is off");
}
}

View File

@ -0,0 +1,19 @@
module command.simpleremotecontrol.garagedooropencommand;
import command.simpleremotecontrol.command;
import command.simpleremotecontrol.garagedoor;
class GarageDoorOpenCommand : Command
{
GarageDoor garageDoor;
this(GarageDoor garageDoor)
{
this.garageDoor = garageDoor;
}
override void execute()
{
garageDoor.up();
}
}

View File

@ -0,0 +1,16 @@
module command.simpleremotecontrol.light;
import std.stdio : writeln;
class Light
{
void on()
{
writeln("Light is On");
}
void off()
{
writeln("Light is Off");
}
}

View File

@ -0,0 +1,19 @@
module command.simpleremotecontrol.lightoffcommand;
import command.simpleremotecontrol.command;
import command.simpleremotecontrol.light;
class LightOffCommand : Command
{
Light light;
this(Light light)
{
this.light = light;
}
override void execute()
{
light.off();
}
}

View File

@ -0,0 +1,19 @@
module command.simpleremotecontrol.lightoncommand;
import command.simpleremotecontrol.command;
import command.simpleremotecontrol.light;
class LightOnCommand : Command
{
Light light;
this(Light light)
{
this.light = light;
}
override void execute()
{
light.on();
}
}

View File

@ -0,0 +1,18 @@
module command.simpleremotecontrol.simpleremotecontrol;
import command.simpleremotecontrol.command;
class SimpleRemoteControl
{
Command slot;
void setCommand(Command command)
{
slot = command;
}
void buttonWasPressed()
{
slot.execute();
}
}