设计模式之抽象工厂

 

首先非常感谢睡仔:工厂模式,我的所有工厂模式的理解均来自于他的博客思想。

abstractfactory.h

//设计模式之抽象工厂,此方式是在工厂方法的基础上进一步抽象的结果
/*
理解:
	世事多变,随着时间的推移,走过的地方越来越多,你天南海北的朋友也越来越多。
你发现菜原来还分了许多菜系,鲁菜、粤菜、湘菜等等,它们各有各的风味,同样
是红烧肉由不同菜系出来的味道也各不相同, 你招待不同的朋友要用不同的菜系,
这下难办了,你的厨师都是鲁菜风味,怎么办,广东的朋友来了吃不惯。现在我们
再回到简单工厂模式,我们把红烧肉再向下继承,生成鲁菜红烧肉、粤菜红烧肉、
湘菜红烧肉;清蒸鱼向下继承为鲁菜清蒸鱼、粤菜清蒸鱼、湘菜清蒸鱼,其它也以此类推。
我们也修改一下老婆的这个类,不让其返回食物基类,而是返回红烧肉、清蒸鱼、
爆炒空心菜、西红柿鸡蛋汤这一层次,并把这些方法抽象化,作为菜系工厂基类,
然后再从此基类继承出,鲁菜工厂、粤菜工厂、湘菜工厂等等,再由这些具体工厂
实现创建具体菜的工作,哈哈你如果招待广东朋友就用粤菜工厂,返回的就是一桌
粤菜菜系的红烧肉、清蒸鱼、空心菜和西红柿鸡蛋汤了,你的广东朋友一定会吃的
非常合乎胃口了。噢,非常好,你已经实现了抽象工厂模式了。
*/

//定义产品类的基类
class product//cookfood
{
protected://控制不让用户直接实例化
	product();
public:
	virtual ~product();
	//virtual void cookingfish() = 0;//做红烧鱼的接口
	//virtual void cookingbeef() = 0;//做回锅肉的接口
	virtual void cooking() = 0;//做菜的接口
};

class cookingfish : public product
{
public:
	cookingfish(){}
	~cookingfish(){}

public:
	virtual void cooking();
};

class lucaifish : public cookingfish
{
public:
	lucaifish(){};
	~lucaifish(){}
public:
	virtual void cooking();
};
class chuancaifish : public cookingfish
{
public:
	chuancaifish(){}
	~chuancaifish(){}

public:
	virtual void cooking();
};

class cookingbeef :public product
{
public:
	cookingbeef(){}
	~cookingbeef(){}
public:
	virtual void cooking();
};

class lucaibeef:public cookingbeef
{
public:
	lucaibeef(){}
	~lucaibeef(){};
public:
	virtual void cooking();
};
class chuancaibeef:public cookingbeef
{
public:
	chuancaibeef(){}
	~chuancaibeef(){}
public:
	virtual void cooking();
};

class abstractfactory
{
public:
	abstractfactory(){};
	virtual ~abstractfactory(){};

public:
	virtual cookingfish* createfish() = 0;//做红烧鱼
	virtual cookingbeef* createbeef() = 0;//做回锅肉
};

class concretefactorylucai:public abstractfactory
{
public:
	concretefactorylucai(){}
	~concretefactorylucai(){}
public:
	virtual cookingfish* createfish();
	virtual cookingbeef* createbeef();
};

class concretefactorychuancai:public abstractfactory
{
public:
	concretefactorychuancai(){}
	~concretefactorychuancai(){}

public:
	virtual cookingfish* createfish();
	virtual cookingbeef* createbeef();
};




abstractfactory.cpp

#include 
#include "abstractfactory.h"

product::product()
{

}
product::~product()
{

}

void cookingfish::cooking()
{
	return ;
}

void lucaifish::cooking()
{
	printf("cooking 鲁菜红烧鱼\n");
}

void chuancaifish::cooking()
{
	printf("cooking 川菜红烧鱼\n");
}

void cookingbeef::cooking()
{
	return ;
}
void lucaibeef::cooking()
{
	printf("cooking 鲁菜回锅肉\n");
}

void chuancaibeef::cooking()
{
	printf("cooking 川菜回锅肉\n");
}


cookingfish* concretefactorylucai::createfish()
{
	cookingfish *fish = new lucaifish();
	return fish;
}

cookingbeef* concretefactorylucai::createbeef()
{
	cookingbeef *beef = new lucaibeef();
	return beef ;
}

cookingfish* concretefactorychuancai::createfish()
{
	cookingfish *fish = new chuancaifish();
	return fish;
}

cookingbeef* concretefactorychuancai::createbeef()
{
	cookingbeef *beef = new chuancaibeef();
	return beef;
}

int main()
{
	//我想吃鲁菜
	printf("我想吃鲁菜\n");
	abstractfactory *absfct = new concretefactorylucai();
	cookingfish *fish = absfct->createfish();
	fish->cooking();
	cookingbeef *beef = absfct->createbeef();
	beef->cooking();

	delete fish;
	delete beef;
	delete absfct;

	//我想吃川菜
	printf("我想吃川菜\n");
	absfct = new concretefactorychuancai();

	fish = absfct->createfish();
	fish->cooking();
	beef = absfct->createbeef();
	beef->cooking();

	delete fish;
	delete beef;
	delete absfct;

}

本文仅附上程序,理解的重点在simplefactory.h文件,主要看类之间的结构关系

另外,这篇博客介绍的非常好,仅供参考:http://blog.csdn.net/wuzhekai1985/article/details/6660462



 

你可能感兴趣的:(设计模式,C++,学习,设计模式,class,delete,工作,产品)