#ifndef __INGREDIENT_H__
#define __INGREDIENT_H__
#include <iostream>
using namespace std;
class Dough
{ };
class ThinDough:public Dough
{ };
class BigDough :public Dough
{ };
class CaDough :public Dough
{ }; class Sauce
{ }; class MariSauce : public Sauce
{ }; class SariSauce :public Sauce
{ }; class Clams
{ }; class FreshClams : public Clams
{ }; class OldClams :public Clams
{ }; class IngredientFactory
{
public:
IngredientFactory(){}
virtual ~IngredientFactory(){}
virtual Dough* createDough()=0{}
virtual Sauce* createSauce()=0{}
virtual Clams* createClams()=0{}
}; class NYIngredientFactory : public IngredientFactory
{
public:
NYIngredientFactory(){}
virtual ~NYIngredientFactory(){}
virtual Dough* createDough()
{
return new ThinDough();
}
virtual Sauce* createSauce()
{
return new MariSauce();
}
virtual Clams* createClams()
{
return new FreshClams();
}
}; class ChicagoIngredientFactory : public IngredientFactory
{
public:
ChicagoIngredientFactory(){}
virtual ~ChicagoIngredientFactory(){}
virtual Dough* createDough()
{
return new BigDough();
}
virtual Sauce* createSauce()
{
return new SariSauce();
}
virtual Clams* createClams()
{
return new OldClams();
}
}; class CalifoniaIngredientFactory : public IngredientFactory
{
public:
CalifoniaIngredientFactory(){}
virtual ~CalifoniaIngredientFactory(){}
virtual Dough* createDough()
{
return new CaDough();
}
virtual Sauce* createSauce()
{
return new SariSauce();
}
virtual Clams* createClams()
{
return new FreshClams();
}
}; #endif
#ifndef __PIZZA_H__
#define __PIZZA_H__
#include "Ingredient.h"
class Pizza
{
protected:
Dough *dough;
Sauce *sauce;
Clams *clams;
public:
Pizza(){}
virtual ~Pizza(){}
virtual void prepare(){}
void bake()
{
std::cout << "bake" << std::endl;
}
void cut()
{
std::cout << "cut" << std::endl;
}
void box()
{
std::cout << "box" << std::endl;
}
}; class CheesePizza : public Pizza
{
private:
IngredientFactory *ingdFactory;
public:
CheesePizza(IngredientFactory *ifactory)
{
ingdFactory = ifactory;
} virtual ~CheesePizza(){}
virtual void prepare()
{
dough = ingdFactory->createDough();
sauce = ingdFactory->createSauce();
clams = ingdFactory->createClams();
}
}; class ClamPizza : public Pizza
{
private:
IngredientFactory *ingdFactory;
public:
ClamPizza(IngredientFactory *ifactory)
{
ingdFactory = ifactory;
} virtual ~ClamPizza(){}
virtual void prepare()
{
dough = ingdFactory->createDough();
sauce = ingdFactory->createSauce();
clams = ingdFactory->createClams();
}
}; #endif
#ifndef __STORE_H__
#define __STORE_H__
#include "Pizza.h"
class PizzaStore
{
public:
PizzaStore(){}
virtual ~PizzaStore(){}
virtual Pizza* orderPizza(string type)
{
Pizza *p = createPizza(type);
p->bake();
p->cut();
p->box();
return p;
}
private:
virtual Pizza* createPizza(string type) = 0
{ }
}; class NYPizzaStore : public PizzaStore
{
public:
NYPizzaStore(){}
virtual ~NYPizzaStore(){}
private:
virtual Pizza* createPizza(string type)
{ IngredientFactory * ingfy = new NYIngredientFactory();
Pizza *p; if (type =="cheese")
{
p = new CheesePizza(ingfy);
}
else if (type == "clams")
{
p = new ClamPizza(ingfy);
}
else p = NULL;
return p;
}
}; class ChgoPizzaStore :public PizzaStore
{
public:
ChgoPizzaStore(){}
virtual ~ChgoPizzaStore(){}
private:
virtual Pizza* createPizza(string type)
{ IngredientFactory * ingfy = new NYIngredientFactory();
Pizza *p; if (type == "cheese")
{
p = new CheesePizza(ingfy);
}
else if (type == "clams")
{
p = new ClamPizza(ingfy);
}
else p = NULL;
return p;
}
}; class CaliforniaPizzaStore : public PizzaStore
{
public:
CaliforniaPizzaStore(){}
virtual ~CaliforniaPizzaStore(){}
private:
virtual Pizza* createPizza(string type)
{
IngredientFactory * ingfy = new CalifoniaIngredientFactory();
Pizza *p; if (type == "cheese")
{
p = new CheesePizza(ingfy);
}
else if (type == "clams")
{
p = new ClamPizza(ingfy);
}
else p = NULL;
return p;
}
};
#endif
#include "store.h"
int main()
{
PizzaStore *NYstore = new NYPizzaStore();
Pizza *p = NYstore->orderPizza("cheese");
Pizza *p2 = NYstore->orderPizza("clams"); PizzaStore *CalifoniaStore = new CaliforniaPizzaStore();
Pizza *c = CalifoniaStore->orderPizza("cheese");
Pizza *c2 = CalifoniaStore->orderPizza("clams"); return 0;
}