工厂模式属于创建型模式,大致可以分为三类,简单工厂模式、工厂方法模式、抽象工厂模式。
头文件:
#pragma once
#include <memory>
using namespace std;
enum CTYPE {COREA, COREB};
struct SingleCore{
virtual void show() = 0;
};
struct SingleCoreA : public SingleCore {
void show();
};
struct SingleCoreB : public SingleCore {
void show();
};
/***************************
* 简单工厂模式
****************************/
struct Factory {
shared_ptr<SingleCore> createSingleCore(enum CTYPE type);
};
/***************************
* 抽象工厂模式
****************************/
struct AbstructFactory {
virtual shared_ptr<SingleCore> createSingleCore() = 0;
};
struct FactoryA : public AbstructFactory {
shared_ptr<SingleCore> createSingleCore();
};
struct FactoryB : public AbstructFactory {
shared_ptr<SingleCore> createSingleCore();
};
/***************************
* 工厂方法模式
****************************/
struct MultiCore {
virtual void show() = 0;
};
struct MultiCoreA : public MultiCore {
void show();
};
struct MultiCoreB : public MultiCore {
void show();
};
struct FactoryMethod {
virtual shared_ptr<SingleCore> createSingleCore() = 0;
virtual shared_ptr<MultiCore> createMultiCore() = 0;
};
struct FactoryMethodA : public FactoryMethod {
shared_ptr<SingleCore> createSingleCore();
shared_ptr<MultiCore> createMultiCore();
};
struct FactoryMethodB : public FactoryMethod {
shared_ptr<SingleCore> createSingleCore();
shared_ptr<MultiCore> createMultiCore();
};
void factoryTest(void);
cpp文件:
#include "factory.h"
#include <iostream>
void SingleCoreA::show()
{
cout << "SingleCoreA" << endl;
}
void SingleCoreB::show()
{
cout << "SingleCoreB" << endl;
}
shared_ptr<SingleCore> Factory::createSingleCore(enum CTYPE type)
{
SingleCore *pCore = NULL;
if (COREA == type)
{
pCore = new SingleCoreA();
}
else
{
pCore = new SingleCoreB();
}
return shared_ptr<SingleCore>(pCore);
}
shared_ptr<SingleCore> FactoryA::createSingleCore()
{
SingleCore* pCore = new SingleCoreA();
return shared_ptr<SingleCore>(pCore);
}
shared_ptr<SingleCore> FactoryB::createSingleCore()
{
SingleCore* pCore = new SingleCoreB();
return shared_ptr<SingleCore>(pCore);
}
void MultiCoreA::show()
{
cout << "MultiCoreA" << endl;
}
void MultiCoreB::show()
{
cout << "MultiCoreB" << endl;
}
shared_ptr<SingleCore> FactoryMethodA::createSingleCore()
{
SingleCore* pCore = new SingleCoreA();
return shared_ptr<SingleCore>(pCore);
}
shared_ptr<MultiCore> FactoryMethodA::createMultiCore()
{
MultiCore* pCore = new MultiCoreA();
return shared_ptr<MultiCore>(pCore);
}
shared_ptr<SingleCore> FactoryMethodB::createSingleCore()
{
SingleCore* pCore = new SingleCoreB();
return shared_ptr<SingleCore>(pCore);
}
shared_ptr<MultiCore> FactoryMethodB::createMultiCore()
{
MultiCore* pCore = new MultiCoreB();
return shared_ptr<MultiCore>(pCore);
}
/***************************
* 测试代码
****************************/
void factoryTest()
{
shared_ptr<SingleCore> pCoreA = Factory().createSingleCore(COREA);
pCoreA->show();
shared_ptr<SingleCore> pCoreB = Factory().createSingleCore(COREB);
pCoreB->show();
shared_ptr<SingleCore> pCoreAA = FactoryA().createSingleCore();
pCoreAA->show();
shared_ptr<SingleCore> pCoreBB = FactoryB().createSingleCore();
pCoreBB->show();
shared_ptr<SingleCore> pSingleCoreA = FactoryMethodA().createSingleCore();
pSingleCoreA->show();
shared_ptr<MultiCore> pMultiCoreB = FactoryMethodB().createMultiCore();
pMultiCoreB->show();
}
简单工厂——一对多(一个工厂对应多个产品);工厂模式——一对一(一个工厂对应一个产品):抽象工厂——多对多(多个工厂分别对应多个产品)
原文链接:https://blog.csdn.net/wuzhekai1985/article/details/6660462#commentsedit