Skip to content

Commit

Permalink
Infrastructure problem solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Kudryashov Dmitrii committed Nov 13, 2021
1 parent 3743637 commit e190f99
Showing 1 changed file with 107 additions and 40 deletions.
147 changes: 107 additions & 40 deletions PizzaStore/pizza_store.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include <iostream>
#include <memory>
#include <vector>

using std::make_unique;
using std::unique_ptr;
using namespace std;

enum class PizzaType {
cheese,
Expand All @@ -12,81 +12,148 @@ 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<string> 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<Pizza> orderPizza(PizzaType pizza_type) {
unique_ptr<Pizza> pizza = createPizza(pizza_type);

pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();

return pizza;
}

protected:
virtual unique_ptr<Pizza> createPizza(PizzaType pizza_type) = 0;
};

class SimplePizzaFactory {
class NYPizzaStore : public PizzaStore {
public:
unique_ptr<Pizza> createPizza(PizzaType pizza_type) {
unique_ptr<Pizza> createPizza(PizzaType pizza_type) override {
unique_ptr<Pizza> pizza;

switch (pizza_type) {
case PizzaType::cheese:
pizza = make_unique<CheesePizza>();
pizza = make_unique<NYStyleCheesePizza>();
break;
case PizzaType::greek:
pizza = make_unique<GreekPizza>();
pizza = make_unique<NYStyleGreekPizza>();
break;
case PizzaType::pepperoni:
pizza = make_unique<PepperoniPizza>();
pizza = make_unique<NYStylePepperoniPizza>();
break;
}

return pizza;
}
};

// 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<Pizza> orderPizza(PizzaType pizza_type) {
unique_ptr<Pizza> pizza = factory_->createPizza(pizza_type);
unique_ptr<Pizza> createPizza(PizzaType pizza_type) override {
unique_ptr<Pizza> pizza;

pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();
switch (pizza_type) {
case PizzaType::cheese:
pizza = make_unique<ChicagoStyleCheesePizza>();
break;
case PizzaType::greek:
pizza = make_unique<ChicagoStyleGreekPizza>();
break;
case PizzaType::pepperoni:
pizza = make_unique<ChicagoStylePepperoniPizza>();
break;
}

return pizza;
}

private:
SimplePizzaFactory* factory_;
};

int main() {
SimplePizzaFactory factory;
unique_ptr<PizzaStore> ny_store(new NYPizzaStore);
unique_ptr<PizzaStore> chicago_store(new ChicagoPizzaStore);

PizzaStore pizza_store(&factory);
unique_ptr<Pizza> 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> pizza_2 = chicago_store->orderPizza(PizzaType::cheese);
cout << "Joel ordered a " << pizza_2->getName() << '\n'
<< '\n';

return 0;
}

0 comments on commit e190f99

Please sign in to comment.