Compare commits

..

2 Commits

Author SHA1 Message Date
Alexander Zhirov 5034db3d00 edit readme 2021-11-03 18:40:14 +03:00
Alexander Zhirov a44ab07735 factory without ingredients 2021-11-02 21:18:09 +03:00
16 changed files with 481 additions and 0 deletions

View File

@ -0,0 +1,7 @@
Паттерн "Фабричный Метод" определяет интерфейс создания объекта, но позволяет субклассам выбрать создаваемый экземпляр.
Все фабричные паттерны обеспечивают слабую связанность за счет сокращения зависимости приложения от конкретных классов.
Фабричный Метод основан на наследовании: создание объектов делегируется субклассам, реализующим фабричный метод для создания объектов. Задача Фабричного Метода - перемещение создания экземпляров в субклассы.
*Принцип:*
- По возможности использовать абстракции в коде.

View File

@ -0,0 +1,43 @@
/*
* ChicagoPizzaStore.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "PizzaStore.hpp"
#include "ChicagoStylePizza.hpp"
#include "ChicagoStyleCheesePizza.hpp"
#include "ChicagoStyleVeggiePizza.hpp"
#include "ChicagoStyleClamPizza.hpp"
#include "ChicagoStylePepperoniPizza.hpp"
class ChicagoPizzaStore: public PizzaStore
{
public:
Pizza createPizza(const std::string &type) const override
{
if (type == "cheese")
{
return ChicagoStyleCheesePizza();
}
else if (type == "veggie")
{
return ChicagoStyleVeggiePizza();
}
else if (type == "clam")
{
return ChicagoStyleClamPizza();
}
else if (type == "pepperoni")
{
return ChicagoStylePepperoniPizza();
}
else
{
return ChicagoStylePizza();
}
}
};

View File

@ -0,0 +1,28 @@
/*
* ChicagoStyleCheesePizza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class ChicagoStyleCheesePizza: public Pizza
{
public:
ChicagoStyleCheesePizza()
{
setName("Chicago Style Deep Dish Cheese Pizza");
setDough("Extra Thick Crust Dough");
setSauce("Plum Tomato Sauce");
addTopping("Shredded Mozzarella Cheese");
}
void cut() const override
{
std::cout << "Cutting the pizza into square slices" << std::endl;
}
};

View File

@ -0,0 +1,29 @@
/*
* ChicagoStyleClamPizza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class ChicagoStyleClamPizza: public Pizza
{
public:
ChicagoStyleClamPizza()
{
setName("Chicago Style Clam Pizza");
setDough("Extra Thick Crust Dough");
setSauce("Plum Tomato Sauce");
addTopping("Shredded Mozzarella Cheese");
addTopping("Frozen Clams from Chesapeake Bay");
}
void cut() const override
{
std::cout << "Cutting the pizza into square slices" << std::endl;
}
};

View File

@ -0,0 +1,32 @@
/*
* ChicagoStylePepperoniPizza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class ChicagoStylePepperoniPizza: public Pizza
{
public:
ChicagoStylePepperoniPizza()
{
setName("Chicago Style Pepperoni Pizza");
setDough("Extra Thick Crust Dough");
setSauce("Plum Tomato Sauce");
addTopping("Shredded Mozzarella Cheese");
addTopping("Black Olives");
addTopping("Spinach");
addTopping("Eggplant");
addTopping("Sliced Pepperoni");
}
void cut() const override
{
std::cout << "Cutting the pizza into square slices" << std::endl;
}
};

View File

@ -0,0 +1,14 @@
/*
* ChicagoStylePizza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class ChicagoStylePizza: public Pizza
{
};

View File

@ -0,0 +1,31 @@
/*
* ChicagoStyleVeggiePizza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class ChicagoStyleVeggiePizza: public Pizza
{
public:
ChicagoStyleVeggiePizza()
{
setName("Chicago Deep Dish Veggie Pizza");
setDough("Extra Thick Crust Dough");
setSauce("Plum Tomato Sauce");
addTopping("Shredded Mozzarella Cheese");
addTopping("Black Olives");
addTopping("Spinach");
addTopping("Eggplant");
}
void cut() const override
{
std::cout << "Cutting the pizza into square slices" << std::endl;
}
};

43
lesson_4/NYPizzaStore.hpp Normal file
View File

@ -0,0 +1,43 @@
/*
* NYPizzaStore.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "PizzaStore.hpp"
#include "NYStylePiza.hpp"
#include "NYStyleCheesePiza.hpp"
#include "NYStyleVeggiePizza.hpp"
#include "NYStyleClamPizza.hpp"
#include "NYStylePepperoniPizza.hpp"
class NYPizzaStore: public PizzaStore
{
public:
Pizza createPizza(const std::string &type) const override
{
if (type == "cheese")
{
return NYStyleCheesePiza();
}
else if (type == "veggie")
{
return NYStyleVeggiePizza();
}
else if (type == "clam")
{
return NYStyleClamPizza();
}
else if (type == "pepperoni")
{
return NYStylePepperoniPizza();
}
else
{
return NYStylePiza();
}
}
};

View File

@ -0,0 +1,23 @@
/*
* NYStyleCheesePiza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class NYStyleCheesePiza: public Pizza
{
public:
NYStyleCheesePiza()
{
setName("NY Style Sauce and Cheese Pizza");
setDough("Thin Crust Dough");
setSauce("Marinara Sauce");
addTopping("Grated Reggiano Cheese");
}
};

View File

@ -0,0 +1,24 @@
/*
* NYStyleClamPizza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class NYStyleClamPizza: public Pizza
{
public:
NYStyleClamPizza()
{
setName("NY Style Clam Pizza");
setDough("Thin Crust Dough");
setSauce("Marinara Sauce");
addTopping("Grated Reggiano Cheese");
addTopping("Fresh Clams from Long Island Sound");
}
};

View File

@ -0,0 +1,28 @@
/*
* NYStylePepperoniPizza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class NYStylePepperoniPizza: public Pizza
{
public:
NYStylePepperoniPizza()
{
setName("NY Style Pepperoni Pizza");
setDough("Thin Crust Dough");
setSauce("Marinara Sauce");
addTopping("Grated Reggiano Cheese");
addTopping("Sliced Pepperoni");
addTopping("Garlic");
addTopping("Onion");
addTopping("Mushrooms");
addTopping("Red Pepper");
}
};

14
lesson_4/NYStylePiza.hpp Normal file
View File

@ -0,0 +1,14 @@
/*
* NYStylePiza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class NYStylePiza: public Pizza
{
};

View File

@ -0,0 +1,27 @@
/*
* NYStyleVeggiePizza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class NYStyleVeggiePizza: public Pizza
{
public:
NYStyleVeggiePizza()
{
setName("NY Style Veggie Pizza");
setDough("Thin Crust Dough");
setSauce("Marinara Sauce");
addTopping("Grated Reggiano Cheese");
addTopping("Garlic");
addTopping("Onion");
addTopping("Mushrooms");
addTopping("Red Pepper");
}
};

75
lesson_4/Pizza.hpp Normal file
View File

@ -0,0 +1,75 @@
/*
* Pizza.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <list>
class Pizza
{
private:
std::string name;
std::string dough;
std::string sauce;
std::list<std::string> toppings;
public:
virtual void prepare() const
{
std::cout << "Preparing " << name << std::endl;
std::cout << "Tossing dough..." << std::endl;
std::cout << "Adding sauce..." << std::endl;
std::cout << "Adding toppings:" << std::endl;
for (const std::string &topping : toppings)
{
std::cout << '\t' << topping << std::endl;
}
}
virtual void bake() const
{
std::cout << "Bake for 25 minutes at 350" << std::endl;
}
virtual void cut() const
{
std::cout << "Cutting the pizza into diagonal slices" << std::endl;
}
virtual void box() const
{
std::cout << "Place pizza in official PizzaStore box" << std::endl;
}
virtual std::string getName() const
{
return name;
}
virtual void setName(std::string name)
{
this->name = name;
}
virtual void setDough(std::string dough)
{
this->dough = dough;
}
virtual void setSauce(std::string sauce)
{
this->sauce = sauce;
}
virtual void addTopping(std::string topping)
{
toppings.push_back(topping);
}
virtual ~Pizza() {}
};

30
lesson_4/PizzaStore.hpp Normal file
View File

@ -0,0 +1,30 @@
/*
* PizzaStore.hpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class PizzaStore
{
public:
virtual Pizza orderPizza(const std::string &type) const
{
Pizza pizza = createPizza(type);
pizza.prepare();
pizza.bake();
pizza.cut();
pizza.box();
return pizza;
}
virtual Pizza createPizza(const std::string &type) const = 0;
virtual ~PizzaStore() {}
};

33
lesson_4/main.cpp Normal file
View File

@ -0,0 +1,33 @@
/*
* main.cpp
*
* Created on: 2 нояб. 2021 г.
* Author: alexander
*/
#include <vector>
#include "NYPizzaStore.hpp"
#include "ChicagoPizzaStore.hpp"
int main()
{
std::vector<std::string> pizzaTypes {"cheese", "clam", "pepperoni", "veggie"};
PizzaStore *nyStore = new NYPizzaStore();
PizzaStore *chicagoStore = new ChicagoPizzaStore();
Pizza pizza;
for (const std::string &pizzaType : pizzaTypes)
{
pizza = nyStore->orderPizza(pizzaType);
std::cout << "Ethan ordered a " << pizza.getName() << std::endl;
pizza = chicagoStore->orderPizza(pizzaType);
std::cout << "Joel ordered a " << pizza.getName() << std::endl;
}
delete nyStore;
delete chicagoStore;
return 0;
}