设计模式:抽象工厂模式(4.2)C++版

抽象工厂模式:创建一族相关的产品。


C++示例代码如下:

#include "stdafx.h"

/*
* CONTENTS: DESIGN PATTERN, ABSTRACT FACTORY PATTERN
*   AUTHOR: YAO H. WANG
*     TIME: 2013-11-1 17:25:29
*  EDITION: 1
*
* ALL RIGHTS RESERVED!
*/

#include <string>
#include <list>
#include <iostream>

using namespace std;

//调料
class Dough
{
public:
	virtual void print() =0;
};

class ThickCrustDough: public Dough
{
public:
	void print()
	{
		cout << "ThickCrustDough" << endl;
	}
};

class ThinCrustDough: public Dough
{
public:
	void print()
	{
		cout << "ThinCrustDough" << endl;
	}
};

class Sauce
{
public:
	virtual void print() =0;
};

class PlumTomatoSauce: public Sauce
{
public:
	void print()
	{
		cout << "PlumTomatoSauce" << endl;
	}
};

class MarinaraSauce: public Sauce
{
public:
	void print()
	{
		cout << "MarinaraSauce" << endl;
	}
};

class Cheese
{
public:
	virtual void print() =0;
};

class MozzarellaCheese: public Cheese
{
public:
	void print()
	{
		cout << "MozzarellaCheese" << endl;
	}
};

class ReggianoCheese: public Cheese
{
public:
	void print()
	{
		cout << "ReggianoCheese" << endl;
	}
};

class Veggies
{
public:
	virtual void print() = 0;
};

class Garlic: public Veggies
{
public:
	void print()
	{
		cout << "Garlic" << endl;
	}
};

class Onion: public Veggies
{
public:
	void print()
	{
		cout << "Onion" << endl;
	}
};

class Mushroom: public Veggies
{
public:
	void print()
	{
		cout << "Mushroom" << endl;
	}
};

class RedPepper: public Veggies
{
public:
	void print()
	{
		cout << "RedPepper" << endl;
	}
};

class BlackOlives: public Veggies
{
public:
	void print()
	{
		cout << "BlackOlives" << endl;
	}
};

class Spinach: public Veggies
{
public:
	void print()
	{
		cout << "Spinach" << endl;
	}
};

class EggPlant: public Veggies
{
public:
	void print()
	{
		cout << "EggPlant" << endl;
	}
};

class Pepperoni
{
	virtual void print() = 0;
};

class SlicePepperoni: public Pepperoni
{
	void print()
	{
		cout << "SlicePepperoni" << endl;
	}
};

class Clams
{
public:
	virtual void print() = 0;
};

class FrozenClams: public Clams
{
public:
	void print()
	{
		cout << "FrozenClams" << endl;
	}
};

class FreshClams: public Clams
{
public:
	void print()
	{
		cout << "FreshClams" << endl;
	}
};

//调料工厂
class PizzaIngredientFactory
{
public:
	virtual Dough* createDough() = 0;
	virtual Sauce* createSauce() = 0;
	virtual Cheese* createCheese() = 0;
	virtual list<Veggies*> createVeggies() = 0;
	virtual Pepperoni* createPepperoni() = 0;
	virtual Clams* createClams() = 0;
};

class NYPizzaIngredientFactory: public PizzaIngredientFactory
{
public:
	Dough* createDough()
	{
		return new ThinCrustDough();
	}

	Sauce* createSauce()
	{
		return new MarinaraSauce();
	}

	Cheese* createCheese()
	{
		return new ReggianoCheese();
	}

	list<Veggies*> createVeggies()
	{
		list<Veggies*> lv;
		lv.push_back(new Garlic());
		lv.push_back(new Onion());
		lv.push_back(new Mushroom());
		lv.push_back(new RedPepper());
		return lv;
	}

	Pepperoni* createPepperoni()
	{
		return new SlicePepperoni();
	}

	Clams* createClams()
	{
		return new FreshClams();
	}
};

class ChicagoPizzaIngredientFactory: public PizzaIngredientFactory
{
public:
	Dough* createDough()
	{
		return new ThickCrustDough();
	}

	Sauce* createSauce()
	{
		return new PlumTomatoSauce();
	}

	Cheese* createCheese()
	{
		return new MozzarellaCheese();
	}

	list<Veggies*> createVeggies()
	{
		list<Veggies*> lv;
		lv.push_back(new BlackOlives());
		lv.push_back(new Spinach());
		lv.push_back(new EggPlant());
		return lv;
	}

	Pepperoni* createPepperoni()
	{
		return new SlicePepperoni();
	}

	Clams* createClams()
	{
		return new FrozenClams();
	}
};

//产品
class Pizza
{
public:
	string name;
	Dough *dough;
	Sauce *sauce;
	list<Veggies*> lv ;
	Cheese *cheese;
	Pepperoni *pepperoni;
	Clams *clam;

	virtual void prepare() = 0;

	virtual void bake()
	{
		cout << "Bake for 25 minutes at 350" << endl;
	}

	virtual void cut()
	{
		cout << "Cutting the pizza into diagonal slices" << endl;
	}

	virtual void box()
	{
		cout << "Place pizza in official PizzaStore box" << endl;
	}

	void setName(string name)
	{
		this->name = name;
	}

	string getName()
	{
		return name;
	}
};

class CheesePizza: public Pizza
{
private:
	PizzaIngredientFactory *ingredientFactory;

public:
	CheesePizza(PizzaIngredientFactory *ingredientFactory)
	{
		this->ingredientFactory = ingredientFactory;
	}

	void prepare()
	{
		cout << "Preparing " << getName() << endl;
		dough = ingredientFactory->createDough();
		sauce = ingredientFactory->createSauce();
		cheese = ingredientFactory->createCheese();
	}
};

class ClamPizza: public Pizza
{
private:
	PizzaIngredientFactory *ingredientFactory;

public:
	ClamPizza(PizzaIngredientFactory *ingredientFactory)
	{
		this->ingredientFactory = ingredientFactory;
	}

	void prepare()
	{
		cout << "Preparing " << getName() << endl;
		dough = ingredientFactory->createDough();
		sauce = ingredientFactory->createSauce();
		cheese = ingredientFactory->createCheese();
		clam = ingredientFactory->createClams();
	}
};

class VeggiePizza: public Pizza
{
private:
	PizzaIngredientFactory *ingredientFactory;

public:
	VeggiePizza(PizzaIngredientFactory *ingredientFactory)
	{
		this->ingredientFactory = ingredientFactory;
	}

	void prepare()
	{
		cout << "Preparing " << getName() << endl;
		dough = ingredientFactory->createDough();
		sauce = ingredientFactory->createSauce();
		cheese = ingredientFactory->createCheese();
		lv = ingredientFactory->createVeggies();
	}
};

class PepperoniPizza: public Pizza
{
private:
	PizzaIngredientFactory *ingredientFactory;

public:
	PepperoniPizza(PizzaIngredientFactory *ingredientFactory)
	{
		this->ingredientFactory = ingredientFactory;
	}

	void prepare()
	{
		cout << "Preparing " << getName() << endl;
		dough = ingredientFactory->createDough();
		sauce = ingredientFactory->createSauce();
		cheese = ingredientFactory->createCheese();
		pepperoni = ingredientFactory->createPepperoni();
	}
};

//工厂
class PizzaStore
{
public:
	Pizza* orderPizza(string type)
	{
		Pizza *pizza;

		pizza = createPizza(type);

		pizza->prepare();
		pizza->bake();
		pizza->cut();
		pizza->box();

		return pizza;
	}

private:
	virtual Pizza* createPizza(string type) = 0;
};

class NYPizzaStore: public PizzaStore
{
	Pizza* createPizza(string type)
	{
		Pizza *pizza = NULL;
		PizzaIngredientFactory *ingredientFactory = new NYPizzaIngredientFactory();

		if("cheese" == type)
		{
			pizza = new CheesePizza(ingredientFactory);
			pizza->setName("New York Style Cheese Pizza");
		}
		else if("veggie" == type)
		{
			pizza = new VeggiePizza(ingredientFactory);
			pizza->setName("New York Style Veggie Pizza");
		}
		else if("clam" == type)
		{
			pizza = new ClamPizza(ingredientFactory);
			pizza->setName("New York Style Clam Pizza");
		}
		else if("pepperoni" == type)
		{
			pizza = new PepperoniPizza(ingredientFactory);
			pizza->setName("New York Style Pepperoni Pizza");
		}
		return pizza;
	}
};

class ChicagoPizzaStore: public PizzaStore
{
	Pizza* createPizza(string type)
	{
		Pizza *pizza = NULL;
		PizzaIngredientFactory *ingredientFactory = new ChicagoPizzaIngredientFactory();

		if("cheese" == type)
		{
			pizza = new CheesePizza(ingredientFactory);
			pizza->setName("Chicago Style Cheese Pizza");
		}
		else if("veggie" == type)
		{
			pizza = new VeggiePizza(ingredientFactory);
			pizza->setName("Chicago Style Veggie Pizza");
		}
		else if("clam" == type)
		{
			pizza = new ClamPizza(ingredientFactory);
			pizza->setName("Chicago Style Clam Pizza");
		}
		else if("pepperoni" == type)
		{
			pizza = new PepperoniPizza(ingredientFactory);
			pizza->setName("Chicago Style Pepperoni Pizza");
		}
		return pizza;
	}
};

//测试
void main()
{
	PizzaStore *nyStore = new NYPizzaStore();
	PizzaStore *chicagoStore = new ChicagoPizzaStore();

	Pizza *pizza = nyStore->orderPizza("cheese");
	cout << "Ethan ordered a " + pizza->getName() << endl;

	pizza = chicagoStore->orderPizza("veggie");
	cout << "Joel ordered a " + pizza->getName() << endl;

	delete nyStore;
	delete chicagoStore;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值