diff --git a/command/README.md b/command/README.md index 53fca58..f17abfd 100644 --- a/command/README.md +++ b/command/README.md @@ -3,3 +3,9 @@ Поведенческий паттерн проектирования, который превращает запросы в объекты, позволяя передавать их как аргументы при вызове методов, ставить запросы в очередь, логировать их, а также поддерживать отмену операций. Паттерн **Команда** инкапсулирует запрос в виде объекта, делая возможной параметризацию клиентских объектов с другими запросами, организацию очереди или регистрацию запросов, а также поддержку отмены операций. + +## Схемы + +![scheme-1](scheme-1.png) + +![scheme-2](scheme-2.png) diff --git a/command/remote/app.d b/command/remote/app.d new file mode 100644 index 0000000..5133a5d --- /dev/null +++ b/command/remote/app.d @@ -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); +} diff --git a/command/remote/ceilingfan.d b/command/remote/ceilingfan.d new file mode 100644 index 0000000..6e68c77 --- /dev/null +++ b/command/remote/ceilingfan.d @@ -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; + } +} diff --git a/command/remote/ceilingfanoffcommand.d b/command/remote/ceilingfanoffcommand.d new file mode 100644 index 0000000..374160a --- /dev/null +++ b/command/remote/ceilingfanoffcommand.d @@ -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(); + } +} diff --git a/command/remote/ceilingfanoncommand.d b/command/remote/ceilingfanoncommand.d new file mode 100644 index 0000000..bd103be --- /dev/null +++ b/command/remote/ceilingfanoncommand.d @@ -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(); + } +} diff --git a/command/remote/command.d b/command/remote/command.d new file mode 100644 index 0000000..6ff373c --- /dev/null +++ b/command/remote/command.d @@ -0,0 +1,6 @@ +module command.remote.command; + +interface Command +{ + void execute(); +} diff --git a/command/remote/garagedoor.d b/command/remote/garagedoor.d new file mode 100644 index 0000000..0acc4c3 --- /dev/null +++ b/command/remote/garagedoor.d @@ -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"); + } +} diff --git a/command/remote/garagedoordowncommand.d b/command/remote/garagedoordowncommand.d new file mode 100644 index 0000000..2080815 --- /dev/null +++ b/command/remote/garagedoordowncommand.d @@ -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(); + } +} diff --git a/command/remote/garagedoorupcommand.d b/command/remote/garagedoorupcommand.d new file mode 100644 index 0000000..bbe7d71 --- /dev/null +++ b/command/remote/garagedoorupcommand.d @@ -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(); + } +} diff --git a/command/remote/light.d b/command/remote/light.d new file mode 100644 index 0000000..ada1b47 --- /dev/null +++ b/command/remote/light.d @@ -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"); + } +} diff --git a/command/remote/lightoffcommand.d b/command/remote/lightoffcommand.d new file mode 100644 index 0000000..3124c74 --- /dev/null +++ b/command/remote/lightoffcommand.d @@ -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(); + } +} diff --git a/command/remote/lightoncommand.d b/command/remote/lightoncommand.d new file mode 100644 index 0000000..9fa2298 --- /dev/null +++ b/command/remote/lightoncommand.d @@ -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(); + } +} diff --git a/command/remote/nocommand.d b/command/remote/nocommand.d new file mode 100644 index 0000000..85b138d --- /dev/null +++ b/command/remote/nocommand.d @@ -0,0 +1,8 @@ +module command.remote.nocommand; + +import command.remote.command; + +class NoCommand : Command +{ + override void execute() {} +} diff --git a/command/remote/remotecontrol.d b/command/remote/remotecontrol.d new file mode 100644 index 0000000..797965e --- /dev/null +++ b/command/remote/remotecontrol.d @@ -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; + } +} diff --git a/command/remote/stereo.d b/command/remote/stereo.d new file mode 100644 index 0000000..65f32f8 --- /dev/null +++ b/command/remote/stereo.d @@ -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); + } +} diff --git a/command/remote/stereooffcommand.d b/command/remote/stereooffcommand.d new file mode 100644 index 0000000..ed9dcf4 --- /dev/null +++ b/command/remote/stereooffcommand.d @@ -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(); + } +} diff --git a/command/remote/stereoonwithcdcommand.d b/command/remote/stereoonwithcdcommand.d new file mode 100644 index 0000000..8b8c87a --- /dev/null +++ b/command/remote/stereoonwithcdcommand.d @@ -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); + } +} diff --git a/command/scheme-1.png b/command/scheme-1.png new file mode 100644 index 0000000..9f7263b Binary files /dev/null and b/command/scheme-1.png differ diff --git a/command/scheme-2.png b/command/scheme-2.png new file mode 100644 index 0000000..9149dff Binary files /dev/null and b/command/scheme-2.png differ diff --git a/singleton/README.md b/singleton/README.md index 500e281..e08b7a0 100644 --- a/singleton/README.md +++ b/singleton/README.md @@ -1,3 +1,7 @@ # Одиночка Порождающий паттерн проектирования, который гарантирует, что у класса есть только один экземпляр, и предоставляет к нему глобальную точку доступа. + +## Схемы + +![scheme-1](scheme-1.png) diff --git a/singleton/scheme-1.png b/singleton/scheme-1.png new file mode 100644 index 0000000..d3c3ab7 Binary files /dev/null and b/singleton/scheme-1.png differ