Compare commits

..

32 Commits
master ... D

Author SHA1 Message Date
Alexander Zhirov ffaefeac75 state gummball winner 2022-11-20 21:09:50 +03:00
Alexander Zhirov e93f2da595 state gummball 2022-11-20 02:40:58 +03:00
Alexander Zhirov 41ff6962c3 composite code 2022-11-18 01:06:11 +03:00
Alexander Zhirov 9a6936ed7b composite 2022-11-17 16:36:56 +03:00
Alexander Zhirov 935f9c7563 iterator 2022-11-17 15:39:07 +03:00
Alexander Zhirov afb0c148a1 template method: adding sort 2022-11-17 10:16:54 +03:00
Alexander Zhirov 38ae23f3db Merge branch 'D' of zhirov.kz:alexander/patterns into D 2022-11-17 02:26:47 +03:00
Alexander Zhirov 0c320c05a4 template method 2022-11-17 02:25:52 +03:00
Alexander Zhirov a66b3dcbb5 facade 2022-11-16 16:43:46 +03:00
Alexander Zhirov deac4ef4c7 adapter 2022-11-16 13:55:33 +03:00
Alexander Zhirov ecfd117aba remote macrocommand 2022-11-15 23:25:25 +03:00
Alexander Zhirov 94690db1ef remote undo status 2022-11-15 22:39:18 +03:00
Alexander Zhirov 949451a1f1 remote undo 2022-11-15 22:11:46 +03:00
Alexander Zhirov 1744b158c6 new command scheme 2022-11-15 16:54:35 +03:00
Alexander Zhirov 857e6e97bb command remote 2022-11-15 16:51:22 +03:00
Alexander Zhirov 301e6da1a9 simple remote command 2022-11-14 22:45:14 +03:00
Alexander Zhirov 3c374df63d simple remote command 2022-11-14 22:45:04 +03:00
Alexander Zhirov 91e4cafaa9 singleton 2022-11-14 21:54:08 +03:00
Alexander Zhirov 9091f51cfa added schemes 2022-11-14 15:13:48 +03:00
Alexander Zhirov 9a7cf7f6f8 abstract factory: thick crust dough 2022-11-14 00:45:45 +03:00
Alexander Zhirov 0aabb39e79 abstract factory 2022-11-13 03:59:35 +03:00
Alexander Zhirov 8bfa03ce82 factory method 2022-11-12 22:47:14 +03:00
Alexander Zhirov f9c95af5ff replace tabs 2022-11-12 01:24:33 +03:00
Alexander Zhirov f197ef0255 simple factory 2022-11-12 01:22:12 +03:00
Alexander Zhirov 90c1b88af9 decorator 2022-11-11 15:51:42 +03:00
Alexander Zhirov e815172862 add Java sources 2022-11-11 12:54:25 +03:00
Alexander Zhirov b6e1ec089c notifyObservers second variant 2022-11-11 10:56:59 +03:00
Alexander Zhirov 850cb17d02 notifyObservers first variant 2022-11-11 10:22:38 +03:00
Alexander Zhirov 8170c93255 observer CurrentConditionsDisplay 2022-11-11 03:03:18 +03:00
Alexander Zhirov ddbfb4820c beginning observer 2022-11-10 16:28:55 +03:00
Alexander Zhirov e9712fdf28 change the flight in strategy 2022-11-10 00:29:18 +03:00
Alexander Zhirov 8284e0f476 strategy 2022-11-10 00:22:13 +03:00
284 changed files with 7060 additions and 2 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
app
app.o

Binary file not shown.

View File

@ -1,3 +1,39 @@
# Паттерны проектирования
# Паттерны проектирования на языке D
Паттерны проектирования на различных языках программирования по книге [Head First. Паттерны проектирования](https://ftp.zhirov.kz/books/IT/%D0%9F%D0%B0%D1%82%D1%82%D0%B5%D1%80%D0%BD%D1%8B/Head%20First.%20%D0%9F%D0%B0%D1%82%D1%82%D0%B5%D1%80%D0%BD%D1%8B%20%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F%20%28%D0%AD%D1%80%D0%B8%D0%BA%20%D0%A4%D1%80%D0%B8%D0%BC%D0%B5%D0%BD%2C%20%D0%AD%D0%BB%D0%B8%D0%B7%D0%B0%D0%B1%D0%B5%D1%82%20%D0%A0%D0%BE%D0%B1%D1%81%D0%BE%D0%BD%29%202022.pdf)
Паттерны проектирования по книге [Head First. Паттерны проектирования](https://ftp.zhirov.kz/books/IT/%D0%9F%D0%B0%D1%82%D1%82%D0%B5%D1%80%D0%BD%D1%8B/Head%20First.%20%D0%9F%D0%B0%D1%82%D1%82%D0%B5%D1%80%D0%BD%D1%8B%20%D0%BF%D1%80%D0%BE%D0%B5%D0%BA%D1%82%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D1%8F%20%28%D0%AD%D1%80%D0%B8%D0%BA%20%D0%A4%D1%80%D0%B8%D0%BC%D0%B5%D0%BD%2C%20%D0%AD%D0%BB%D0%B8%D0%B7%D0%B0%D0%B1%D0%B5%D1%82%20%D0%A0%D0%BE%D0%B1%D1%81%D0%BE%D0%BD%29%202022.pdf) ([исходный код на Java](Head-First-Design-Patterns-master.zip))
## Паттерны
### Поведенческие
1. [Стратегия](strategy/)
2. [Наблюдатель](observer/)
3. [Команда](command/)
4. [Шаблонный метод](templatemethod/)
5. [Итератор](iterator/)
6. [Состояние](state/)
### Структурные
1. [Декоратор](decorator/)
2. [Адаптер](adapter/)
3. [Фасад](facade/)
4. [Компоновщик](composite/)
### Пораждающие
1. [Фабричный метод (Простая фабрика)](factorymethod/)
2. [Абстрактная фабрика](abstractfactory/)
3. [Одиночка](singleton/)
## Компиляция
```sh
dmd *.d
```
или
```sh
ldc2 *.d
```

15
abstractfactory/README.md Normal file
View File

@ -0,0 +1,15 @@
# Абстрактная фабрика
Порождающий паттерн проектирования, который позволяет создавать семейства связанных объектов, не привязываясь к конкретным классам создаваемых объектов.
Паттерн **Абстрактная Фабрика** предоставляет интерфейс создания семейств взаимосвязанных или взаимозависимых объектов без указания их конкретных классов.
## Принципы
- Код должен зависеть от абстракций, а не от конкретных классов
## Схемы
![scheme-1](scheme-1.png)
![scheme-2](scheme-2.png)

37
abstractfactory/app.d Normal file
View File

@ -0,0 +1,37 @@
module abstractfactory.app;
import abstractfactory.pizza;
import abstractfactory.pizzastore;
import abstractfactory.nypizzastore;
import abstractfactory.chicagopizzastore;
import std.stdio : writeln;
void main()
{
PizzaStore nyStore = new NYPizzaStore();
PizzaStore chicagoStore = new ChicagoPizzaStore();
Pizza pizza = nyStore.orderPizza("cheese");
writeln("Ethan ordered a ", pizza);
pizza = chicagoStore.orderPizza("cheese");
writeln("Joel ordered a ", pizza);
pizza = nyStore.orderPizza("clam");
writeln("Ethan ordered a ", pizza);
pizza = chicagoStore.orderPizza("clam");
writeln("Joel ordered a ", pizza);
pizza = nyStore.orderPizza("pepperoni");
writeln("Ethan ordered a ", pizza);
pizza = chicagoStore.orderPizza("pepperoni");
writeln("Joel ordered a ", pizza);
pizza = nyStore.orderPizza("veggie");
writeln("Ethan ordered a ", pizza);
pizza = chicagoStore.orderPizza("veggie");
writeln("Joel ordered a ", pizza);
}

View File

@ -0,0 +1,11 @@
module abstractfactory.blackolives;
import abstractfactory.veggies;
class BlackOlives : Veggies
{
override string toString() const @safe pure nothrow
{
return "Black Olives";
}
}

9
abstractfactory/cheese.d Normal file
View File

@ -0,0 +1,9 @@
module abstractfactory.cheese;
interface Cheese
{
string opBinary(string op : "~")(string s) const
{
return (cast(Object)this).toString() ~ s;
}
}

View File

@ -0,0 +1,23 @@
module abstractfactory.cheesepizza;
import abstractfactory.pizza;
import abstractfactory.pizzaingredientfactory;
import std.stdio : writeln;
class CheesePizza : Pizza
{
PizzaIngredientFactory ingredientFactory;
this(PizzaIngredientFactory ingredientFactory)
{
this.ingredientFactory = ingredientFactory;
}
override void prepare()
{
writeln("Preparing ", name);
dough = ingredientFactory.createDough();
sauce = ingredientFactory.createSauce();
cheese = ingredientFactory.createCheese();
}
}

View File

@ -0,0 +1,51 @@
module abstractfactory.chicagopizzaingredientfactory;
import abstractfactory.pizzaingredientfactory;
import abstractfactory.dough;
import abstractfactory.thickcrustdough;
import abstractfactory.sauce;
import abstractfactory.plumtomatosauce;
import abstractfactory.cheese;
import abstractfactory.mozzarellacheese;
import abstractfactory.veggies;
import abstractfactory.blackolives;
import abstractfactory.spinach;
import abstractfactory.eggplant;
import abstractfactory.pepperoni;
import abstractfactory.slicedpepperoni;
import abstractfactory.clams;
import abstractfactory.frozenclams;
class ChicagoPizzaIngredientFactory : PizzaIngredientFactory
{
override Dough createDough()
{
return new ThinCrustDough();
}
override Sauce createSauce()
{
return new PlumTomatoSauce();
}
override Cheese createCheese()
{
return new MozzarellaCheese();
}
override Veggies[] createVeggies()
{
Veggies[] veggies = cast(Veggies[])[ new BlackOlives(), new Spinach(), new Eggplant() ];
return veggies;
}
override Pepperoni createPepperoni()
{
return new SlicedPepperoni();
}
override Clams createClam()
{
return new FrozenClams();
}
}

View File

@ -0,0 +1,43 @@
module abstractfactory.chicagopizzastore;
import abstractfactory.pizza;
import abstractfactory.pizzastore;
import abstractfactory.pizzaingredientfactory;
import abstractfactory.chicagopizzaingredientfactory;
import abstractfactory.cheesepizza;
import abstractfactory.veggiepizza;
import abstractfactory.clampizza;
import abstractfactory.pepperonipizza;
class ChicagoPizzaStore : PizzaStore
{
protected:
override Pizza createPizza(string item)
{
Pizza pizza;
PizzaIngredientFactory ingredientFactory = new ChicagoPizzaIngredientFactory();
if (item == "cheese")
{
pizza = new CheesePizza(ingredientFactory);
pizza.setName("Chicago Style Cheese Pizza");
}
else if (item == "veggie")
{
pizza = new VeggiePizza(ingredientFactory);
pizza.setName("Chicago Style Veggie Pizza");
}
else if (item == "clam")
{
pizza = new ClamPizza(ingredientFactory);
pizza.setName("Chicago Style Clam Pizza");
}
else if (item == "pepperoni")
{
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("Chicago Style Pepperoni Pizza");
}
return pizza;
}
}

View File

@ -0,0 +1,24 @@
module abstractfactory.clampizza;
import abstractfactory.pizza;
import abstractfactory.pizzaingredientfactory;
import std.stdio : writeln;
class ClamPizza : Pizza
{
PizzaIngredientFactory ingredientFactory;
this(PizzaIngredientFactory ingredientFactory)
{
this.ingredientFactory = ingredientFactory;
}
override void prepare()
{
writeln("Preparing ", name);
dough = ingredientFactory.createDough();
sauce = ingredientFactory.createSauce();
cheese = ingredientFactory.createCheese();
clams = ingredientFactory.createClam();
}
}

9
abstractfactory/clams.d Normal file
View File

@ -0,0 +1,9 @@
module abstractfactory.clams;
interface Clams
{
string opBinary(string op : "~")(string s) const
{
return (cast(Object)this).toString() ~ s;
}
}

9
abstractfactory/dough.d Normal file
View File

@ -0,0 +1,9 @@
module abstractfactory.dough;
interface Dough
{
string opBinary(string op : "~")(string s) const
{
return (cast(Object)this).toString() ~ s;
}
}

View File

@ -0,0 +1,11 @@
module abstractfactory.eggplant;
import abstractfactory.veggies;
class Eggplant : Veggies
{
override string toString() const @safe pure nothrow
{
return "Eggplant";
}
}

View File

@ -0,0 +1,11 @@
module abstractfactory.freshclams;
import abstractfactory.clams;
class FreshClams : Clams
{
override string toString() const @safe pure nothrow
{
return "Fresh Clams from Long Island Sound";
}
}

View File

@ -0,0 +1,11 @@
module abstractfactory.frozenclams;
import abstractfactory.clams;
class FrozenClams : Clams
{
override string toString() const @safe pure nothrow
{
return "Frozen Clams from Chesapeake Bay";
}
}

11
abstractfactory/garlic.d Normal file
View File

@ -0,0 +1,11 @@
module abstractfactory.garlic;
import abstractfactory.veggies;
class Garlic : Veggies
{
override string toString() const @safe pure nothrow
{
return "Garlic";
}
}

View File

@ -0,0 +1,11 @@
module abstractfactory.marinarasauce;
import abstractfactory.sauce;
class MarinaraSauce : Sauce
{
override string toString() const @safe pure nothrow
{
return "Marinara Sauce";
}
}

View File

@ -0,0 +1,11 @@
module abstractfactory.mozzarellacheese;
import abstractfactory.cheese;
class MozzarellaCheese : Cheese
{
override string toString() const @safe pure nothrow
{
return "Shredded Mozzarella";
}
}

View File

@ -0,0 +1,11 @@
module abstractfactory.mushroom;
import abstractfactory.veggies;
class Mushroom : Veggies
{
override string toString() const @safe pure nothrow
{
return "Mushroom";
}
}

View File

@ -0,0 +1,52 @@
module abstractfactory.nypizzaingredientfactory;
import abstractfactory.pizzaingredientfactory;
import abstractfactory.dough;
import abstractfactory.thincrustdough;
import abstractfactory.sauce;
import abstractfactory.marinarasauce;
import abstractfactory.cheese;
import abstractfactory.reggianocheese;
import abstractfactory.veggies;
import abstractfactory.garlic;
import abstractfactory.onion;
import abstractfactory.mushroom;
import abstractfactory.redpepper;
import abstractfactory.pepperoni;
import abstractfactory.slicedpepperoni;
import abstractfactory.clams;
import abstractfactory.freshclams;
class NYPizzaIngredientFactory : PizzaIngredientFactory
{
override Dough createDough()
{
return new ThinCrustDough();
}
override Sauce createSauce()
{
return new MarinaraSauce();
}
override Cheese createCheese()
{
return new ReggianoCheese();
}
override Veggies[] createVeggies()
{
Veggies[] veggies = cast(Veggies[])[ new Garlic(), new Onion(), new Mushroom(), new RedPepper() ];
return veggies;
}
override Pepperoni createPepperoni()
{
return new SlicedPepperoni();
}
override Clams createClam()
{
return new FreshClams();
}
}

View File

@ -0,0 +1,43 @@
module abstractfactory.nypizzastore;
import abstractfactory.pizza;
import abstractfactory.pizzastore;
import abstractfactory.pizzaingredientfactory;
import abstractfactory.nypizzaingredientfactory;
import abstractfactory.cheesepizza;
import abstractfactory.veggiepizza;
import abstractfactory.clampizza;
import abstractfactory.pepperonipizza;
class NYPizzaStore : PizzaStore
{
protected:
override Pizza createPizza(string item)
{
Pizza pizza;
PizzaIngredientFactory ingredientFactory = new NYPizzaIngredientFactory();
if (item == "cheese")
{
pizza = new CheesePizza(ingredientFactory);
pizza.setName("New York Style Cheese Pizza");
}
else if (item == "veggie")
{
pizza = new VeggiePizza(ingredientFactory);
pizza.setName("New York Style Veggie Pizza");
}
else if (item == "clam")
{
pizza = new ClamPizza(ingredientFactory);
pizza.setName("New York Style Clam Pizza");
}
else if (item == "pepperoni")
{
pizza = new PepperoniPizza(ingredientFactory);
pizza.setName("New York Style Pepperoni Pizza");
}
return pizza;
}
}

11
abstractfactory/onion.d Normal file
View File

@ -0,0 +1,11 @@
module abstractfactory.onion;
import abstractfactory.veggies;
class Onion : Veggies
{
override string toString() const @safe pure nothrow
{
return "Onion";
}
}

View File

@ -0,0 +1,9 @@
module abstractfactory.pepperoni;
interface Pepperoni
{
string opBinary(string op : "~")(string s) const
{
return (cast(Object)this).toString() ~ s;
}
}

View File

@ -0,0 +1,25 @@
module abstractfactory.pepperonipizza;
import abstractfactory.pizza;
import abstractfactory.pizzaingredientfactory;
import std.stdio : writeln;
class PepperoniPizza : Pizza
{
PizzaIngredientFactory ingredientFactory;
this(PizzaIngredientFactory ingredientFactory)
{
this.ingredientFactory = ingredientFactory;
}
override void prepare()
{
writeln("Preparing ", name);
dough = ingredientFactory.createDough();
sauce = ingredientFactory.createSauce();
cheese = ingredientFactory.createCheese();
veggies = ingredientFactory.createVeggies();
pepperoni = ingredientFactory.createPepperoni();
}
}

88
abstractfactory/pizza.d Normal file
View File

@ -0,0 +1,88 @@
module abstractfactory.pizza;
import abstractfactory.dough;
import abstractfactory.sauce;
import abstractfactory.veggies;
import abstractfactory.cheese;
import abstractfactory.pepperoni;
import abstractfactory.clams;
import std.stdio : writeln;
import std.conv : to;
abstract class Pizza
{
protected:
string name;
Dough dough;
Sauce sauce;
Veggies[] veggies;
Cheese cheese;
Pepperoni pepperoni;
Clams clams;
public:
void prepare();
void bake()
{
writeln("Bake for 25 minutes at 350");
}
void cut()
{
writeln("Cut the pizza into diagonal slices");
}
void box()
{
writeln("Place pizza in official PizzaStore box");
}
void setName(string name)
{
this.name = name;
}
string getName()
{
return name;
}
override string toString() const
{
string s;
s ~= "---- " ~ name ~ " ----\n";
if (dough !is null)
{
s ~= dough ~ "\n";
}
if (sauce !is null)
{
s ~= sauce ~ "\n";
}
if (cheese !is null)
{
s ~= cheese ~ "\n";
}
if (veggies.length)
{
foreach (key, val; veggies)
{
s ~= val.to!string;
if (key + 1 < veggies.length)
{
s ~= ", ";
}
}
}
if (clams !is null)
{
s ~= clams ~ "\n";
}
if (pepperoni !is null)
{
s ~= pepperoni ~ "\n";
}
return s;
}
}

View File

@ -0,0 +1,18 @@
module abstractfactory.pizzaingredientfactory;
import abstractfactory.dough;
import abstractfactory.sauce;
import abstractfactory.cheese;
import abstractfactory.veggies;
import abstractfactory.pepperoni;
import abstractfactory.clams;
interface PizzaIngredientFactory
{
Dough createDough();
Sauce createSauce();
Cheese createCheese();
Veggies[] createVeggies();
Pepperoni createPepperoni();
Clams createClam();
}

View File

@ -0,0 +1,24 @@
module abstractfactory.pizzastore;
import abstractfactory.pizza;
import std.stdio : writeln;
abstract class PizzaStore
{
protected:
Pizza createPizza(string item);
public:
Pizza orderPizza(string type)
{
Pizza pizza = createPizza(type);
writeln("--- Making a " ~ pizza.getName() ~ " ---");
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
}

View File

@ -0,0 +1,11 @@
module abstractfactory.plumtomatosauce;
import abstractfactory.sauce;
class PlumTomatoSauce : Sauce
{
override string toString() const @safe pure nothrow
{
return "Tomato sauce with plum tomatoes";
}
}

View File

@ -0,0 +1,11 @@
module abstractfactory.redpepper;
import abstractfactory.veggies;
class RedPepper : Veggies
{
override string toString() const @safe pure nothrow
{
return "Red Pepper";
}
}

View File

@ -0,0 +1,11 @@
module abstractfactory.reggianocheese;
import abstractfactory.cheese;
class ReggianoCheese : Cheese
{
override string toString() const @safe pure nothrow
{
return "Reggiano Cheese";
}
}

9
abstractfactory/sauce.d Normal file
View File

@ -0,0 +1,9 @@
module abstractfactory.sauce;
interface Sauce
{
string opBinary(string op : "~")(string s) const
{
return (cast(Object)this).toString() ~ s;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 375 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 449 KiB

View File

@ -0,0 +1,11 @@
module abstractfactory.slicedpepperoni;
import abstractfactory.pepperoni;
class SlicedPepperoni : Pepperoni
{
override string toString() const @safe pure nothrow
{
return "Sliced Pepperoni";
}
}

11
abstractfactory/spinach.d Normal file
View File

@ -0,0 +1,11 @@
module abstractfactory.spinach;
import abstractfactory.veggies;
class Spinach : Veggies
{
override string toString() const @safe pure nothrow
{
return "Spinach";
}
}

View File

@ -0,0 +1,16 @@
module abstractfactory.thickcrustdough;
import abstractfactory.dough;
class ThinCrustDough : Dough
{
override string toString() const @safe pure nothrow
{
return "ThickCrust style extra thick crust dough";
}
string opBinary(string op : "~")(string s)
{
return (cast(Object)this).toString() ~ s;
}
}

View File

@ -0,0 +1,16 @@
module abstractfactory.thincrustdough;
import abstractfactory.dough;
class ThinCrustDough : Dough
{
override string toString() const @safe pure nothrow
{
return "Thin Crust Dough";
}
string opBinary(string op : "~")(string s)
{
return (cast(Object)this).toString() ~ s;
}
}

View File

@ -0,0 +1,24 @@
module abstractfactory.veggiepizza;
import abstractfactory.pizza;
import abstractfactory.pizzaingredientfactory;
import std.stdio : writeln;
class VeggiePizza : Pizza
{
PizzaIngredientFactory ingredientFactory;
this(PizzaIngredientFactory ingredientFactory)
{
this.ingredientFactory = ingredientFactory;
}
override void prepare()
{
writeln("Preparing ", name);
dough = ingredientFactory.createDough();
sauce = ingredientFactory.createSauce();
cheese = ingredientFactory.createCheese();
veggies = ingredientFactory.createVeggies();
}
}

View File

@ -0,0 +1,9 @@
module abstractfactory.veggies;
interface Veggies
{
string opBinary(string op : "~")(string s) const
{
return (cast(Object)this).toString() ~ s;
}
}

19
adapter/README.md Normal file
View File

@ -0,0 +1,19 @@
# Адаптер
Структурный паттерн проектирования, который позволяет объектам с несовместимыми интерфейсами работать вместе.
Паттерн **Адаптер** преобразует интерфейс класса к другому интерфейсу, на который рассчитан клиент. Адаптер обеспечивает совместную работу классов, невозможную в обычных условиях из-за несовместимости интерфейсов.
## Схемы
![scheme-1](scheme-1.png)
![scheme-2](scheme-2.png)
![scheme-3](scheme-3.png)
![scheme-4](scheme-4.png)
![scheme-5](scheme-5.png)
![scheme-6](scheme-6.png)

BIN
adapter/scheme-1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 197 KiB

BIN
adapter/scheme-2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 104 KiB

BIN
adapter/scheme-3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 61 KiB

BIN
adapter/scheme-4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

BIN
adapter/scheme-5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 161 KiB

BIN
adapter/scheme-6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 205 KiB

View File

@ -0,0 +1,28 @@
module app;
import lib;
import std.stdio : writeln;
void main()
{
Duck duck = new MallardDuck();
Turkey turkey = new WildTurkey();
Duck turkeyAdapter = new TurkeyAdapter(turkey);
writeln("The Turkey says...");
turkey.gobble();
turkey.fly();
writeln("\nThe Duck says...");
testDuck(duck);
writeln("\nThe TurkeyAdapter says...");
testDuck(turkeyAdapter);
}
void testDuck(Duck duck)
{
duck.quack();
duck.fly();
}

View File

@ -0,0 +1,7 @@
module lib.duck;
interface Duck
{
void quack();
void fly();
}

View File

@ -0,0 +1,17 @@
module lib.mallarduck;
import lib.duck;
import std.stdio : writeln;
class MallardDuck : Duck
{
void quack()
{
writeln("Quack!");
}
void fly()
{
writeln("I'm flying");
}
}

View File

@ -0,0 +1,7 @@
module lib;
public import lib.duck;
public import lib.mallarduck;
public import lib.turkey;
public import lib.turkeyadapter;
public import lib.wildturkey;

View File

@ -0,0 +1,7 @@
module lib.turkey;
interface Turkey
{
void gobble();
void fly();
}

View File

@ -0,0 +1,27 @@
module lib.turkeyadapter;
import lib.turkey;
import lib.duck;
class TurkeyAdapter : Duck
{
Turkey turkey;
this(Turkey turkey)
{
this.turkey = turkey;
}
void quack()
{
turkey.gobble();
}
void fly()
{
foreach (val; 0..5)
{
turkey.fly();
}
}
}

View File

@ -0,0 +1,17 @@
module lib.wildturkey;
import lib.turkey;
import std.stdio : writeln;
class WildTurkey : Turkey
{
void gobble()
{
writeln("Gobble gobble");
}
void fly()
{
writeln("I'm flying a short distance");
}
}

13
command/README.md Normal file
View File

@ -0,0 +1,13 @@
# Команда
Поведенческий паттерн проектирования, который превращает запросы в объекты, позволяя передавать их как аргументы при вызове методов, ставить запросы в очередь, логировать их, а также поддерживать отмену операций.
Паттерн **Команда** инкапсулирует запрос в виде объекта, делая возможной параметризацию клиентских объектов с другими запросами, организацию очереди или регистрацию запросов, а также поддержку отмены операций.
## Схемы
![scheme-1](scheme-1.png)
![scheme-2](scheme-2.png)
![scheme-3](scheme-3.png)

57
command/remote/app.d Normal file
View File

@ -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(3, 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);
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

6
command/remote/command.d Normal file
View File

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

View File

@ -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");
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

23
command/remote/light.d Normal file
View File

@ -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");
}
}

View File

@ -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();
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,8 @@
module command.remote.nocommand;
import command.remote.command;
class NoCommand : Command
{
override void execute() {}
}

View File

@ -0,0 +1,54 @@
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;
import std.array : split, back;
import std.format;
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 ~ "] "
~ format("%22s", (cast(Object)onCommands[i]).classinfo.name.split(".").back())
~ format("%23s", (cast(Object)offCommands[i]).classinfo.name.split(".").back())
~ "\n";
}
return s;
}
}

44
command/remote/stereo.d Normal file
View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

52
command/remoteundo/app.d Normal file
View File

@ -0,0 +1,52 @@
module command.remoteundo.app;
import command.remoteundo.remotecontrol;
import command.remoteundo.lightoncommand;
import command.remoteundo.lightoffcommand;
import command.remoteundo.light;
import command.remoteundo.ceilingfan;
import command.remoteundo.ceilingfanoffcommand;
import command.remoteundo.ceilingfanoncommand;
import command.remoteundo.garagedoorupcommand;
import command.remoteundo.garagedoordowncommand;
import command.remoteundo.garagedoor;
import command.remoteundo.stereoonwithcdcommand;
import command.remoteundo.stereo;
import command.remoteundo.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");
remoteControl.setCommand(0, new LightOnCommand(livingRoomLight), new LightOffCommand(livingRoomLight));
// remoteControl.setCommand(1, new LightOnCommand(kitchenLight), new LightOffCommand(kitchenLight));
// remoteControl.setCommand(2, new CeilingFanOnCommand(ceilingFan), new CeilingFanOffCommand(ceilingFan));
// remoteControl.setCommand(3, new StereoOnWithCDCommand(stereo), new StereoOffCommand(stereo));
// remoteControl.setCommand(4, new GarageDoorUpCommand(garageDoor), new GarageDoorDownCommand(garageDoor));
// writeln(remoteControl);
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
writeln(remoteControl);
remoteControl.undoButtonWasPushed();
remoteControl.offButtonWasPushed(0);
remoteControl.onButtonWasPushed(0);
writeln(remoteControl);
remoteControl.undoButtonWasPushed();
// remoteControl.onButtonWasPushed(1);
// remoteControl.offButtonWasPushed(1);
// remoteControl.onButtonWasPushed(2);
// remoteControl.offButtonWasPushed(2);
// remoteControl.onButtonWasPushed(3);
// remoteControl.offButtonWasPushed(3);
// remoteControl.onButtonWasPushed(4);
// remoteControl.offButtonWasPushed(4);
}

View File

@ -0,0 +1,48 @@
module command.remoteundo.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;
}
}

View File

@ -0,0 +1,24 @@
module command.remoteundo.ceilingfanoffcommand;
import command.remoteundo.command;
import command.remoteundo.ceilingfan;
class CeilingFanOffCommand : Command
{
CeilingFan ceilingFan;
this(CeilingFan ceilingFan)
{
this.ceilingFan = ceilingFan;
}
void execute()
{
ceilingFan.off();
}
void undo()
{
ceilingFan.high();
}
}

View File

@ -0,0 +1,24 @@
module command.remoteundo.ceilingfanoncommand;
import command.remoteundo.command;
import command.remoteundo.ceilingfan;
class CeilingFanOnCommand : Command
{
CeilingFan ceilingFan;
this(CeilingFan ceilingFan)
{
this.ceilingFan = ceilingFan;
}
void execute()
{
ceilingFan.high();
}
void undo()
{
ceilingFan.off();
}
}

View File

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

View File

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

View File

@ -0,0 +1,24 @@
module command.remoteundo.garagedoordowncommand;
import command.remoteundo.command;
import command.remoteundo.garagedoor;
class GarageDoorDownCommand : Command
{
GarageDoor garageDoor;
this(GarageDoor garageDoor)
{
this.garageDoor = garageDoor;
}
void execute()
{
garageDoor.down();
}
void undo()
{
garageDoor.up();
}
}

View File

@ -0,0 +1,24 @@
module command.remoteundo.garagedoorupcommand;
import command.remoteundo.command;
import command.remoteundo.garagedoor;
class GarageDoorUpCommand : Command
{
GarageDoor garageDoor;
this(GarageDoor garageDoor)
{
this.garageDoor = garageDoor;
}
void execute()
{
garageDoor.up();
}
void undo()
{
garageDoor.down();
}
}

View File

@ -0,0 +1,23 @@
module command.remoteundo.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.remoteundo.lightoffcommand;
import command.remoteundo.command;
import command.remoteundo.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.remoteundo.lightoncommand;
import command.remoteundo.command;
import command.remoteundo.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,9 @@
module command.remoteundo.nocommand;
import command.remoteundo.command;
class NoCommand : Command
{
void execute() {}
void undo() {}
}

View File

@ -0,0 +1,63 @@
module command.remoteundo.remotecontrol;
import command.remoteundo.command;
import command.remoteundo.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("%22s", (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.remoteundo.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.remoteundo.stereooffcommand;
import command.remoteundo.command;
import command.remoteundo.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.remoteundo.stereoonwithcdcommand;
import command.remoteundo.command;
import command.remoteundo.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,36 @@
module command.remoteundostatus.app;
import command.remoteundostatus.remotecontrol;
import command.remoteundostatus.lightoncommand;
import command.remoteundostatus.lightoffcommand;
import command.remoteundostatus.light;
import command.remoteundostatus.ceilingfan;
import command.remoteundostatus.ceilingfanoffcommand;
import command.remoteundostatus.ceilingfanhighcommand;
import command.remoteundostatus.ceilingfanmediumcommand;
import command.remoteundostatus.ceilingfanlowcommand;
import command.remoteundostatus.garagedoorupcommand;
import command.remoteundostatus.garagedoordowncommand;
import command.remoteundostatus.garagedoor;
import command.remoteundostatus.stereoonwithcdcommand;
import command.remoteundostatus.stereo;
import command.remoteundostatus.stereooffcommand;
import std.stdio : writeln;
void main()
{
auto remoteControl = new RemoteControl();
auto ceilingFan = new CeilingFan("Living Room");
remoteControl.setCommand(0, new CeilingFanMediumCommand(ceilingFan), new CeilingFanOffCommand(ceilingFan));
remoteControl.setCommand(1, new CeilingFanHighCommand(ceilingFan), new CeilingFanOffCommand(ceilingFan));
remoteControl.onButtonWasPushed(0);
remoteControl.offButtonWasPushed(0);
writeln(remoteControl);
remoteControl.undoButtonWasPushed();
remoteControl.onButtonWasPushed(1);
writeln(remoteControl);
remoteControl.undoButtonWasPushed();
}

View File

@ -0,0 +1,49 @@
module command.remoteundostatus.ceilingfan;
import std.stdio : writeln;
class CeilingFan
{
private string location;
private int speed;
static const int HIGH = 3;
static const int MEDIUM = 2;
static const int LOW = 1;
static const int OFF = 0;
this(string location)
{
this.location = location;
}
void high()
{
speed = HIGH;
writeln(location ~ " ceiling fan is on high");
}
void medium()
{
speed = MEDIUM;
writeln(location ~ " ceiling fan is on medium");
}
void low()
{
speed = LOW;
writeln(location ~ " ceiling fan is on low");
}
void off()
{
speed = OFF;
writeln(location ~ " ceiling fan is off");
}
int getSpeed()
{
return speed;
}
}

View File

@ -0,0 +1,41 @@
module command.remoteundostatus.ceilingfanhighcommand;
import command.remoteundostatus.command;
import command.remoteundostatus.ceilingfan;
class CeilingFanHighCommand : Command
{
CeilingFan ceilingFan;
int prevSpeed;
this(CeilingFan ceilingFan)
{
this.ceilingFan = ceilingFan;
}
void execute()
{
prevSpeed = ceilingFan.getSpeed();
ceilingFan.high();
}
void undo()
{
switch (prevSpeed)
{
case CeilingFan.HIGH:
ceilingFan.high();
break;
case CeilingFan.MEDIUM:
ceilingFan.medium();
break;
case CeilingFan.LOW:
ceilingFan.low();
break;
case CeilingFan.OFF:
ceilingFan.off();
break;
default: break;
}
}
}

View File

@ -0,0 +1,41 @@
module command.remoteundostatus.ceilingfanlowcommand;
import command.remoteundostatus.command;
import command.remoteundostatus.ceilingfan;
class CeilingFanLowCommand : Command
{
CeilingFan ceilingFan;
int prevSpeed;
this(CeilingFan ceilingFan)
{
this.ceilingFan = ceilingFan;
}
void execute()
{
prevSpeed = ceilingFan.getSpeed();
ceilingFan.low();
}
void undo()
{
switch (prevSpeed)
{
case CeilingFan.HIGH:
ceilingFan.high();
break;
case CeilingFan.MEDIUM:
ceilingFan.medium();
break;
case CeilingFan.LOW:
ceilingFan.low();
break;
case CeilingFan.OFF:
ceilingFan.off();
break;
default: break;
}
}
}

View File

@ -0,0 +1,41 @@
module command.remoteundostatus.ceilingfanmediumcommand;
import command.remoteundostatus.command;
import command.remoteundostatus.ceilingfan;
class CeilingFanMediumCommand : Command
{
CeilingFan ceilingFan;
int prevSpeed;
this(CeilingFan ceilingFan)
{
this.ceilingFan = ceilingFan;
}
void execute()
{
prevSpeed = ceilingFan.getSpeed();
ceilingFan.medium();
}
void undo()
{
switch (prevSpeed)
{
case CeilingFan.HIGH:
ceilingFan.high();
break;
case CeilingFan.MEDIUM:
ceilingFan.medium();
break;
case CeilingFan.LOW:
ceilingFan.low();
break;
case CeilingFan.OFF:
ceilingFan.off();
break;
default: break;
}
}
}

View File

@ -0,0 +1,41 @@
module command.remoteundostatus.ceilingfanoffcommand;
import command.remoteundostatus.command;
import command.remoteundostatus.ceilingfan;
class CeilingFanOffCommand : Command
{
CeilingFan ceilingFan;
int prevSpeed;
this(CeilingFan ceilingFan)
{
this.ceilingFan = ceilingFan;
}
void execute()
{
prevSpeed = ceilingFan.getSpeed();
ceilingFan.off();
}
void undo()
{
switch (prevSpeed)
{
case CeilingFan.HIGH:
ceilingFan.high();
break;
case CeilingFan.MEDIUM:
ceilingFan.medium();
break;
case CeilingFan.LOW:
ceilingFan.low();
break;
case CeilingFan.OFF:
ceilingFan.off();
break;
default: break;
}
}
}

View File

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

View File

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

View File

@ -0,0 +1,24 @@
module command.remoteundostatus.garagedoordowncommand;
import command.remoteundostatus.command;
import command.remoteundostatus.garagedoor;
class GarageDoorDownCommand : Command
{
GarageDoor garageDoor;
this(GarageDoor garageDoor)
{
this.garageDoor = garageDoor;
}
void execute()
{
garageDoor.down();
}
void undo()
{
garageDoor.up();
}
}

View File

@ -0,0 +1,24 @@
module command.remoteundostatus.garagedoorupcommand;
import command.remoteundostatus.command;
import command.remoteundostatus.garagedoor;
class GarageDoorUpCommand : Command
{
GarageDoor garageDoor;
this(GarageDoor garageDoor)
{
this.garageDoor = garageDoor;
}
void execute()
{
garageDoor.up();
}
void undo()
{
garageDoor.down();
}
}

View File

@ -0,0 +1,23 @@
module command.remoteundostatus.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.remoteundostatus.lightoffcommand;
import command.remoteundostatus.command;
import command.remoteundostatus.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.remoteundostatus.lightoncommand;
import command.remoteundostatus.command;
import command.remoteundostatus.light;
class LightOnCommand : Command
{
Light light;
this(Light light)
{
this.light = light;
}
void execute()
{
light.on();
}
void undo()
{
light.off();
}
}

Some files were not shown because too many files have changed in this diff Show More