From e190f99447d1ebee13bab8e21a641e954a927991 Mon Sep 17 00:00:00 2001 From: Kudryashov Dmitrii Date: Sat, 13 Nov 2021 22:14:27 +0300 Subject: [PATCH] Infrastructure problem solution --- PizzaStore/pizza_store.cpp | 147 +++++++++++++++++++++++++++---------- 1 file changed, 107 insertions(+), 40 deletions(-) diff --git a/PizzaStore/pizza_store.cpp b/PizzaStore/pizza_store.cpp index 83a8223..d888ad7 100644 --- a/PizzaStore/pizza_store.cpp +++ b/PizzaStore/pizza_store.cpp @@ -1,8 +1,8 @@ #include #include +#include -using std::make_unique; -using std::unique_ptr; +using namespace std; enum class PizzaType { cheese, @@ -12,41 +12,109 @@ enum class PizzaType { class Pizza { public: - virtual void prepare() = 0; - void bake(){}; - void cut(){}; - void box(){}; + virtual void prepare() { + cout << "Preparing " << name_ << '\n'; + cout << "Tossing dough " << dough_ << '\n'; + cout << "Adding sauce " << sauce_ << '\n'; + + cout << "Adding toppings " << '\n'; + for (const auto& topping : toppings_) { + cout << " " << topping; + } + cout << '\n'; + }; + + virtual void bake() { + cout << "Bake for 25 minutes at 350" << '\n'; + }; + + virtual void cut() { + cout << "Cutting the pizza into diagonal slices" << '\n'; + }; + + virtual void box() { + cout << "Place pizza in official PizzaStore box" << '\n'; + }; + + string getName() { + return name_; + } + +protected: + string name_; + string dough_; + string sauce_; + vector toppings_; }; -class CheesePizza : public Pizza { +class NYStyleCheesePizza : public Pizza { public: - void prepare() override{}; + NYStyleCheesePizza() { + name_ = "NY Style Sauce and Cheese Pizza"; + dough_ = "Thin Crust Dough"; + sauce_ = "Marinara Sauce"; + + toppings_.emplace_back("Grated Reggiano Cheese"); + } }; -class GreekPizza : public Pizza { +class ChicagoStyleCheesePizza : public Pizza { public: - void prepare() override{}; + ChicagoStyleCheesePizza() { + name_ = "Chicago Style Deep Dish Cheese Pizza"; + dough_ = "Extra Thick Crust Dough"; + sauce_ = "Plum Tomato Sauce"; + + toppings_.emplace_back("Shredded Mozzarella Cheese"); + } + void cut() override { + cout << "Cutting the pizza into square slices" << '\n'; + } +}; + +class NYStyleGreekPizza : public Pizza { +}; + +class ChicagoStyleGreekPizza : public Pizza { +}; + +class NYStylePepperoniPizza : public Pizza { +}; + +class ChicagoStylePepperoniPizza : public Pizza { }; -class PepperoniPizza : public Pizza { +class PizzaStore { public: - void prepare() override{}; + unique_ptr orderPizza(PizzaType pizza_type) { + unique_ptr pizza = createPizza(pizza_type); + + pizza->prepare(); + pizza->bake(); + pizza->cut(); + pizza->box(); + + return pizza; + } + +protected: + virtual unique_ptr createPizza(PizzaType pizza_type) = 0; }; -class SimplePizzaFactory { +class NYPizzaStore : public PizzaStore { public: - unique_ptr createPizza(PizzaType pizza_type) { + unique_ptr createPizza(PizzaType pizza_type) override { unique_ptr pizza; switch (pizza_type) { case PizzaType::cheese: - pizza = make_unique(); + pizza = make_unique(); break; case PizzaType::greek: - pizza = make_unique(); + pizza = make_unique(); break; case PizzaType::pepperoni: - pizza = make_unique(); + pizza = make_unique(); break; } @@ -54,39 +122,38 @@ class SimplePizzaFactory { } }; -// Problem: -// nyStore -> NYPizzaFactory -// chicagoStore -> ChicagoPizzaFactory - -// every store has it own sequence of pizza recipe - -class PizzaStore { +class ChicagoPizzaStore : public PizzaStore { public: - PizzaStore(SimplePizzaFactory* factory) - : factory_(factory) { - } - - unique_ptr orderPizza(PizzaType pizza_type) { - unique_ptr pizza = factory_->createPizza(pizza_type); + unique_ptr createPizza(PizzaType pizza_type) override { + unique_ptr pizza; - pizza->prepare(); - pizza->bake(); - pizza->cut(); - pizza->box(); + switch (pizza_type) { + case PizzaType::cheese: + pizza = make_unique(); + break; + case PizzaType::greek: + pizza = make_unique(); + break; + case PizzaType::pepperoni: + pizza = make_unique(); + break; + } return pizza; } - -private: - SimplePizzaFactory* factory_; }; int main() { - SimplePizzaFactory factory; + unique_ptr ny_store(new NYPizzaStore); + unique_ptr chicago_store(new ChicagoPizzaStore); - PizzaStore pizza_store(&factory); + unique_ptr pizza_1 = ny_store->orderPizza(PizzaType::cheese); + cout << "Ethan ordered a " << pizza_1->getName() << '\n' + << '\n'; - const auto pizza = pizza_store.orderPizza(PizzaType::pepperoni); + unique_ptr pizza_2 = chicago_store->orderPizza(PizzaType::cheese); + cout << "Joel ordered a " << pizza_2->getName() << '\n' + << '\n'; return 0; } \ No newline at end of file