<Head First 设计模式>:工厂模式2:抽象工厂模式--Pizza



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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值