首页 > 代码库 > 大话设计模式C++实现-第8章-工厂方法模式

大话设计模式C++实现-第8章-工厂方法模式

一、UML图

技术分享

二、概念

工厂方法模式(Factory Method):定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法是一个类的实例化延迟到其子类。


三、包括的角色

(1)抽象工厂

(2)详细工厂

(3)抽象产品

(4)详细产品


四、优势

(1)工厂方法模式是对简单工厂模式的略微的改进。工厂方法模式的用意是定义一个创建产品对象的工厂接口,将实际工作推迟到子类中。

(2)与简单工厂模式相比,制造产品的工厂类不再 仅仅有一个。而是每种详细产品类都相应一个生产它的详细工厂类。

而这些详细工厂类的共同特征再被提取出来形成一个抽象产品类,这些详细产品类都继承自这个抽象产品类。

(3)当须要添加一种产品的时候,须要做的是:添加一种继承自抽象产品的详细产品类。添加一种继承在抽象工厂的详细工厂类。更改client。而不须要在简单工厂模式中那样更改工厂内的switch。


五、C++实现

(1)计算器的样例

#include <iostream>
#include <cstdlib>

using namespace std;

//抽象产品类
class Operation
{
protected:
	double numberA;
	double numberB;

public:
	double getA()
	{
		return numberA;
	}
	double getB()
	{
		return numberB;
	}
	void setA(double number)
	{
		numberA=number;
	}
	void setB(double number)
	{
		numberB=number;
	}

	virtual double GetResult()
	{
		double result=0;
		return result;
	}
};
//以下是四个详细产品类
class OperationAdd:public Operation
{
public:
	double GetResult()
	{
		double result=0;
		result=numberA+numberB;
		return result;
	}
};

class OperationSub:public Operation
{
public:
	double GetResult()
	{
		double result=0;
		result=numberA-numberB;
		return result;
	}
};

class OperationMul:public Operation
{
public:
	double GetResult()
	{
		double result=0;
		result=numberA*numberB;
		return result;
	}
};

class OperationDiv:public Operation
{
public:
	double GetResult()
	{
		double result=0;
		if(numberB!=0)
			result=numberA/numberB;
		return result;
	}
};

//抽象工厂类
class AbstractFactory
{
public:
	virtual Operation* createOperation()
	{
		return new Operation;
	}
};
//以下是四个详细工厂类。分别用于产生四个详细产品
class AddFactory:public AbstractFactory
{
public:
	Operation* createOperation()
	{
		Operation* oper=new OperationAdd;
		return oper;
	}
};

class SubFactory:public AbstractFactory
{
public:
	Operation* createOperation()
	{
		Operation* oper=new OperationSub;
		return oper;
	}
};

class MulFactory:public AbstractFactory
{
public:
	Operation* createOperation()
	{
		Operation* oper=new OperationMul;
		return oper;
	}
};

class DivFactory:public AbstractFactory
{
public:
	Operation* createOperation()
	{
		Operation* oper=new OperationDiv;
		return oper;
	}
};

//client
void main()
{
	AbstractFactory* af=NULL;
	af=new AddFactory();
	
	Operation* oper=NULL;
	oper=af->createOperation();
	
	oper->setA(1);
	oper->setB(2);
	cout<<oper->GetResult()<<endl;
	
	if(af!=NULL)
	{
		delete af;
		af=NULL;
	}
	if(oper!=NULL)
	{
		delete oper;
		oper=NULL;
	}

	system("pause");
}



(2)雷锋工厂的样例

#include <iostream>
#include <cstdlib>

using namespace std;

//抽象产品类:雷锋
class Leifeng
{
public:
	virtual void Sweep()
	{
		cout<<"扫地"<<endl;
	}
	virtual void Wash()
	{
		cout<<"洗衣"<<endl;
	}
	virtual void BuyRice()
	{
		cout<<"买米"<<endl;
	}
};

//以下是两个详细产品类
class Undergraduate:public Leifeng
{
public:
	void Sweep()
	{
		cout<<"学生-扫地"<<endl;
	}
	void Wash()
	{
		cout<<"学生-洗衣"<<endl;
	}
	void BuyRice()
	{
		cout<<"学生-买米"<<endl;
	}
};

class Volunteer:public Leifeng
{
public:
	void Sweep()
	{
		cout<<"志愿者-扫地"<<endl;
	}
	void Wash()
	{
		cout<<"志愿者-洗衣"<<endl;
	}
	void BuyRice()
	{
		cout<<"志愿者-买米"<<endl;
	}
};

//抽象工厂类
class AbstractFactory
{
public:
	virtual Leifeng* CreateLeifeng()
	{
		return new Leifeng;
	}
};

//以下是两个详细工厂类,分别于两个详细产品相相应
class UndergraduateFactory:public AbstractFactory
{
public:
	Undergraduate* CreateLeifeng()
	{
		return new Undergraduate;
	}
};

class VolunteerFactory:public AbstractFactory
{
public:
	Volunteer* CreateLeifeng()
	{
		return new Volunteer();
	}
};

//client
void main()
{
	//想要生产Volunteer产品的话,仅仅须要将此处的UndergraduateFactory更改为VolunteerFactory就可以。
	AbstractFactory* af=NULL;
	af=new VolunteerFactory;
	
	Leifeng* lf=NULL;
	lf=af->CreateLeifeng();

	lf->BuyRice();
	lf->Sweep();
	lf->Wash();

	if(af!=NULL)
	{
		delete af;
		af=NULL;
	}
	if(lf!=NULL)
	{
		delete lf;
		lf=NULL;
	}

	system("pause");
}

(3)执行截图

技术分享


大话设计模式C++实现-第8章-工厂方法模式