factory method

This commit is contained in:
Alexander Zhirov 2022-11-12 22:47:14 +03:00
parent f9c95af5ff
commit 8bfa03ce82
14 changed files with 351 additions and 0 deletions

View File

@ -2,6 +2,8 @@
Порождающий паттерн проектирования, который определяет общий интерфейс для создания объектов в суперклассе, позволяя подклассам изменять тип создаваемых объектов. Порождающий паттерн проектирования, который определяет общий интерфейс для создания объектов в суперклассе, позволяя подклассам изменять тип создаваемых объектов.
Паттерн **Фабричный Метод** определяет интерфейс создания объекта, но позволяет субклассам выбрать создаваемый экземпляр. Таким образом, Фабричный Метод делегирует операцию создания экземпляра субклассам.
## Принципы ## Принципы
- По возможности использовать абстракции в коде - По возможности использовать абстракции в коде

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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