From 0aabb39e790d71a819d4afc6ae589eb15de49e99 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 13 Nov 2022 03:59:35 +0300 Subject: [PATCH] abstract factory --- README.md | 3 +- abstractfactory/README.md | 9 ++ abstractfactory/app.d | 37 ++++++++ abstractfactory/blackolives.d | 11 +++ abstractfactory/cheese.d | 9 ++ abstractfactory/cheesepizza.d | 23 +++++ .../chicagopizzaingredientfactory.d | 51 +++++++++++ abstractfactory/chicagopizzastore.d | 43 +++++++++ abstractfactory/clampizza.d | 24 +++++ abstractfactory/clams.d | 9 ++ abstractfactory/dough.d | 9 ++ abstractfactory/eggplant.d | 11 +++ abstractfactory/freshclams.d | 11 +++ abstractfactory/frozenclams.d | 11 +++ abstractfactory/garlic.d | 11 +++ abstractfactory/marinarasauce.d | 11 +++ abstractfactory/mozzarellacheese.d | 11 +++ abstractfactory/mushroom.d | 11 +++ abstractfactory/nypizzaingredientfactory.d | 52 +++++++++++ abstractfactory/nypizzastore.d | 43 +++++++++ abstractfactory/onion.d | 11 +++ abstractfactory/pepperoni.d | 9 ++ abstractfactory/pepperonipizza.d | 25 ++++++ abstractfactory/pizza.d | 88 +++++++++++++++++++ abstractfactory/pizzaingredientfactory.d | 18 ++++ abstractfactory/pizzastore.d | 24 +++++ abstractfactory/plumtomatosauce.d | 11 +++ abstractfactory/redpepper.d | 11 +++ abstractfactory/reggianocheese.d | 11 +++ abstractfactory/sauce.d | 9 ++ abstractfactory/slicedpepperoni.d | 11 +++ abstractfactory/spinach.d | 11 +++ abstractfactory/thincrustdough.d | 16 ++++ abstractfactory/veggiepizza.d | 24 +++++ abstractfactory/veggies.d | 9 ++ factorymethod/README.md | 2 +- 36 files changed, 688 insertions(+), 2 deletions(-) create mode 100644 abstractfactory/README.md create mode 100644 abstractfactory/app.d create mode 100644 abstractfactory/blackolives.d create mode 100644 abstractfactory/cheese.d create mode 100644 abstractfactory/cheesepizza.d create mode 100644 abstractfactory/chicagopizzaingredientfactory.d create mode 100644 abstractfactory/chicagopizzastore.d create mode 100644 abstractfactory/clampizza.d create mode 100644 abstractfactory/clams.d create mode 100644 abstractfactory/dough.d create mode 100644 abstractfactory/eggplant.d create mode 100644 abstractfactory/freshclams.d create mode 100644 abstractfactory/frozenclams.d create mode 100644 abstractfactory/garlic.d create mode 100644 abstractfactory/marinarasauce.d create mode 100644 abstractfactory/mozzarellacheese.d create mode 100644 abstractfactory/mushroom.d create mode 100644 abstractfactory/nypizzaingredientfactory.d create mode 100644 abstractfactory/nypizzastore.d create mode 100644 abstractfactory/onion.d create mode 100644 abstractfactory/pepperoni.d create mode 100644 abstractfactory/pepperonipizza.d create mode 100644 abstractfactory/pizza.d create mode 100644 abstractfactory/pizzaingredientfactory.d create mode 100644 abstractfactory/pizzastore.d create mode 100644 abstractfactory/plumtomatosauce.d create mode 100644 abstractfactory/redpepper.d create mode 100644 abstractfactory/reggianocheese.d create mode 100644 abstractfactory/sauce.d create mode 100644 abstractfactory/slicedpepperoni.d create mode 100644 abstractfactory/spinach.d create mode 100644 abstractfactory/thincrustdough.d create mode 100644 abstractfactory/veggiepizza.d create mode 100644 abstractfactory/veggies.d diff --git a/README.md b/README.md index dab9997..6a07d7c 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ ### Пораждающие -1. [Фабричный метод](factorymethod/) +1. [Фабричный метод (Простая фабрика)](factorymethod/) +2. [Абстрактная фабрика](abstractfactory/) ## Компиляция diff --git a/abstractfactory/README.md b/abstractfactory/README.md new file mode 100644 index 0000000..9147003 --- /dev/null +++ b/abstractfactory/README.md @@ -0,0 +1,9 @@ +# Абстрактная фабрика + +Порождающий паттерн проектирования, который позволяет создавать семейства связанных объектов, не привязываясь к конкретным классам создаваемых объектов. + +Паттерн **Абстрактная Фабрика** предоставляет интерфейс создания семейств взаимосвязанных или взаимозависимых объектов без указания их конкретных классов. + +## Принципы + +- Код должен зависеть от абстракций, а не от конкретных классов diff --git a/abstractfactory/app.d b/abstractfactory/app.d new file mode 100644 index 0000000..e76c91a --- /dev/null +++ b/abstractfactory/app.d @@ -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); +} diff --git a/abstractfactory/blackolives.d b/abstractfactory/blackolives.d new file mode 100644 index 0000000..710e35a --- /dev/null +++ b/abstractfactory/blackolives.d @@ -0,0 +1,11 @@ +module abstractfactory.blackolives; + +import abstractfactory.veggies; + +class BlackOlives : Veggies +{ + override string toString() const @safe pure nothrow + { + return "Black Olives"; + } +} diff --git a/abstractfactory/cheese.d b/abstractfactory/cheese.d new file mode 100644 index 0000000..107f845 --- /dev/null +++ b/abstractfactory/cheese.d @@ -0,0 +1,9 @@ +module abstractfactory.cheese; + +interface Cheese +{ + string opBinary(string op : "~")(string s) const + { + return (cast(Object)this).toString() ~ s; + } +} diff --git a/abstractfactory/cheesepizza.d b/abstractfactory/cheesepizza.d new file mode 100644 index 0000000..28334d1 --- /dev/null +++ b/abstractfactory/cheesepizza.d @@ -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(); + } +} diff --git a/abstractfactory/chicagopizzaingredientfactory.d b/abstractfactory/chicagopizzaingredientfactory.d new file mode 100644 index 0000000..b51735f --- /dev/null +++ b/abstractfactory/chicagopizzaingredientfactory.d @@ -0,0 +1,51 @@ +module abstractfactory.chicagopizzaingredientfactory; + +import abstractfactory.pizzaingredientfactory; +import abstractfactory.dough; +import abstractfactory.thincrustdough; +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(); + } +} diff --git a/abstractfactory/chicagopizzastore.d b/abstractfactory/chicagopizzastore.d new file mode 100644 index 0000000..897712e --- /dev/null +++ b/abstractfactory/chicagopizzastore.d @@ -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; + } +} diff --git a/abstractfactory/clampizza.d b/abstractfactory/clampizza.d new file mode 100644 index 0000000..fb4eab4 --- /dev/null +++ b/abstractfactory/clampizza.d @@ -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(); + } +} diff --git a/abstractfactory/clams.d b/abstractfactory/clams.d new file mode 100644 index 0000000..0d79ec4 --- /dev/null +++ b/abstractfactory/clams.d @@ -0,0 +1,9 @@ +module abstractfactory.clams; + +interface Clams +{ + string opBinary(string op : "~")(string s) const + { + return (cast(Object)this).toString() ~ s; + } +} diff --git a/abstractfactory/dough.d b/abstractfactory/dough.d new file mode 100644 index 0000000..d6f28d5 --- /dev/null +++ b/abstractfactory/dough.d @@ -0,0 +1,9 @@ +module abstractfactory.dough; + +interface Dough +{ + string opBinary(string op : "~")(string s) const + { + return (cast(Object)this).toString() ~ s; + } +} diff --git a/abstractfactory/eggplant.d b/abstractfactory/eggplant.d new file mode 100644 index 0000000..4eb6974 --- /dev/null +++ b/abstractfactory/eggplant.d @@ -0,0 +1,11 @@ +module abstractfactory.eggplant; + +import abstractfactory.veggies; + +class Eggplant : Veggies +{ + override string toString() const @safe pure nothrow + { + return "Eggplant"; + } +} diff --git a/abstractfactory/freshclams.d b/abstractfactory/freshclams.d new file mode 100644 index 0000000..28fb1dd --- /dev/null +++ b/abstractfactory/freshclams.d @@ -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"; + } +} diff --git a/abstractfactory/frozenclams.d b/abstractfactory/frozenclams.d new file mode 100644 index 0000000..09431af --- /dev/null +++ b/abstractfactory/frozenclams.d @@ -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"; + } +} diff --git a/abstractfactory/garlic.d b/abstractfactory/garlic.d new file mode 100644 index 0000000..0bdf72a --- /dev/null +++ b/abstractfactory/garlic.d @@ -0,0 +1,11 @@ +module abstractfactory.garlic; + +import abstractfactory.veggies; + +class Garlic : Veggies +{ + override string toString() const @safe pure nothrow + { + return "Garlic"; + } +} diff --git a/abstractfactory/marinarasauce.d b/abstractfactory/marinarasauce.d new file mode 100644 index 0000000..39754d3 --- /dev/null +++ b/abstractfactory/marinarasauce.d @@ -0,0 +1,11 @@ +module abstractfactory.marinarasauce; + +import abstractfactory.sauce; + +class MarinaraSauce : Sauce +{ + override string toString() const @safe pure nothrow + { + return "Marinara Sauce"; + } +} diff --git a/abstractfactory/mozzarellacheese.d b/abstractfactory/mozzarellacheese.d new file mode 100644 index 0000000..20aa408 --- /dev/null +++ b/abstractfactory/mozzarellacheese.d @@ -0,0 +1,11 @@ +module abstractfactory.mozzarellacheese; + +import abstractfactory.cheese; + +class MozzarellaCheese : Cheese +{ + override string toString() const @safe pure nothrow + { + return "Shredded Mozzarella"; + } +} diff --git a/abstractfactory/mushroom.d b/abstractfactory/mushroom.d new file mode 100644 index 0000000..ce53b44 --- /dev/null +++ b/abstractfactory/mushroom.d @@ -0,0 +1,11 @@ +module abstractfactory.mushroom; + +import abstractfactory.veggies; + +class Mushroom : Veggies +{ + override string toString() const @safe pure nothrow + { + return "Mushroom"; + } +} diff --git a/abstractfactory/nypizzaingredientfactory.d b/abstractfactory/nypizzaingredientfactory.d new file mode 100644 index 0000000..2017e20 --- /dev/null +++ b/abstractfactory/nypizzaingredientfactory.d @@ -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(); + } +} diff --git a/abstractfactory/nypizzastore.d b/abstractfactory/nypizzastore.d new file mode 100644 index 0000000..76a012b --- /dev/null +++ b/abstractfactory/nypizzastore.d @@ -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; + } +} diff --git a/abstractfactory/onion.d b/abstractfactory/onion.d new file mode 100644 index 0000000..0c14829 --- /dev/null +++ b/abstractfactory/onion.d @@ -0,0 +1,11 @@ +module abstractfactory.onion; + +import abstractfactory.veggies; + +class Onion : Veggies +{ + override string toString() const @safe pure nothrow + { + return "Onion"; + } +} diff --git a/abstractfactory/pepperoni.d b/abstractfactory/pepperoni.d new file mode 100644 index 0000000..9f94003 --- /dev/null +++ b/abstractfactory/pepperoni.d @@ -0,0 +1,9 @@ +module abstractfactory.pepperoni; + +interface Pepperoni +{ + string opBinary(string op : "~")(string s) const + { + return (cast(Object)this).toString() ~ s; + } +} \ No newline at end of file diff --git a/abstractfactory/pepperonipizza.d b/abstractfactory/pepperonipizza.d new file mode 100644 index 0000000..192c9d9 --- /dev/null +++ b/abstractfactory/pepperonipizza.d @@ -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(); + } +} diff --git a/abstractfactory/pizza.d b/abstractfactory/pizza.d new file mode 100644 index 0000000..d7cdfaa --- /dev/null +++ b/abstractfactory/pizza.d @@ -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; + } +} diff --git a/abstractfactory/pizzaingredientfactory.d b/abstractfactory/pizzaingredientfactory.d new file mode 100644 index 0000000..050fca6 --- /dev/null +++ b/abstractfactory/pizzaingredientfactory.d @@ -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(); +} diff --git a/abstractfactory/pizzastore.d b/abstractfactory/pizzastore.d new file mode 100644 index 0000000..315f0b2 --- /dev/null +++ b/abstractfactory/pizzastore.d @@ -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; + } +} diff --git a/abstractfactory/plumtomatosauce.d b/abstractfactory/plumtomatosauce.d new file mode 100644 index 0000000..e1d857f --- /dev/null +++ b/abstractfactory/plumtomatosauce.d @@ -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"; + } +} diff --git a/abstractfactory/redpepper.d b/abstractfactory/redpepper.d new file mode 100644 index 0000000..1b5ace2 --- /dev/null +++ b/abstractfactory/redpepper.d @@ -0,0 +1,11 @@ +module abstractfactory.redpepper; + +import abstractfactory.veggies; + +class RedPepper : Veggies +{ + override string toString() const @safe pure nothrow + { + return "Red Pepper"; + } +} diff --git a/abstractfactory/reggianocheese.d b/abstractfactory/reggianocheese.d new file mode 100644 index 0000000..f999d37 --- /dev/null +++ b/abstractfactory/reggianocheese.d @@ -0,0 +1,11 @@ +module abstractfactory.reggianocheese; + +import abstractfactory.cheese; + +class ReggianoCheese : Cheese +{ + override string toString() const @safe pure nothrow + { + return "Reggiano Cheese"; + } +} diff --git a/abstractfactory/sauce.d b/abstractfactory/sauce.d new file mode 100644 index 0000000..5d447d1 --- /dev/null +++ b/abstractfactory/sauce.d @@ -0,0 +1,9 @@ +module abstractfactory.sauce; + +interface Sauce +{ + string opBinary(string op : "~")(string s) const + { + return (cast(Object)this).toString() ~ s; + } +} \ No newline at end of file diff --git a/abstractfactory/slicedpepperoni.d b/abstractfactory/slicedpepperoni.d new file mode 100644 index 0000000..becc98a --- /dev/null +++ b/abstractfactory/slicedpepperoni.d @@ -0,0 +1,11 @@ +module abstractfactory.slicedpepperoni; + +import abstractfactory.pepperoni; + +class SlicedPepperoni : Pepperoni +{ + override string toString() const @safe pure nothrow + { + return "Sliced Pepperoni"; + } +} diff --git a/abstractfactory/spinach.d b/abstractfactory/spinach.d new file mode 100644 index 0000000..4385f5a --- /dev/null +++ b/abstractfactory/spinach.d @@ -0,0 +1,11 @@ +module abstractfactory.spinach; + +import abstractfactory.veggies; + +class Spinach : Veggies +{ + override string toString() const @safe pure nothrow + { + return "Spinach"; + } +} diff --git a/abstractfactory/thincrustdough.d b/abstractfactory/thincrustdough.d new file mode 100644 index 0000000..ca854e3 --- /dev/null +++ b/abstractfactory/thincrustdough.d @@ -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; + } +} diff --git a/abstractfactory/veggiepizza.d b/abstractfactory/veggiepizza.d new file mode 100644 index 0000000..ae40a9c --- /dev/null +++ b/abstractfactory/veggiepizza.d @@ -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(); + } +} diff --git a/abstractfactory/veggies.d b/abstractfactory/veggies.d new file mode 100644 index 0000000..20be41e --- /dev/null +++ b/abstractfactory/veggies.d @@ -0,0 +1,9 @@ +module abstractfactory.veggies; + +interface Veggies +{ + string opBinary(string op : "~")(string s) const + { + return (cast(Object)this).toString() ~ s; + } +} diff --git a/factorymethod/README.md b/factorymethod/README.md index 8d6da2d..7eea2cc 100644 --- a/factorymethod/README.md +++ b/factorymethod/README.md @@ -6,4 +6,4 @@ ## Принципы -- По возможности использовать абстракции в коде +- Код должен зависеть от абстракций, а не от конкретных классов