simple remote command
This commit is contained in:
parent
91e4cafaa9
commit
3c374df63d
|
@ -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();
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
module command.simpleremotecontrol.command;
|
||||
|
||||
interface Command
|
||||
{
|
||||
void execute();
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
Reference in New Issue