您的位置:首页 > 其它

设计模式:1.2 工厂模式

2015-08-12 01:24 295 查看
工厂模式:

一个工厂专门用来产生A型号的单核,另一个工厂专门用来生产B型号的单核。客户要做的就是找好工厂,比如要A型号的核,就找A工厂要;否则找B工厂要,不再需要告诉工厂要什么型号的处理器核了。



工厂模式缺点:每增加一种产品,需要增加一个对象的工厂。相比简单工厂,需要更多的类定义



工厂方法:

定义创建对象的接口,让子类决定实例化哪一个类。工厂方法使类的实例化延迟到其子类。



优点:增加新功能只需要增加新工厂和新产品类

没有修改的变化,只有扩展的变化。工厂方法是简单工厂的进一步抽象和推广。



缺点:将逻辑从工厂搬到客户端,想要加功能,本来改工厂类的,现在修改客户端。





main.cpp

#include <iostream>
#include <stdlib.h>
#include "FactoryA.h"
#include "FactoryB.h"
#include <memory>

using namespace std;

/*工厂模式缺点:
每增加一种产品,就需要增加一个对象的工厂。相比简单工厂,需要更多的类。
*/
void process()
{
	FactoryA factoryA;
	shared_ptr<SingleCore> ptrA = factoryA.instance();
	if(ptrA)
	{
		ptrA->show();
	}
	FactoryB factoryB;
	shared_ptr<SingleCore> ptrB = factoryB.instance();
	if(ptrB)
	{
		ptrB->show();
	}
}

int main(int argc,char* argv[])
{
	process();
	system("pause");
	return 0;
}



Factory.h

#ifndef FACTORY_H
#define FACTORY_H
#include <memory>
#include "SingleCore.h"
class Factory
{
public:
	Factory(void);
	virtual ~Factory(void);
	virtual std::shared_ptr<SingleCore> instance()=0;
};
#endif


FactoryA.h

#ifndef FACTORYA_H
#define FACTORYA_H
#include "factory.h"
#include <memory>
class FactoryA :
	public Factory
{
public:
	FactoryA(void);
	~FactoryA(void);
	std::shared_ptr<SingleCore> instance();
};
#endif


FactoryB.h

#ifndef FACTORYB_H
#define FACTORYB_H
#include "factory.h"
#include <memory>
class FactoryB :
	public Factory
{
public:
	FactoryB(void);
	~FactoryB(void);
	std::shared_ptr<SingleCore> instance();
};
#endif


FactoryA.cpp

#include "FactoryA.h"
#include "SingleCoreA.h"

FactoryA::FactoryA(void)
{
}

FactoryA::~FactoryA(void)
{
}

//生产A核的工厂
std::shared_ptr<SingleCore> FactoryA::instance()
{
	std::shared_ptr<SingleCore> ptrA(new SingleCoreA);
	return ptrA;
}


FactoryB.cpp

#include "FactoryB.h"
#include "SingleCoreB.h"

FactoryB::FactoryB(void)
{
}

FactoryB::~FactoryB(void)
{
}

std::shared_ptr<SingleCore> FactoryB::instance()
{
	std::shared_ptr<SingleCore> ptrB(new SingleCoreB);
	return ptrB;
}


SingleCore.h

#ifndef SINGLECORE_H
#define SINGLECORE_H
class SingleCore
{
public:
	SingleCore(void);
	virtual ~SingleCore(void);
	virtual void show()=0;
};
#endif


SingleCoreA.h

#ifndef SINGLECOREA_H
#define SINGLECOREA_H
#include "singlecore.h"
class SingleCoreA :
	public SingleCore
{
public:
	SingleCoreA(void);
	~SingleCoreA(void);
	void show();
};
#endif


SingleCoreB.h

#ifndef SINGLECOREB_H
#define SINGLECOREB_H
#include "singlecore.h"
class SingleCoreB :
	public SingleCore
{
public:
	SingleCoreB(void);
	~SingleCoreB(void);
	void show();
};
#endif



SingleCoreA.cpp

#include "SingleCoreA.h"
#include <iostream>

using namespace std;

SingleCoreA::SingleCoreA(void)
{
}

SingleCoreA::~SingleCoreA(void)
{
}

void SingleCoreA::show()
{
	cout << "单核处理器A" << endl;
}


SingleCoreB.cpp

#include "SingleCoreB.h"
#include <iostream>

using namespace std;

SingleCoreB::SingleCoreB(void)
{
}

SingleCoreB::~SingleCoreB(void)
{
}

void SingleCoreB::show()
{
	cout << "单核处理器B" << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: