Dough.h
#ifndef _DOUGH_H
#define _DOUGH_H class Dough
{
};
#endif
ThinCrustDough.h
#ifndef _THIN_CRUST_DOUGH_H
#define _THIN_CRUST_DOUGH_H #include "Dough.h" class ThinCrustDough : public Dough
{
};
#endif
Sauce.h
#ifndef _SAUCE_H
#define _SAUCE_H class Sauce
{
};
#endif
MarinaraSauce.h
#ifndef _MARINARA_SAUCE_H
#define _MARINARA_SAUCE_H #include "Sauce.h" class MarinaraSauce : public Sauce
{
};
#endif
Pizza.h
#ifndef _PIZZA_H
#define _PIZZA_H
#include <iostream>
#include <string>
#include "Dough.h"
#include "Sauce.h"
class Pizza
{
public:
Pizza() : m_name(), m_p_dough(NULL), m_p_sauce(NULL) {}
virtual ~Pizza() {}
virtual void prepare() = ;
virtual void bake() { std::cout << "Bake for 25 mins at 350" << std::endl; }
virtual void cut() { std::cout << "Cutting the pizza into diagonal slices" << std::endl; }
virtual void box() { std::cout << "Place pizza in official PizzaStore box" << std::endl; }
void set_name(const std::string &name) { m_name = name; }
std::string get_name() { return m_name; }
Dough *m_p_dough;
Sauce *m_p_sauce;
private:
std::string m_name;
};
#endif
CheesePizza.h
#ifndef _CHEESE_PIZZA_H
#define _CHEESE_PIZZA_H #include "Pizza.h"
#include "PizzaIngredientFactory.h" class CheesePizza : public Pizza
{
private:
PizzaIngredientFactory *m_p_ingredient_factory;
public:
CheesePizza(PizzaIngredientFactory *p) : m_p_ingredient_factory(p) {}
void prepare()
{
std::cout << "Preparing " << get_name() << std::endl;
m_p_sauce = m_p_ingredient_factory->create_sauce();
}
}; #endif
GreekPizza.h
#ifndef _GREEK_PIZZA_H
#define _GREEK_PIZZA_H #include "Pizza.h"
#include "PizzaIngredientFactory.h" class GreekPizza : public Pizza
{
private:
PizzaIngredientFactory *m_p_ingredient_factory;
public:
GreekPizza(PizzaIngredientFactory *p) : m_p_ingredient_factory(p) {}
void prepare()
{
std::cout << "Preparing " << get_name() << std::endl;
m_p_dough = m_p_ingredient_factory->create_dough();
}
}; #endif
PizzaStore.h
#ifndef _PIZZA_STORE_H
#define _PIZZA_STORE_H #include "Pizza.h" class PizzaStore
{
private:
virtual Pizza* CreatePizza(const std::string &type) = ;
public:
Pizza* OrderPizza(const std::string &type)
{
Pizza *p_pizza = CreatePizza(type);
if (p_pizza)
{
p_pizza->prepare();
p_pizza->bake();
p_pizza->cut();
p_pizza->box();
}
return p_pizza;
}
};
#endif
NYPizzaStore.h
#ifndef _NY_PIZZA_STORE_H
#define _NY_PIZZA_STORE_H #include "PizzaStore.h"
#include "CheesePizza.h"
#include "GreekPizza.h"
#include "NYPizzaIngredientFactory.h"
class NYPizzaStore : public PizzaStore
{
private:
Pizza* CreatePizza(const std::string &type)
{
PizzaIngredientFactory *p_factory = new NYPizzaIngredientFactory();
if ( "cheese" == type )
{
Pizza *p_pizza = new CheesePizza( p_factory );
p_pizza->set_name("New York Style Cheese Pizza");
return p_pizza;
}
if ( "greek" == type )
{
Pizza *p_pizza = new GreekPizza( p_factory );
p_pizza->set_name("New York Style Greek Pizza");
return p_pizza;
}
return NULL;
}
};
#endif
PizzaIngredientFactory.h
#ifndef _PIZZA_INGREDIENT_FACTORY_H
#define _PIZZA_INGREDIENT_FACTORY_H #include "Dough.h"
#include "Sauce.h" class PizzaIngredientFactory
{
public:
virtual Dough* create_dough() = ;
virtual Sauce* create_sauce() = ;
}; #endif
NYPizzaIngredientFactory.h
#ifndef _NY_PIZZA_INGREDIENT_FACTORY_H
#define _NY_PIZZA_INGREDIENT_FACTORY_H #include "ThinCrustDough.h"
#include "MarinaraSauce.h" class NYPizzaIngredientFactory : public PizzaIngredientFactory
{
public:
Dough* create_dough() { std::cout << "Creating Thin Crust Dough" << std::endl; return new ThinCrustDough(); }
Sauce* create_sauce() { std::cout << "Creating Marinara Sauce" << std::endl; return new MarinaraSauce(); }
}; #endif
main.cpp
#include "NYPizzaStore.h"
int main()
{
NYPizzaStore pizza_store;
Pizza *p_pizza = pizza_store.OrderPizza("greek");
if ( p_pizza )
{
delete p_pizza;
}
return ;
}