工厂模式

工厂模式属于创建型模式,大致可以分为三类,简单工厂模式、工厂方法模式、抽象工厂模式。

头文件:

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值