From 8bfa03ce827d64977579dd1f7e3ae93e190fdb61 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sat, 12 Nov 2022 22:47:14 +0300 Subject: [PATCH] factory method --- factorymethod/README.md | 2 + factorymethod/pizzafactorymethod/app.d | 37 ++++++++++++ .../pizzafactorymethod/chicagopizzastore.d | 35 ++++++++++++ .../chicagostylecheesepizza.d | 21 +++++++ .../chicagostyleclampizza.d | 22 +++++++ .../chicagostylepepperonipizza.d | 25 ++++++++ .../chicagostyleveggiepizza.d | 24 ++++++++ .../pizzafactorymethod/nypizzastore.d | 35 ++++++++++++ .../pizzafactorymethod/nystylecheesepizza.d | 15 +++++ .../pizzafactorymethod/nystyleclampizza.d | 16 ++++++ .../nystylepepperonipizza.d | 20 +++++++ .../pizzafactorymethod/nystyleveggiepizza.d | 19 +++++++ factorymethod/pizzafactorymethod/pizza.d | 57 +++++++++++++++++++ factorymethod/pizzafactorymethod/pizzastore.d | 23 ++++++++ 14 files changed, 351 insertions(+) create mode 100644 factorymethod/pizzafactorymethod/app.d create mode 100644 factorymethod/pizzafactorymethod/chicagopizzastore.d create mode 100644 factorymethod/pizzafactorymethod/chicagostylecheesepizza.d create mode 100644 factorymethod/pizzafactorymethod/chicagostyleclampizza.d create mode 100644 factorymethod/pizzafactorymethod/chicagostylepepperonipizza.d create mode 100644 factorymethod/pizzafactorymethod/chicagostyleveggiepizza.d create mode 100644 factorymethod/pizzafactorymethod/nypizzastore.d create mode 100644 factorymethod/pizzafactorymethod/nystylecheesepizza.d create mode 100644 factorymethod/pizzafactorymethod/nystyleclampizza.d create mode 100644 factorymethod/pizzafactorymethod/nystylepepperonipizza.d create mode 100644 factorymethod/pizzafactorymethod/nystyleveggiepizza.d create mode 100644 factorymethod/pizzafactorymethod/pizza.d create mode 100644 factorymethod/pizzafactorymethod/pizzastore.d diff --git a/factorymethod/README.md b/factorymethod/README.md index 658a931..8d6da2d 100644 --- a/factorymethod/README.md +++ b/factorymethod/README.md @@ -2,6 +2,8 @@ Порождающий паттерн проектирования, который определяет общий интерфейс для создания объектов в суперклассе, позволяя подклассам изменять тип создаваемых объектов. +Паттерн **Фабричный Метод** определяет интерфейс создания объекта, но позволяет субклассам выбрать создаваемый экземпляр. Таким образом, Фабричный Метод делегирует операцию создания экземпляра субклассам. + ## Принципы - По возможности использовать абстракции в коде diff --git a/factorymethod/pizzafactorymethod/app.d b/factorymethod/pizzafactorymethod/app.d new file mode 100644 index 0000000..2516e75 --- /dev/null +++ b/factorymethod/pizzafactorymethod/app.d @@ -0,0 +1,37 @@ +module factorymethod.pizzafactorymethod.app; + +import factorymethod.pizzafactorymethod.pizza; +import factorymethod.pizzafactorymethod.pizzastore; +import factorymethod.pizzafactorymethod.nypizzastore; +import factorymethod.pizzafactorymethod.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.getName()); + + pizza = chicagoStore.orderPizza("cheese"); + writeln("Joel ordered a ", pizza.getName()); + + pizza = nyStore.orderPizza("clam"); + writeln("Ethan ordered a ", pizza.getName()); + + pizza = chicagoStore.orderPizza("clam"); + writeln("Joel ordered a ", pizza.getName()); + + pizza = nyStore.orderPizza("pepperoni"); + writeln("Ethan ordered a ", pizza.getName()); + + pizza = chicagoStore.orderPizza("pepperoni"); + writeln("Joel ordered a ", pizza.getName()); + + pizza = nyStore.orderPizza("veggie"); + writeln("Ethan ordered a ", pizza.getName()); + + pizza = chicagoStore.orderPizza("veggie"); + writeln("Joel ordered a ", pizza.getName()); +} diff --git a/factorymethod/pizzafactorymethod/chicagopizzastore.d b/factorymethod/pizzafactorymethod/chicagopizzastore.d new file mode 100644 index 0000000..a3de9d7 --- /dev/null +++ b/factorymethod/pizzafactorymethod/chicagopizzastore.d @@ -0,0 +1,35 @@ +module factorymethod.pizzafactorymethod.chicagopizzastore; + +import factorymethod.pizzafactorymethod.pizza; +import factorymethod.pizzafactorymethod.pizzastore; +import factorymethod.pizzafactorymethod.chicagostylecheesepizza; +import factorymethod.pizzafactorymethod.chicagostyleclampizza; +import factorymethod.pizzafactorymethod.chicagostylepepperonipizza; +import factorymethod.pizzafactorymethod.chicagostyleveggiepizza; + +class ChicagoPizzaStore : PizzaStore +{ + override Pizza createPizza(string item) + { + if (item == "cheese") + { + return new ChicagoStyleCheesePizza(); + } + else if (item == "veggie") + { + return new ChicagoStyleVeggiePizza(); + } + else if (item == "clam") + { + return new ChicagoStyleClamPizza(); + } + else if (item == "pepperoni") + { + return new ChicagoStylePepperoniPizza(); + } + else + { + return null; + } + } +} diff --git a/factorymethod/pizzafactorymethod/chicagostylecheesepizza.d b/factorymethod/pizzafactorymethod/chicagostylecheesepizza.d new file mode 100644 index 0000000..45d5d69 --- /dev/null +++ b/factorymethod/pizzafactorymethod/chicagostylecheesepizza.d @@ -0,0 +1,21 @@ +module factorymethod.pizzafactorymethod.chicagostylecheesepizza; + +import factorymethod.pizzafactorymethod.pizza; +import std.stdio : writeln; + +class ChicagoStyleCheesePizza : Pizza +{ + this() + { + name = "Chicago Style Deep Dish Cheese Pizza"; + dough = "Extra Thick Crust Dough"; + sauce = "Plum Tomato Sauce"; + + toppings ~= "Shredded Mozzarella Cheese"; + } + + override void cut() + { + writeln("Cutting the pizza into square slices"); + } +} diff --git a/factorymethod/pizzafactorymethod/chicagostyleclampizza.d b/factorymethod/pizzafactorymethod/chicagostyleclampizza.d new file mode 100644 index 0000000..fe1623f --- /dev/null +++ b/factorymethod/pizzafactorymethod/chicagostyleclampizza.d @@ -0,0 +1,22 @@ +module factorymethod.pizzafactorymethod.chicagostyleclampizza; + +import factorymethod.pizzafactorymethod.pizza; +import std.stdio : writeln; + +class ChicagoStyleClamPizza : Pizza +{ + this() + { + name = "Chicago Style Clam Pizza"; + dough = "Extra Thick Crust Dough"; + sauce = "Plum Tomato Sauce"; + + toppings ~= "Shredded Mozzarella Cheese"; + toppings ~= "Frozen Clams from Chesapeake Bay"; + } + + override void cut() + { + writeln("Cutting the pizza into square slices"); + } +} diff --git a/factorymethod/pizzafactorymethod/chicagostylepepperonipizza.d b/factorymethod/pizzafactorymethod/chicagostylepepperonipizza.d new file mode 100644 index 0000000..5f8c1a1 --- /dev/null +++ b/factorymethod/pizzafactorymethod/chicagostylepepperonipizza.d @@ -0,0 +1,25 @@ +module factorymethod.pizzafactorymethod.chicagostylepepperonipizza; + +import factorymethod.pizzafactorymethod.pizza; +import std.stdio : writeln; + +class ChicagoStylePepperoniPizza : Pizza +{ + this() + { + name = "Chicago Style Pepperoni Pizza"; + dough = "Extra Thick Crust Dough"; + sauce = "Plum Tomato Sauce"; + + toppings ~= "Shredded Mozzarella Cheese"; + toppings ~= "Black Olives"; + toppings ~= "Spinach"; + toppings ~= "Eggplant"; + toppings ~= "Sliced Pepperoni"; + } + + override void cut() + { + writeln("Cutting the pizza into square slices"); + } +} diff --git a/factorymethod/pizzafactorymethod/chicagostyleveggiepizza.d b/factorymethod/pizzafactorymethod/chicagostyleveggiepizza.d new file mode 100644 index 0000000..625ed98 --- /dev/null +++ b/factorymethod/pizzafactorymethod/chicagostyleveggiepizza.d @@ -0,0 +1,24 @@ +module factorymethod.pizzafactorymethod.chicagostyleveggiepizza; + +import factorymethod.pizzafactorymethod.pizza; +import std.stdio : writeln; + +class ChicagoStyleVeggiePizza : Pizza +{ + this() + { + name = "Chicago Deep Dish Veggie Pizza"; + dough = "Extra Thick Crust Dough"; + sauce = "Plum Tomato Sauce"; + + toppings ~= "Shredded Mozzarella Cheese"; + toppings ~= "Black Olives"; + toppings ~= "Spinach"; + toppings ~= "Eggplant"; + } + + override void cut() + { + writeln("Cutting the pizza into square slices"); + } +} diff --git a/factorymethod/pizzafactorymethod/nypizzastore.d b/factorymethod/pizzafactorymethod/nypizzastore.d new file mode 100644 index 0000000..570ae81 --- /dev/null +++ b/factorymethod/pizzafactorymethod/nypizzastore.d @@ -0,0 +1,35 @@ +module factorymethod.pizzafactorymethod.nypizzastore; + +import factorymethod.pizzafactorymethod.pizza; +import factorymethod.pizzafactorymethod.pizzastore; +import factorymethod.pizzafactorymethod.nystylecheesepizza; +import factorymethod.pizzafactorymethod.nystyleclampizza; +import factorymethod.pizzafactorymethod.nystylepepperonipizza; +import factorymethod.pizzafactorymethod.nystyleveggiepizza; + +class NYPizzaStore : PizzaStore +{ + override Pizza createPizza(string item) + { + if (item == "cheese") + { + return new NYStyleCheesePizza(); + } + else if (item == "veggie") + { + return new NYStyleVeggiePizza(); + } + else if (item == "clam") + { + return new NYStyleClamPizza(); + } + else if (item == "pepperoni") + { + return new NYStylePepperoniPizza(); + } + else + { + return null; + } + } +} diff --git a/factorymethod/pizzafactorymethod/nystylecheesepizza.d b/factorymethod/pizzafactorymethod/nystylecheesepizza.d new file mode 100644 index 0000000..c4a3247 --- /dev/null +++ b/factorymethod/pizzafactorymethod/nystylecheesepizza.d @@ -0,0 +1,15 @@ +module factorymethod.pizzafactorymethod.nystylecheesepizza; + +import factorymethod.pizzafactorymethod.pizza; + +class NYStyleCheesePizza : Pizza +{ + this() + { + name = "NY Style Sauce and Cheese Pizza"; + dough = "Thin Crust Dough"; + sauce = "Marinara Sauce"; + + toppings ~= "Grated Reggiano Cheese"; + } +} diff --git a/factorymethod/pizzafactorymethod/nystyleclampizza.d b/factorymethod/pizzafactorymethod/nystyleclampizza.d new file mode 100644 index 0000000..4e69d07 --- /dev/null +++ b/factorymethod/pizzafactorymethod/nystyleclampizza.d @@ -0,0 +1,16 @@ +module factorymethod.pizzafactorymethod.nystyleclampizza; + +import factorymethod.pizzafactorymethod.pizza; + +class NYStyleClamPizza : Pizza +{ + this() + { + name = "NY Style Clam Pizza"; + dough = "Thin Crust Dough"; + sauce = "Marinara Sauce"; + + toppings ~= "Grated Reggiano Cheese"; + toppings ~= "Fresh Clams from Long Island Sound"; + } +} diff --git a/factorymethod/pizzafactorymethod/nystylepepperonipizza.d b/factorymethod/pizzafactorymethod/nystylepepperonipizza.d new file mode 100644 index 0000000..98c0e4d --- /dev/null +++ b/factorymethod/pizzafactorymethod/nystylepepperonipizza.d @@ -0,0 +1,20 @@ +module factorymethod.pizzafactorymethod.nystylepepperonipizza; + +import factorymethod.pizzafactorymethod.pizza; + +class NYStylePepperoniPizza : Pizza +{ + this() + { + name = "NY Style Pepperoni Pizza"; + dough = "Thin Crust Dough"; + sauce = "Marinara Sauce"; + + toppings ~= "Grated Reggiano Cheese"; + toppings ~= "Sliced Pepperoni"; + toppings ~= "Garlic"; + toppings ~= "Onion"; + toppings ~= "Mushrooms"; + toppings ~= "Red Pepper"; + } +} diff --git a/factorymethod/pizzafactorymethod/nystyleveggiepizza.d b/factorymethod/pizzafactorymethod/nystyleveggiepizza.d new file mode 100644 index 0000000..8a38a59 --- /dev/null +++ b/factorymethod/pizzafactorymethod/nystyleveggiepizza.d @@ -0,0 +1,19 @@ +module factorymethod.pizzafactorymethod.nystyleveggiepizza; + +import factorymethod.pizzafactorymethod.pizza; + +class NYStyleVeggiePizza : Pizza +{ + this() + { + name = "NY Style Veggie Pizza"; + dough = "Thin Crust Dough"; + sauce = "Marinara Sauce"; + + toppings ~= "Grated Reggiano Cheese"; + toppings ~= "Garlic"; + toppings ~= "Onion"; + toppings ~= "Mushrooms"; + toppings ~= "Red Pepper"; + } +} diff --git a/factorymethod/pizzafactorymethod/pizza.d b/factorymethod/pizzafactorymethod/pizza.d new file mode 100644 index 0000000..86c15b7 --- /dev/null +++ b/factorymethod/pizzafactorymethod/pizza.d @@ -0,0 +1,57 @@ +module factorymethod.pizzafactorymethod.pizza; + +import std.stdio : writeln; + +abstract class Pizza +{ +protected: + string name; + string dough; + string sauce; + string[] toppings; +public: + string getName() + { + return name; + } + + void prepare() + { + writeln("Prepare " ~ name); + writeln("Tossing dough..."); + writeln("Adding sauce..."); + writeln("Adding toppings: "); + foreach (val; toppings) + { + writeln(" " ~ val); + } + } + + 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"); + } + + override string toString() const @safe pure nothrow + { + string s; + s ~= "---- " ~ name ~ " ----\n"; + s ~= dough ~ "\n"; + s ~= sauce ~ "\n"; + foreach (val; toppings) + { + s ~= val ~ '\n'; + } + return s; + } +} diff --git a/factorymethod/pizzafactorymethod/pizzastore.d b/factorymethod/pizzafactorymethod/pizzastore.d new file mode 100644 index 0000000..4b91319 --- /dev/null +++ b/factorymethod/pizzafactorymethod/pizzastore.d @@ -0,0 +1,23 @@ +module factorymethod.pizzafactorymethod.pizzastore; + +import factorymethod.pizzafactorymethod.pizza; +import std.stdio : writeln; + +class PizzaStore +{ + abstract Pizza createPizza(string type); + + Pizza orderPizza(string type) + { + Pizza pizza = createPizza(type); + + writeln("--- Making a " ~ pizza.getName() ~ " ---"); + + pizza.prepare(); + pizza.bake(); + pizza.cut(); + pizza.box(); + + return pizza; + } +}