diff --git a/README.md b/README.md index e69de29..b15547f 100644 --- a/README.md +++ b/README.md @@ -0,0 +1,7 @@ +Паттерн "Абстрактная Фабрика" предоставляет интерфейс для семейств взаимосвязанных объектов без указания их конкретных классов. + +Все фабричные паттерны обеспечивают слабую связанность за счет сокращения зависимости приложения от конкретных классов. +Все фабрики инкапсулируют создание объектов. Абстрактная Фабрика основана на композиции: создание объектов реализуется в методе, доступ к которому осуществляется через интерфейс фабрики. Задача Абстрактной Фабрики - создание семейств взаимосвязанных объектов без зависимости от их конкретных классов. + +*Принцип:* +- По возможности использовать абстракции в коде. diff --git a/lesson_4/Cheese.hpp b/lesson_4/Cheese.hpp new file mode 100644 index 0000000..fd0a102 --- /dev/null +++ b/lesson_4/Cheese.hpp @@ -0,0 +1,48 @@ +/* + * Cheese.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +class Cheese +{ +public: + virtual std::string toString() const = 0; + + virtual ~Cheese() {} +}; + +class MozzarellaCheese: public Cheese +{ +public: + + std::string toString() const + { + return "Shredded Mozzarella"; + } +}; + +class ParmesanCheese: public Cheese +{ +public: + + std::string toString() const + { + return "Shredded Parmesan"; + } +}; + +class ReggianoCheese: public Cheese +{ +public: + + std::string toString() const + { + return "Reggiano Cheese"; + } +}; diff --git a/lesson_4/CheesePizza.hpp b/lesson_4/CheesePizza.hpp new file mode 100644 index 0000000..80d0c56 --- /dev/null +++ b/lesson_4/CheesePizza.hpp @@ -0,0 +1,32 @@ +/* + * CheesePizza.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Pizza.hpp" +#include "PizzaIngredientFactory.hpp" + +class CheesePizza: public Pizza +{ +public: + PizzaIngredientFactory *ingredientFactory; + + CheesePizza(PizzaIngredientFactory *ingredientFactory) : ingredientFactory(ingredientFactory) {} + + void prepare() override + { + std::cout << "Preparing " << getName() << std::endl; + setDough(ingredientFactory->createDough()); + setSauce(ingredientFactory->createSauce()); + setCheese(ingredientFactory->createCheese()); + } + + ~CheesePizza() + { + delete ingredientFactory; + } +}; diff --git a/lesson_4/ChicagoPizzaIngredientFactory.hpp b/lesson_4/ChicagoPizzaIngredientFactory.hpp new file mode 100644 index 0000000..f5a89a7 --- /dev/null +++ b/lesson_4/ChicagoPizzaIngredientFactory.hpp @@ -0,0 +1,52 @@ +/* + * ChicagoPizzaIngredientFactory.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "PizzaIngredientFactory.hpp" +#include "Cheese.hpp" +#include "Sauce.hpp" +#include "Dough.hpp" +#include "Veggies.hpp" +#include "Pepperoni.hpp" +#include "Clams.hpp" + +class ChicagoPizzaIngredientFactory: public PizzaIngredientFactory +{ +public: + Dough* createDough() const override + { + return new ThickCrustDough(); + } + + Sauce* createSauce() const override + { + return new PlumTomatoSauce(); + } + + Cheese* createCheese() const override + { + return new MozzarellaCheese(); + } + + std::vector createVeggies() const override + { + std::vector veggies { new BlackOlivesVeggies(), new SpinachVeggies(), new EggplantVeggies() }; + + return veggies; + } + + Pepperoni* createPepperoni() const override + { + return new SlicedPepperoni(); + } + + Clams* createClam() const override + { + return new FrozenClams(); + } +}; diff --git a/lesson_4/ChicagoPizzaStore.hpp b/lesson_4/ChicagoPizzaStore.hpp index 0e731aa..cd9a638 100644 --- a/lesson_4/ChicagoPizzaStore.hpp +++ b/lesson_4/ChicagoPizzaStore.hpp @@ -1,43 +1,54 @@ /* * ChicagoPizzaStore.hpp * - * Created on: 2 нояб. 2021 г. + * Created on: 3 нояб. 2021 г. * Author: alexander */ #pragma once #include "PizzaStore.hpp" -#include "ChicagoStylePizza.hpp" -#include "ChicagoStyleCheesePizza.hpp" -#include "ChicagoStyleVeggiePizza.hpp" -#include "ChicagoStyleClamPizza.hpp" -#include "ChicagoStylePepperoniPizza.hpp" +#include "CheesePizza.hpp" +#include "PepperoniPizza.hpp" +#include "VeggiePizza.hpp" +#include "ClamPizza.hpp" +#include "StandardPizza.hpp" +#include "NYPizzaIngredientFactory.hpp" class ChicagoPizzaStore: public PizzaStore { public: - Pizza createPizza(const std::string &type) const override + Pizza* createPizza(const std::string &type) const override { + Pizza *pizza = NULL; + PizzaIngredientFactory *ingredientFactory = new NYPizzaIngredientFactory(); + if (type == "cheese") { - return ChicagoStyleCheesePizza(); + pizza = new CheesePizza(ingredientFactory); + pizza->setName("Chicago Style Cheese Pizza"); } else if (type == "veggie") { - return ChicagoStyleVeggiePizza(); + pizza = new VeggiePizza(ingredientFactory); + pizza->setName("Chicago Style Veggie Pizza"); } else if (type == "clam") { - return ChicagoStyleClamPizza(); + pizza = new ClamPizza(ingredientFactory); + pizza->setName("Chicago Style Clam Pizza"); } else if (type == "pepperoni") { - return ChicagoStylePepperoniPizza(); + pizza = new PepperoniPizza(ingredientFactory); + pizza->setName("Chicago Style Pepperoni Pizza"); } else { - return ChicagoStylePizza(); + pizza = new StandardPizza(ingredientFactory); + pizza->setName("Chicago Style Standard Pizza"); } + + return pizza; } }; diff --git a/lesson_4/ChicagoStyleCheesePizza.hpp b/lesson_4/ChicagoStyleCheesePizza.hpp deleted file mode 100644 index b68ba4d..0000000 --- a/lesson_4/ChicagoStyleCheesePizza.hpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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; - } -}; diff --git a/lesson_4/ChicagoStyleClamPizza.hpp b/lesson_4/ChicagoStyleClamPizza.hpp deleted file mode 100644 index b8d5c12..0000000 --- a/lesson_4/ChicagoStyleClamPizza.hpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * 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; - } -}; diff --git a/lesson_4/ChicagoStylePepperoniPizza.hpp b/lesson_4/ChicagoStylePepperoniPizza.hpp deleted file mode 100644 index 0922f86..0000000 --- a/lesson_4/ChicagoStylePepperoniPizza.hpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * 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; - } -}; diff --git a/lesson_4/ChicagoStylePizza.hpp b/lesson_4/ChicagoStylePizza.hpp deleted file mode 100644 index 0545efe..0000000 --- a/lesson_4/ChicagoStylePizza.hpp +++ /dev/null @@ -1,14 +0,0 @@ -/* - * ChicagoStylePizza.hpp - * - * Created on: 2 нояб. 2021 г. - * Author: alexander - */ - -#pragma once - -#include "Pizza.hpp" - -class ChicagoStylePizza: public Pizza -{ -}; diff --git a/lesson_4/ChicagoStyleVeggiePizza.hpp b/lesson_4/ChicagoStyleVeggiePizza.hpp deleted file mode 100644 index 8a44a0f..0000000 --- a/lesson_4/ChicagoStyleVeggiePizza.hpp +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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; - } -}; diff --git a/lesson_4/ClamPizza.hpp b/lesson_4/ClamPizza.hpp new file mode 100644 index 0000000..60f7d8f --- /dev/null +++ b/lesson_4/ClamPizza.hpp @@ -0,0 +1,33 @@ +/* + * ClamPizza.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Pizza.hpp" +#include "PizzaIngredientFactory.hpp" + +class ClamPizza: public Pizza +{ +public: + PizzaIngredientFactory *ingredientFactory; + + ClamPizza(PizzaIngredientFactory *ingredientFactory) : ingredientFactory(ingredientFactory) {} + + void prepare() override + { + std::cout << "Preparing " << getName() << std::endl; + setDough(ingredientFactory->createDough()); + setSauce(ingredientFactory->createSauce()); + setCheese(ingredientFactory->createCheese()); + setClams(ingredientFactory->createClam()); + } + + ~ClamPizza() + { + delete ingredientFactory; + } +}; diff --git a/lesson_4/Clams.hpp b/lesson_4/Clams.hpp new file mode 100644 index 0000000..367b77a --- /dev/null +++ b/lesson_4/Clams.hpp @@ -0,0 +1,38 @@ +/* + * Clams.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +class Clams +{ +public: + virtual std::string toString() const = 0; + + virtual ~Clams() {} +}; + +class FreshClams: public Clams +{ +public: + + std::string toString() const + { + return "Fresh Clams from Long Island Sound"; + } +}; + +class FrozenClams: public Clams +{ +public: + + std::string toString() const + { + return "Frozen Clams from Chesapeake Bay"; + } +}; diff --git a/lesson_4/Dough.hpp b/lesson_4/Dough.hpp new file mode 100644 index 0000000..78fac50 --- /dev/null +++ b/lesson_4/Dough.hpp @@ -0,0 +1,38 @@ +/* + * Dough.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +class Dough +{ +public: + virtual std::string toString() const = 0; + + virtual ~Dough() {} +}; + +class ThinCrustDough: public Dough +{ +public: + + std::string toString() const + { + return "Thin Crust Dough"; + } +}; + +class ThickCrustDough: public Dough +{ +public: + + std::string toString() const + { + return "ThickCrust style extra thick crust dough"; + } +}; diff --git a/lesson_4/NYPizzaIngredientFactory.hpp b/lesson_4/NYPizzaIngredientFactory.hpp new file mode 100644 index 0000000..ca4f69c --- /dev/null +++ b/lesson_4/NYPizzaIngredientFactory.hpp @@ -0,0 +1,52 @@ +/* + * NYPizzaIngredientFactory.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "PizzaIngredientFactory.hpp" +#include "Cheese.hpp" +#include "Sauce.hpp" +#include "Dough.hpp" +#include "Veggies.hpp" +#include "Pepperoni.hpp" +#include "Clams.hpp" + +class NYPizzaIngredientFactory: public PizzaIngredientFactory +{ +public: + Dough* createDough() const override + { + return new ThinCrustDough(); + } + + Sauce* createSauce() const override + { + return new MarinaraSauce(); + } + + Cheese* createCheese() const override + { + return new ReggianoCheese(); + } + + std::vector createVeggies() const override + { + std::vector veggies { new GarlicVeggies(), new OnionVeggies(), new MushroomVeggies(), new RedPepperVeggies() }; + + return veggies; + } + + Pepperoni* createPepperoni() const override + { + return new SlicedPepperoni(); + } + + Clams* createClam() const override + { + return new FreshClams(); + } +}; diff --git a/lesson_4/NYPizzaStore.hpp b/lesson_4/NYPizzaStore.hpp index c07c72e..0c40bf0 100644 --- a/lesson_4/NYPizzaStore.hpp +++ b/lesson_4/NYPizzaStore.hpp @@ -1,43 +1,54 @@ /* * NYPizzaStore.hpp * - * Created on: 2 нояб. 2021 г. + * Created on: 3 нояб. 2021 г. * Author: alexander */ #pragma once #include "PizzaStore.hpp" -#include "NYStylePiza.hpp" -#include "NYStyleCheesePiza.hpp" -#include "NYStyleVeggiePizza.hpp" -#include "NYStyleClamPizza.hpp" -#include "NYStylePepperoniPizza.hpp" +#include "CheesePizza.hpp" +#include "PepperoniPizza.hpp" +#include "VeggiePizza.hpp" +#include "ClamPizza.hpp" +#include "StandardPizza.hpp" +#include "NYPizzaIngredientFactory.hpp" class NYPizzaStore: public PizzaStore { public: - Pizza createPizza(const std::string &type) const override + Pizza* createPizza(const std::string &type) const override { + Pizza *pizza = NULL; + PizzaIngredientFactory *ingredientFactory = new NYPizzaIngredientFactory(); + if (type == "cheese") { - return NYStyleCheesePiza(); + pizza = new CheesePizza(ingredientFactory); + pizza->setName("New York Style Cheese Pizza"); } else if (type == "veggie") { - return NYStyleVeggiePizza(); + pizza = new VeggiePizza(ingredientFactory); + pizza->setName("New York Style Veggie Pizza"); } else if (type == "clam") { - return NYStyleClamPizza(); + pizza = new ClamPizza(ingredientFactory); + pizza->setName("New York Style Clam Pizza"); } else if (type == "pepperoni") { - return NYStylePepperoniPizza(); + pizza = new PepperoniPizza(ingredientFactory); + pizza->setName("New York Style Pepperoni Pizza"); } else { - return NYStylePiza(); + pizza = new StandardPizza(ingredientFactory); + pizza->setName("New York Style Standard Pizza"); } + + return pizza; } }; diff --git a/lesson_4/NYStyleCheesePiza.hpp b/lesson_4/NYStyleCheesePiza.hpp deleted file mode 100644 index 7c32227..0000000 --- a/lesson_4/NYStyleCheesePiza.hpp +++ /dev/null @@ -1,23 +0,0 @@ -/* - * 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"); - } -}; diff --git a/lesson_4/NYStyleClamPizza.hpp b/lesson_4/NYStyleClamPizza.hpp deleted file mode 100644 index 3bad4d4..0000000 --- a/lesson_4/NYStyleClamPizza.hpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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"); - } -}; diff --git a/lesson_4/NYStylePepperoniPizza.hpp b/lesson_4/NYStylePepperoniPizza.hpp deleted file mode 100644 index 0118355..0000000 --- a/lesson_4/NYStylePepperoniPizza.hpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * 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"); - } -}; diff --git a/lesson_4/NYStylePiza.hpp b/lesson_4/NYStylePiza.hpp deleted file mode 100644 index 019c802..0000000 --- a/lesson_4/NYStylePiza.hpp +++ /dev/null @@ -1,14 +0,0 @@ -/* - * NYStylePiza.hpp - * - * Created on: 2 нояб. 2021 г. - * Author: alexander - */ - -#pragma once - -#include "Pizza.hpp" - -class NYStylePiza: public Pizza -{ -}; diff --git a/lesson_4/NYStyleVeggiePizza.hpp b/lesson_4/NYStyleVeggiePizza.hpp deleted file mode 100644 index 942b0d3..0000000 --- a/lesson_4/NYStyleVeggiePizza.hpp +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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"); - } -}; diff --git a/lesson_4/Pepperoni.hpp b/lesson_4/Pepperoni.hpp new file mode 100644 index 0000000..1099813 --- /dev/null +++ b/lesson_4/Pepperoni.hpp @@ -0,0 +1,27 @@ +/* + * Pepperoni.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +class Pepperoni { +public: + virtual std::string toString() const = 0; + + virtual ~Pepperoni() {} +}; + +class SlicedPepperoni: public Pepperoni +{ +public: + + std::string toString() const + { + return "Sliced Pepperoni"; + } +}; diff --git a/lesson_4/PepperoniPizza.hpp b/lesson_4/PepperoniPizza.hpp new file mode 100644 index 0000000..e756e91 --- /dev/null +++ b/lesson_4/PepperoniPizza.hpp @@ -0,0 +1,34 @@ +/* + * PepperoniPizza.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Pizza.hpp" +#include "PizzaIngredientFactory.hpp" + +class PepperoniPizza: public Pizza +{ +public: + PizzaIngredientFactory *ingredientFactory; + + PepperoniPizza(PizzaIngredientFactory *ingredientFactory) : ingredientFactory(ingredientFactory) {} + + void prepare() override + { + std::cout << "Preparing " << getName() << std::endl; + setDough(ingredientFactory->createDough()); + setSauce(ingredientFactory->createSauce()); + setCheese(ingredientFactory->createCheese()); + setVeggies(ingredientFactory->createVeggies()); + setPepperoni(ingredientFactory->createPepperoni()); + } + + ~PepperoniPizza() + { + delete ingredientFactory; + } +}; diff --git a/lesson_4/Pizza.hpp b/lesson_4/Pizza.hpp index a136117..a0d511b 100644 --- a/lesson_4/Pizza.hpp +++ b/lesson_4/Pizza.hpp @@ -1,35 +1,36 @@ /* * Pizza.hpp * - * Created on: 2 нояб. 2021 г. + * Created on: 3 нояб. 2021 г. * Author: alexander */ #pragma once #include -#include +#include +#include +#include "Dough.hpp" +#include "Sauce.hpp" +#include "Cheese.hpp" +#include "Veggies.hpp" +#include "Pepperoni.hpp" +#include "Clams.hpp" class Pizza { private: std::string name; - std::string dough; - std::string sauce; - std::list toppings; + + Dough *dough = NULL; + Sauce *sauce = NULL; + std::vector veggies; + Cheese *cheese = NULL; + Pepperoni *pepperoni = NULL; + Clams *clam = NULL; 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 prepare() = 0; virtual void bake() const { @@ -56,20 +57,82 @@ public: this->name = name; } - virtual void setDough(std::string dough) + virtual void setDough(Dough *dough) { this->dough = dough; } - - virtual void setSauce(std::string sauce) + virtual void setSauce(Sauce *sauce) { this->sauce = sauce; } - - virtual void addTopping(std::string topping) + virtual void setVeggies(const std::vector &veggies) { - toppings.push_back(topping); + this->veggies = veggies; + } + virtual void setCheese(Cheese *cheese) + { + this->cheese = cheese; + } + virtual void setPepperoni(Pepperoni *pepperoni) + { + this->pepperoni = pepperoni; + } + virtual void setClams(Clams *clam) + { + this->clam = clam; } - virtual ~Pizza() {} + void toString() + { + std::cout << "---- " << name << " ----" << std::endl; + + if (dough) + { + std::cout << '\t' << dough->toString() << std::endl; + } + if (sauce) + { + std::cout << '\t' << sauce->toString() << std::endl; + } + if (cheese) + { + std::cout << '\t' << cheese->toString() << std::endl; + } + if (!veggies.empty()) + { + std::cout << '\t'; + for (size_t i = 0; i < veggies.size(); i++) + { + std::cout << veggies[i]->toString(); + + if (i < veggies.size() - 1) + { + std::cout << ", "; + } + } + std::cout << std::endl; + } + if (clam) + { + std::cout << '\t' << clam->toString() << std::endl; + } + if (pepperoni) + { + std::cout << '\t' << pepperoni->toString() << std::endl; + } + } + + virtual ~Pizza() + { + if (dough) delete dough; + if (sauce) delete sauce; + if (cheese) delete cheese; + if (pepperoni) delete pepperoni; + if (clam) delete clam; + + for (auto vegetable : veggies) + { + delete vegetable; + } + } }; diff --git a/lesson_4/PizzaIngredientFactory.hpp b/lesson_4/PizzaIngredientFactory.hpp new file mode 100644 index 0000000..1752470 --- /dev/null +++ b/lesson_4/PizzaIngredientFactory.hpp @@ -0,0 +1,29 @@ +/* + * PizzaIngredientFactory.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Dough.hpp" +#include "Sauce.hpp" +#include "Cheese.hpp" +#include "Veggies.hpp" +#include "Pepperoni.hpp" +#include "Clams.hpp" +#include + +class PizzaIngredientFactory +{ +public: + virtual Dough* createDough() const = 0; + virtual Sauce* createSauce() const = 0; + virtual Cheese* createCheese() const = 0; + virtual std::vector createVeggies() const = 0; + virtual Pepperoni* createPepperoni() const = 0; + virtual Clams* createClam() const = 0; + + virtual ~PizzaIngredientFactory() {} +}; diff --git a/lesson_4/PizzaStore.hpp b/lesson_4/PizzaStore.hpp index 1055c6c..e913d16 100644 --- a/lesson_4/PizzaStore.hpp +++ b/lesson_4/PizzaStore.hpp @@ -1,7 +1,7 @@ /* * PizzaStore.hpp * - * Created on: 2 нояб. 2021 г. + * Created on: 3 нояб. 2021 г. * Author: alexander */ @@ -12,19 +12,21 @@ class PizzaStore { public: - virtual Pizza orderPizza(const std::string &type) const + virtual Pizza* orderPizza(const std::string &type) const { - Pizza pizza = createPizza(type); + Pizza *pizza = createPizza(type); - pizza.prepare(); - pizza.bake(); - pizza.cut(); - pizza.box(); + std::cout << "--- Making a " << pizza->getName() << " ---" << std::endl; + + pizza->prepare(); + pizza->bake(); + pizza->cut(); + pizza->box(); return pizza; } - virtual Pizza createPizza(const std::string &type) const = 0; + virtual Pizza* createPizza(const std::string &type) const = 0; virtual ~PizzaStore() {} }; diff --git a/lesson_4/Sauce.hpp b/lesson_4/Sauce.hpp new file mode 100644 index 0000000..e8aaf6f --- /dev/null +++ b/lesson_4/Sauce.hpp @@ -0,0 +1,38 @@ +/* + * Sauce.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +class Sauce +{ +public: + virtual std::string toString() const = 0; + + virtual ~Sauce() {} +}; + +class MarinaraSauce: public Sauce +{ +public: + + std::string toString() const + { + return "Marinara Sauce"; + } +}; + +class PlumTomatoSauce: public Sauce +{ +public: + + std::string toString() const + { + return "Tomato sauce with plum tomatoes"; + } +}; diff --git a/lesson_4/StandardPizza.hpp b/lesson_4/StandardPizza.hpp new file mode 100644 index 0000000..2b16baf --- /dev/null +++ b/lesson_4/StandardPizza.hpp @@ -0,0 +1,29 @@ +/* + * StandardPizza.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Pizza.hpp" +#include "PizzaIngredientFactory.hpp" + +class StandardPizza: public Pizza +{ +public: + PizzaIngredientFactory *ingredientFactory; + + StandardPizza(PizzaIngredientFactory *ingredientFactory) : ingredientFactory(ingredientFactory) {} + + void prepare() override + { + std::cout << "Preparing " << getName() << std::endl; + } + + ~StandardPizza() + { + delete ingredientFactory; + } +}; diff --git a/lesson_4/VeggiePizza.hpp b/lesson_4/VeggiePizza.hpp new file mode 100644 index 0000000..5d046d2 --- /dev/null +++ b/lesson_4/VeggiePizza.hpp @@ -0,0 +1,33 @@ +/* + * VeggiePizza.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include "Pizza.hpp" +#include "PizzaIngredientFactory.hpp" + +class VeggiePizza: public Pizza +{ +public: + PizzaIngredientFactory *ingredientFactory; + + VeggiePizza(PizzaIngredientFactory *ingredientFactory) : ingredientFactory(ingredientFactory) {} + + void prepare() override + { + std::cout << "Preparing " << getName() << std::endl; + setDough(ingredientFactory->createDough()); + setSauce(ingredientFactory->createSauce()); + setCheese(ingredientFactory->createCheese()); + setVeggies(ingredientFactory->createVeggies()); + } + + ~VeggiePizza() + { + delete ingredientFactory; + } +}; diff --git a/lesson_4/Veggies.hpp b/lesson_4/Veggies.hpp new file mode 100644 index 0000000..00a8275 --- /dev/null +++ b/lesson_4/Veggies.hpp @@ -0,0 +1,87 @@ +/* + * Veggies.hpp + * + * Created on: 3 нояб. 2021 г. + * Author: alexander + */ + +#pragma once + +#include + +class Veggies { +public: + virtual std::string toString() const = 0; + + virtual ~Veggies() {} +}; + +class BlackOlivesVeggies: public Veggies +{ +public: + + std::string toString() const + { + return "Black Olives"; + } +}; + +class EggplantVeggies: public Veggies +{ +public: + + std::string toString() const + { + return "Eggplant"; + } +}; + +class MushroomVeggies: public Veggies +{ +public: + + std::string toString() const + { + return "Mushrooms"; + } +}; + +class GarlicVeggies: public Veggies +{ +public: + + std::string toString() const + { + return "Garlic"; + } +}; + +class RedPepperVeggies: public Veggies +{ +public: + + std::string toString() const + { + return "Red Pepper"; + } +}; + +class SpinachVeggies: public Veggies +{ +public: + + std::string toString() const + { + return "Spinach"; + } +}; + +class OnionVeggies: public Veggies +{ +public: + + std::string toString() const + { + return "Onion"; + } +}; diff --git a/lesson_4/main.cpp b/lesson_4/main.cpp index 742e61a..8ebe11e 100644 --- a/lesson_4/main.cpp +++ b/lesson_4/main.cpp @@ -1,29 +1,32 @@ /* * main.cpp * - * Created on: 2 нояб. 2021 г. + * Created on: 3 нояб. 2021 г. * Author: alexander */ -#include #include "NYPizzaStore.hpp" #include "ChicagoPizzaStore.hpp" +#include int main() { - std::vector pizzaTypes {"cheese", "clam", "pepperoni", "veggie"}; + std::vector stylesPizza {"cheese", "clam", "pepperoni", "veggie"}; PizzaStore *nyStore = new NYPizzaStore(); PizzaStore *chicagoStore = new ChicagoPizzaStore(); - Pizza pizza; + Pizza *pizza = NULL; - for (const std::string &pizzaType : pizzaTypes) + for (const std::string &style : stylesPizza) { - 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; + pizza = nyStore->orderPizza(style); + std::cout << "Ethan ordered a " << pizza->getName() << std::endl; + pizza->toString(); + delete pizza; + pizza = chicagoStore->orderPizza(style); + std::cout << "Joel ordered a " << pizza->getName() << std::endl; + pizza->toString(); + delete pizza; } delete nyStore;