Skip to content

Commit

Permalink
Simple fabric idioma: solution
Browse files Browse the repository at this point in the history
Kudryashov Dmitrii committed Nov 11, 2021

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 7736e4e commit 4cff637
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions PizzaStore/pizza_store.cpp
Original file line number Diff line number Diff line change
@@ -33,37 +33,54 @@ class PepperoniPizza : public Pizza {
void prepare() override{};
};

unique_ptr<Pizza> orderPizza(PizzaType pizza_type) {
unique_ptr<Pizza> pizza;

// this is a problem - if you wish to add or remove some pizza types - you'll have to change this code
switch (pizza_type) {
case PizzaType::cheese:
pizza = make_unique<CheesePizza>();
break;
case PizzaType::greek:
pizza = make_unique<GreekPizza>();
break;
case PizzaType::pepperoni:
pizza = make_unique<PepperoniPizza>();
break;
default:
return pizza; // nullptr
class SimplePizzaFactory {
public:
unique_ptr<Pizza> createPizza(PizzaType pizza_type) {
unique_ptr<Pizza> pizza;

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

return pizza;
}
};

pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();
class PizzaStore {
public:
PizzaStore(SimplePizzaFactory* factory)
: factory_(factory) {
}

return pizza;
}
unique_ptr<Pizza> orderPizza(PizzaType pizza_type) {
unique_ptr<Pizza> pizza = factory_->createPizza(pizza_type);

int main() {
auto order = orderPizza(PizzaType::cheese);
if (!order) {
return -1;
pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();

return pizza;
}

private:
SimplePizzaFactory* factory_;
};

int main() {
SimplePizzaFactory factory;

PizzaStore pizza_store(&factory);

const auto pizza = pizza_store.orderPizza(PizzaType::pepperoni);

return 0;
}

0 comments on commit 4cff637

Please sign in to comment.