简单工厂,工厂方法和抽象工厂三种设计模式的区别与C++实现

2013年3月31日 由 Creater 留言 »

1.简单工厂:简单工厂无需定义抽象工厂类,一般是依靠case,或者if..else..加以判断来产生不同的产品对象。总的来说为“将产品抽象出来,工厂类无需抽象,全部产品都由这个工厂类产生”;

2.工厂方法:针对每一个产品都定义一个派生工厂类,派生工厂类中有一个方法(函数)来产生产品对象,所以叫做工厂方法。总的来说为“将产品抽象出来,将工厂抽象出来,一个工厂子类来产生一个产品子类对象”;

3.抽象工厂:抽象程度更高,工厂方法将所有产品都抽象成几个基类。 如果有多个基类的话,则针对这一系列商品就需要抽象工厂。抽象工厂把几种产品抽象共同的东西,把相互依赖的对象抽象出来,只要实现这些接口就可以得到不同的产品.

下面以宝马(BMW)车和奔驰(BENZ)车为例,这两个品牌的车又有商务(Business)用车和跑车(Sport)(运动型车)之分。

简单工厂

product.h

#ifndef _PRODUCT_H_
#define _PRODUCT_H_
class Product
{
public:
	virtual ~Product();
	Product();
};
class BMWBusiness : public Product
{
public:
	BMWBusiness();
};
class BMWSport : public Product
{
public:
	BMWSport();
};
class BENZBusiness : public Product
{
public:
	BENZBusiness();
};
class BENZSport : public Product
{
public:
	BENZSport();
};
#endif

product.cpp

#include "product.h"

#include
using namespace std;
Product::Product(){}
Product::~Product(){}
BMWBusiness::BMWBusiness()
{
	cout<<"宝马商务车已经产出"<<endl;
}
BMWSport::BMWSport()
{
	cout<<"宝马跑车已经产出"<<endl;
}
BENZBusiness::BENZBusiness()
{
	cout<<"奔驰商务车已经产出"<<endl;
}
BENZSport::BENZSport()
{
	cout<<"奔驰跑车已经产出"<<endl; }

factory.h

#ifndef _FACTORY_H_
#define _FACTORY_H_
#include
using namespace std;
class Product;
class Factory
{
public:
	Factory();
	Product* makeProduct(string name);
};
#endif

factory.cpp

#include "factory.h"
#include "product.h"
Factory::Factory(){}
Product* Factory::makeProduct(string name)
{
	if(name == "BMWBusiness") return new BMWBusiness();
	else if(name == "BMWSport") return new BMWSport();
	else if(name == "BENZBusiness") return new BENZBusiness();
	else if(name == "BENZSport") return new BENZSport();
	else throw("没有这种车");
}

main.cpp

#include "product.h"
#include "factory.h"

int main()
{
	Factory *factory = new Factory();
	factory->makeProduct("BMWBusiness");
	factory->makeProduct("BENZSport");
}

代码下载连接creater
缺点:如果想扩展商品很容易,直接继承Product就可以。但是需要在工厂类中增加分支选择。

工厂方法

product.h与priduct.cpp与上面的代码相同。

factory.h

#ifndef _FACTORY_H_
#define _FACTORY_H_
#include
using namespace std;
class Product;
class Factory
{
public:
	Factory();
	~Factory();
	virtual Product* makeProduct();
};
class BMWBusinessFactory : public Factory
{
public:
	Product* makeProduct();
};
class BMWSportFactory : public Factory
{
public:
	Product* makeProduct();
};
class BENZBusinessFactory : public Factory
{
public:
	Product* makeProduct();
};
class BENZSportFactory : public Factory
{
public:
	Product* makeProduct();
};
#endif
#include "factory.h"
#include "product.h"
Factory::Factory(){}
Factory::~Factory(){}
Product* Factory::makeProduct(){}
Product* BMWBusinessFactory::makeProduct() {return new BMWBusiness();}
Product* BMWSportFactory::makeProduct() {return new BMWSport();}
Product* BENZBusinessFactory::makeProduct() {return new BENZBusiness();}
Product* BENZSportFactory::makeProduct() {return new BENZSport();}

main.cpp

#include "product.h"
#include "factory.h"

int main()
{
	Factory *factory = new BENZBusinessFactory();
	Product *product = factory->makeProduct();
	delete product;
	delete factory;
}

缺点:想增加商品很容易,直接继承Product就可以了,然后再针对添加的商品增加工厂子类,即继承factory即可。不会修改以前固定类的代码。但是所有的商品都必须是有共同的基类。

代码下载连接creater1

抽象工厂

可以这样想象:前面的工厂方法,简单工厂都将奔驰车,宝马车放在一个工厂里生产,从实际意义出发这就是不合常理的,因为什么都能生产的话那就是山寨工厂了。理论上应该宝马车公司生产宝马系列车,不管是跑车还是商务车,奔驰车也一样。
product.h

#ifndef _PRODUCT_H_
#define _PRODUCT_H_
//商务车
class BusinessProduct
{
public:
	virtual ~BusinessProduct();
	BusinessProduct();
};
//运动型跑车
class SportProduct
{
public:
	virtual ~SportProduct();
	SportProduct();
};

class BMWBusiness : public BusinessProduct
{
public:
	BMWBusiness();
};
class BMWSport : public SportProduct
{
public:
	BMWSport();
};
class BENZBusiness : public BusinessProduct
{
public:
	BENZBusiness();
};
class BENZSport : public SportProduct
{
public:
	BENZSport();
};
#endif

product.cpp

#include "product.h"

#include
using namespace std;
BusinessProduct::BusinessProduct(){}
BusinessProduct::~BusinessProduct(){}
SportProduct::SportProduct(){}
SportProduct::~SportProduct(){}
BMWBusiness::BMWBusiness()
{
	cout<<"宝马商务车已经产出"<<endl;
}
BMWSport::BMWSport()
{
	cout<<"宝马跑车已经产出"<<endl;
}
BENZBusiness::BENZBusiness()
{
	cout<<"奔驰商务车已经产出"<<endl;
}
BENZSport::BENZSport()
{
	cout<<"奔驰跑车已经产出"<<endl;
}

factory.h

#ifndef _FACTORY_H_
#define _FACTORY_H_
#include
using namespace std;
class SportProduct;
class BusinessProduct;
class Factory
{
public:
	Factory();
	~Factory();
	//生产商务车
	virtual BusinessProduct* makeBusinessProduct();
	//生产跑车
	virtual SportProduct* makeSportProduct();
};
//宝马车公司
class BMWFactory : public Factory
{
public:
	BusinessProduct* makeBusinessProduct();
	SportProduct* makeSportProduct();
};
//奔驰车公司
class BENZFactory : public Factory
{
public:
	BusinessProduct* makeBusinessProduct();
	SportProduct* makeSportProduct();
};
#endif

factory.cpp

#include "factory.h"
#include "product.h"
Factory::Factory(){}
Factory::~Factory(){}
BusinessProduct* Factory::makeBusinessProduct(){}
SportProduct* Factory::makeSportProduct(){}

//宝马车公司生产的车系
BusinessProduct* BMWFactory::makeBusinessProduct() {return new BMWBusiness();}
SportProduct* BMWFactory::makeSportProduct() {return new BMWSport();}
//奔驰车公司生产的车系
BusinessProduct* BENZFactory::makeBusinessProduct() {return new BENZBusiness();}
SportProduct* BENZFactory::makeSportProduct() {return new BENZSport();}

main.cpp

#include "product.h"
#include "factory.h"

int main()
{
	//定义奔驰工厂
	Factory *factory = new BENZFactory();
	//奔驰工厂生存的商务车
	BusinessProduct *product1 = factory->makeBusinessProduct();
	SportProduct *product2 = factory->makeSportProduct();
	delete product2;
	delete product1;
	delete factory;
}

至此,宝马车与奔驰车不再是山寨工厂里的一锅端,而是正品货了。
缺点:可能就算抽象度更高,相对来说理解需要时间。
下载连接creater3

广告位

发表评论

你必须 登陆 方可发表评论.