command remote
This commit is contained in:
parent
301e6da1a9
commit
857e6e97bb
|
@ -3,3 +3,9 @@
|
|||
Поведенческий паттерн проектирования, который превращает запросы в объекты, позволяя передавать их как аргументы при вызове методов, ставить запросы в очередь, логировать их, а также поддерживать отмену операций.
|
||||
|
||||
Паттерн **Команда** инкапсулирует запрос в виде объекта, делая возможной параметризацию клиентских объектов с другими запросами, организацию очереди или регистрацию запросов, а также поддержку отмены операций.
|
||||
|
||||
## Схемы
|
||||
|
||||
![scheme-1](scheme-1.png)
|
||||
|
||||
![scheme-2](scheme-2.png)
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
module command.remote.app;
|
||||
|
||||
import command.remote.remotecontrol;
|
||||
import command.remote.lightoncommand;
|
||||
import command.remote.lightoffcommand;
|
||||
import command.remote.light;
|
||||
import command.remote.ceilingfan;
|
||||
import command.remote.ceilingfanoffcommand;
|
||||
import command.remote.ceilingfanoncommand;
|
||||
import command.remote.garagedoorupcommand;
|
||||
import command.remote.garagedoordowncommand;
|
||||
import command.remote.garagedoor;
|
||||
import command.remote.stereoonwithcdcommand;
|
||||
import command.remote.stereo;
|
||||
import command.remote.stereooffcommand;
|
||||
import std.stdio : writeln;
|
||||
|
||||
void main()
|
||||
{
|
||||
auto remoteControl = new RemoteControl();
|
||||
|
||||
auto livingRoomLight = new Light("Living Room");
|
||||
auto kitchenLight = new Light("Kitchen");
|
||||
auto ceilingFan = new CeilingFan("Living Room");
|
||||
auto garageDoor = new GarageDoor("Garage");
|
||||
auto stereo = new Stereo("Living Room");
|
||||
|
||||
auto livingRoomLightOn = new LightOnCommand(livingRoomLight);
|
||||
auto livingRoomLightOff = new LightOffCommand(livingRoomLight);
|
||||
auto kitchenLightOn = new LightOnCommand(kitchenLight);
|
||||
auto kitchenLightOff = new LightOffCommand(kitchenLight);
|
||||
auto ceilingFanOn = new CeilingFanOnCommand(ceilingFan);
|
||||
auto ceilingFanOff = new CeilingFanOffCommand(ceilingFan);
|
||||
auto garageDoorUp = new GarageDoorUpCommand(garageDoor);
|
||||
auto garageDoorDown = new GarageDoorDownCommand(garageDoor);
|
||||
auto stereoOnWithCD = new StereoOnWithCDCommand(stereo);
|
||||
auto stereoOff = new StereoOffCommand(stereo);
|
||||
|
||||
remoteControl.setCommand(0, livingRoomLightOn, livingRoomLightOff);
|
||||
remoteControl.setCommand(1, kitchenLightOn, kitchenLightOff);
|
||||
remoteControl.setCommand(2, ceilingFanOn, ceilingFanOff);
|
||||
remoteControl.setCommand(2, stereoOnWithCD, stereoOff);
|
||||
remoteControl.setCommand(4, garageDoorUp, garageDoorDown);
|
||||
|
||||
writeln(remoteControl);
|
||||
|
||||
remoteControl.onButtonWasPressed(0);
|
||||
remoteControl.offButtonWasPressed(0);
|
||||
remoteControl.onButtonWasPressed(1);
|
||||
remoteControl.offButtonWasPressed(1);
|
||||
remoteControl.onButtonWasPressed(2);
|
||||
remoteControl.offButtonWasPressed(2);
|
||||
remoteControl.onButtonWasPressed(3);
|
||||
remoteControl.offButtonWasPressed(3);
|
||||
remoteControl.onButtonWasPressed(4);
|
||||
remoteControl.offButtonWasPressed(4);
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
module command.remote.ceilingfan;
|
||||
|
||||
import std.stdio : writeln;
|
||||
|
||||
class CeilingFan
|
||||
{
|
||||
private string location;
|
||||
private int level;
|
||||
|
||||
static int HIGH = 2;
|
||||
static int MEDIUM = 1;
|
||||
static int LOW = 0;
|
||||
|
||||
this(string location)
|
||||
{
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
void high()
|
||||
{
|
||||
level = HIGH;
|
||||
writeln(location ~ " ceiling fan is on high");
|
||||
|
||||
}
|
||||
|
||||
void medium()
|
||||
{
|
||||
level = MEDIUM;
|
||||
writeln(location ~ " ceiling fan is on medium");
|
||||
}
|
||||
|
||||
void low()
|
||||
{
|
||||
level = LOW;
|
||||
writeln(location ~ " ceiling fan is on low");
|
||||
}
|
||||
|
||||
void off()
|
||||
{
|
||||
level = 0;
|
||||
writeln(location ~ " ceiling fan is off");
|
||||
}
|
||||
|
||||
int getSpeed()
|
||||
{
|
||||
return level;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
module command.remote.ceilingfanoffcommand;
|
||||
|
||||
import command.remote.command;
|
||||
import command.remote.ceilingfan;
|
||||
|
||||
class CeilingFanOffCommand : Command
|
||||
{
|
||||
CeilingFan ceilingFan;
|
||||
|
||||
this(CeilingFan ceilingFan)
|
||||
{
|
||||
this.ceilingFan = ceilingFan;
|
||||
}
|
||||
|
||||
override void execute()
|
||||
{
|
||||
ceilingFan.off();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
module command.remote.ceilingfanoncommand;
|
||||
|
||||
import command.remote.command;
|
||||
import command.remote.ceilingfan;
|
||||
|
||||
class CeilingFanOnCommand : Command
|
||||
{
|
||||
CeilingFan ceilingFan;
|
||||
|
||||
this(CeilingFan ceilingFan)
|
||||
{
|
||||
this.ceilingFan = ceilingFan;
|
||||
}
|
||||
|
||||
override void execute()
|
||||
{
|
||||
ceilingFan.high();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
module command.remote.command;
|
||||
|
||||
interface Command
|
||||
{
|
||||
void execute();
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
module command.remote.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,19 @@
|
|||
module command.remote.garagedoordowncommand;
|
||||
|
||||
import command.remote.command;
|
||||
import command.remote.garagedoor;
|
||||
|
||||
class GarageDoorDownCommand : Command
|
||||
{
|
||||
GarageDoor garageDoor;
|
||||
|
||||
this(GarageDoor garageDoor)
|
||||
{
|
||||
this.garageDoor = garageDoor;
|
||||
}
|
||||
|
||||
override void execute()
|
||||
{
|
||||
garageDoor.down();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
module command.remote.garagedoorupcommand;
|
||||
|
||||
import command.remote.command;
|
||||
import command.remote.garagedoor;
|
||||
|
||||
class GarageDoorUpCommand : Command
|
||||
{
|
||||
GarageDoor garageDoor;
|
||||
|
||||
this(GarageDoor garageDoor)
|
||||
{
|
||||
this.garageDoor = garageDoor;
|
||||
}
|
||||
|
||||
override void execute()
|
||||
{
|
||||
garageDoor.up();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
module command.remote.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,19 @@
|
|||
module command.remote.lightoffcommand;
|
||||
|
||||
import command.remote.command;
|
||||
import command.remote.light;
|
||||
|
||||
class LightOffCommand : Command
|
||||
{
|
||||
Light light;
|
||||
|
||||
this(Light light)
|
||||
{
|
||||
this.light = light;
|
||||
}
|
||||
|
||||
override void execute()
|
||||
{
|
||||
light.off();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
module command.remote.lightoncommand;
|
||||
|
||||
import command.remote.command;
|
||||
import command.remote.light;
|
||||
|
||||
class LightOnCommand : Command
|
||||
{
|
||||
Light light;
|
||||
|
||||
this(Light light)
|
||||
{
|
||||
this.light = light;
|
||||
}
|
||||
|
||||
override void execute()
|
||||
{
|
||||
light.on();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
module command.remote.nocommand;
|
||||
|
||||
import command.remote.command;
|
||||
|
||||
class NoCommand : Command
|
||||
{
|
||||
override void execute() {}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
module command.remote.remotecontrol;
|
||||
|
||||
import command.remote.command;
|
||||
import command.remote.nocommand;
|
||||
import std.conv : to;
|
||||
import std.stdio : writeln;
|
||||
import std.algorithm.mutation : fill;
|
||||
|
||||
class RemoteControl
|
||||
{
|
||||
Command[] onCommands;
|
||||
Command[] offCommands;
|
||||
|
||||
this()
|
||||
{
|
||||
onCommands = new Command[7];
|
||||
offCommands = new Command[7];
|
||||
Command noCommand = new NoCommand();
|
||||
fill(onCommands, noCommand);
|
||||
fill(offCommands, noCommand);
|
||||
}
|
||||
|
||||
void setCommand(int slot, Command onCommand, Command offCommand)
|
||||
{
|
||||
onCommands[slot] = onCommand;
|
||||
offCommands[slot] = offCommand;
|
||||
}
|
||||
|
||||
void onButtonWasPressed(int slot)
|
||||
{
|
||||
onCommands[slot].execute();
|
||||
}
|
||||
|
||||
void offButtonWasPressed(int slot)
|
||||
{
|
||||
offCommands[slot].execute();
|
||||
}
|
||||
|
||||
override string toString() const
|
||||
{
|
||||
string s = "\n------ Remote Control -------\n";
|
||||
for (int i = 0; i < 7; ++i)
|
||||
{
|
||||
s ~= "[slot " ~ i.to!string ~ "] " ~ typeof(onCommands[i]).stringof
|
||||
~ " " ~ typeof(offCommands[i]).stringof ~ "\n";
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,44 @@
|
|||
module command.remote.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,19 @@
|
|||
module command.remote.stereooffcommand;
|
||||
|
||||
import command.remote.command;
|
||||
import command.remote.stereo;
|
||||
|
||||
class StereoOffCommand : Command
|
||||
{
|
||||
Stereo stereo;
|
||||
|
||||
this(Stereo stereo)
|
||||
{
|
||||
this.stereo = stereo;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
stereo.off();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
module command.remote.stereoonwithcdcommand;
|
||||
|
||||
import command.remote.command;
|
||||
import command.remote.stereo;
|
||||
|
||||
class StereoOnWithCDCommand : Command
|
||||
{
|
||||
Stereo stereo;
|
||||
|
||||
this(Stereo stereo)
|
||||
{
|
||||
this.stereo = stereo;
|
||||
}
|
||||
|
||||
void execute()
|
||||
{
|
||||
stereo.on();
|
||||
stereo.setCD();
|
||||
stereo.setVolume(11);
|
||||
}
|
||||
}
|
Binary file not shown.
After Width: | Height: | Size: 584 KiB |
Binary file not shown.
After Width: | Height: | Size: 322 KiB |
|
@ -1,3 +1,7 @@
|
|||
# Одиночка
|
||||
|
||||
Порождающий паттерн проектирования, который гарантирует, что у класса есть только один экземпляр, и предоставляет к нему глобальную точку доступа.
|
||||
|
||||
## Схемы
|
||||
|
||||
![scheme-1](scheme-1.png)
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 193 KiB |
Reference in New Issue