Compare commits

...
This repository has been archived on 2022-11-09. You can view files and clone it, but cannot push or open issues or pull requests.

2 Commits

Author SHA1 Message Date
Alexander Zhirov 97ec325534 factory with ingredients 2021-11-03 18:35:58 +03:00
Alexander Zhirov a44ab07735 factory without ingredients 2021-11-02 21:18:09 +03:00
20 changed files with 891 additions and 0 deletions

View File

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

48
lesson_4/Cheese.hpp Normal file
View File

@ -0,0 +1,48 @@
/*
* Cheese.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <string>
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";
}
};

32
lesson_4/CheesePizza.hpp Normal file
View File

@ -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;
}
};

View File

@ -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<Veggies*> createVeggies() const override
{
std::vector<Veggies*> veggies { new BlackOlivesVeggies(), new SpinachVeggies(), new EggplantVeggies() };
return veggies;
}
Pepperoni* createPepperoni() const override
{
return new SlicedPepperoni();
}
Clams* createClam() const override
{
return new FrozenClams();
}
};

View File

@ -0,0 +1,54 @@
/*
* ChicagoPizzaStore.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "PizzaStore.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 *pizza = NULL;
PizzaIngredientFactory *ingredientFactory = new NYPizzaIngredientFactory();
if (type == "cheese")
{
pizza = new CheesePizza(ingredientFactory);
pizza->setName("Chicago Style Cheese Pizza");
}
else if (type == "veggie")
{
pizza = new VeggiePizza(ingredientFactory);
pizza->setName("Chicago Style Veggie Pizza");
}
else if (type == "clam")
{
pizza = new ClamPizza(ingredientFactory);
pizza->setName("Chicago Style Clam Pizza");
}
else if (type == "pepperoni")
{
pizza = new PepperoniPizza(ingredientFactory);
pizza->setName("Chicago Style Pepperoni Pizza");
}
else
{
pizza = new StandardPizza(ingredientFactory);
pizza->setName("Chicago Style Standard Pizza");
}
return pizza;
}
};

33
lesson_4/ClamPizza.hpp Normal file
View File

@ -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;
}
};

38
lesson_4/Clams.hpp Normal file
View File

@ -0,0 +1,38 @@
/*
* Clams.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <string>
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";
}
};

38
lesson_4/Dough.hpp Normal file
View File

@ -0,0 +1,38 @@
/*
* Dough.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <string>
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";
}
};

View File

@ -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<Veggies*> createVeggies() const override
{
std::vector<Veggies*> 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();
}
};

54
lesson_4/NYPizzaStore.hpp Normal file
View File

@ -0,0 +1,54 @@
/*
* NYPizzaStore.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "PizzaStore.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 *pizza = NULL;
PizzaIngredientFactory *ingredientFactory = new NYPizzaIngredientFactory();
if (type == "cheese")
{
pizza = new CheesePizza(ingredientFactory);
pizza->setName("New York Style Cheese Pizza");
}
else if (type == "veggie")
{
pizza = new VeggiePizza(ingredientFactory);
pizza->setName("New York Style Veggie Pizza");
}
else if (type == "clam")
{
pizza = new ClamPizza(ingredientFactory);
pizza->setName("New York Style Clam Pizza");
}
else if (type == "pepperoni")
{
pizza = new PepperoniPizza(ingredientFactory);
pizza->setName("New York Style Pepperoni Pizza");
}
else
{
pizza = new StandardPizza(ingredientFactory);
pizza->setName("New York Style Standard Pizza");
}
return pizza;
}
};

27
lesson_4/Pepperoni.hpp Normal file
View File

@ -0,0 +1,27 @@
/*
* Pepperoni.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <string>
class Pepperoni {
public:
virtual std::string toString() const = 0;
virtual ~Pepperoni() {}
};
class SlicedPepperoni: public Pepperoni
{
public:
std::string toString() const
{
return "Sliced Pepperoni";
}
};

View File

@ -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;
}
};

138
lesson_4/Pizza.hpp Normal file
View File

@ -0,0 +1,138 @@
/*
* Pizza.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <iostream>
#include <vector>
#include <sstream>
#include "Dough.hpp"
#include "Sauce.hpp"
#include "Cheese.hpp"
#include "Veggies.hpp"
#include "Pepperoni.hpp"
#include "Clams.hpp"
class Pizza
{
private:
std::string name;
Dough *dough = NULL;
Sauce *sauce = NULL;
std::vector<Veggies*> veggies;
Cheese *cheese = NULL;
Pepperoni *pepperoni = NULL;
Clams *clam = NULL;
public:
virtual void prepare() = 0;
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(Dough *dough)
{
this->dough = dough;
}
virtual void setSauce(Sauce *sauce)
{
this->sauce = sauce;
}
virtual void setVeggies(const std::vector<Veggies*> &veggies)
{
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;
}
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;
}
}
};

View File

@ -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 <vector>
class PizzaIngredientFactory
{
public:
virtual Dough* createDough() const = 0;
virtual Sauce* createSauce() const = 0;
virtual Cheese* createCheese() const = 0;
virtual std::vector<Veggies*> createVeggies() const = 0;
virtual Pepperoni* createPepperoni() const = 0;
virtual Clams* createClam() const = 0;
virtual ~PizzaIngredientFactory() {}
};

32
lesson_4/PizzaStore.hpp Normal file
View File

@ -0,0 +1,32 @@
/*
* PizzaStore.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include "Pizza.hpp"
class PizzaStore
{
public:
virtual Pizza* orderPizza(const std::string &type) const
{
Pizza *pizza = createPizza(type);
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 ~PizzaStore() {}
};

38
lesson_4/Sauce.hpp Normal file
View File

@ -0,0 +1,38 @@
/*
* Sauce.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <string>
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";
}
};

View File

@ -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;
}
};

33
lesson_4/VeggiePizza.hpp Normal file
View File

@ -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;
}
};

87
lesson_4/Veggies.hpp Normal file
View File

@ -0,0 +1,87 @@
/*
* Veggies.hpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#pragma once
#include <string>
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";
}
};

36
lesson_4/main.cpp Normal file
View File

@ -0,0 +1,36 @@
/*
* main.cpp
*
* Created on: 3 нояб. 2021 г.
* Author: alexander
*/
#include "NYPizzaStore.hpp"
#include "ChicagoPizzaStore.hpp"
#include <vector>
int main()
{
std::vector<std::string> stylesPizza {"cheese", "clam", "pepperoni", "veggie"};
PizzaStore *nyStore = new NYPizzaStore();
PizzaStore *chicagoStore = new ChicagoPizzaStore();
Pizza *pizza = NULL;
for (const std::string &style : stylesPizza)
{
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;
delete chicagoStore;
return 0;
}