关闭

C++抽象基类的实例化(抽象工厂模式)

标签: c++函数设计模式class
274人阅读 评论(0) 收藏 举报

今天学习设计模式《抽象工厂模式》,参考了两个博客:

http://blog.csdn.net/wuzhekai1985/article/details/6660462

http://www.cnblogs.com/wanggary/archive/2011/04/13/2015288.html

然后按照第二个博客的意见,应用类模板实现工厂类;中间出现一个不能实例化抽象基类的问题,通过查资料,然后另外新建一个工程实验后知道:

1,抽象类是不能初始化的(最基本的,大家都知道的)

2,如果抽象基类的派生类有哪怕只有一个函数没有实现,这个抽象基类仍然不能实例化;即需要将抽象基类中定义的虚接口函数全部在派生类中实现(说的不太专业。。。)

代码如下:

//core.h
#ifndef _CORE_H_
#define _CORE_H_

class Core{
public:
	Core(){}
	virtual ~Core(){}
	virtual void NumOfCore() = 0;
	virtual void SpeedOfCore() = 0;
	virtual void VersionOfCore() = 0;
};
#endif
<pre name="code" class="cpp">//multicore.h
#ifndef _MULTICORE_H_
#define _MULTICORE_H_

#include"core.h"
#include<iostream>
using namespace std;

class MultiCore :public Core{
public:
	MultiCore(){}
	~MultiCore(){}
	void NumOfCore(){
		cout << "multicore" << endl;
	}
	void SpeedOfCore(){
		cout << "speed fast" << endl;
	}
	virtual void VersionOfCore() = 0;
};
#endif
<pre name="code" class="cpp">//singlecore.h
#ifndef _SINGLECORE_H_
#define _SINGLECORE_H_

#include"core.h"
#include<iostream>
using namespace std;

class SingleCore :public Core{
public:
	SingleCore(){}
	~SingleCore(){}
	void NumOfCore(){
		cout << "singlecore" << endl;
	}
	void SpeedOfCore(){
		cout << "speed slow" << endl;
	}
	virtual void VersionOfCore() = 0;
};
#endif
<pre name="code" class="cpp">//newmulticore.h
#ifndef _NEWMULTICORE_H_
#define _NEWMULTICORE_H_

#include"multicore.h"

class NewMultiCore :public MultiCore{
public:
	NewMultiCore(){}
	~NewMultiCore(){}
	void VersionOfCore(){
		cout << "new multi version core" << endl;
	}
};

#endif
<pre name="code" class="cpp">//newsinglecore.h
#ifndef _NEWSINGLECORE_H_
#define _NEWSINGLECORE_H_

#include"singlecore.h"

class NewSingleCore :public SingleCore{
public:
	NewSingleCore(){}
	~NewSingleCore(){}
	void VersionOfCore(){
		cout << "new single version core" << endl;
	}
};
#endif
<pre name="code" class="cpp">//oldmulticore.h
#ifndef _OLDMULTICORE_H_
#define _OLDMULTICORE_H_

#include"multicore.h"

class OldMultiCore :public MultiCore{
public:
	OldMultiCore(){}
	~OldMultiCore(){}
	void VersionOfCore(){
		cout << "old multi version core" << endl;
	}
};

#endif
<pre name="code" class="cpp">//oldsinglecore.h
#ifndef _OLDSINGLECORE_H_
#define _OLDSINGLECORE_H_

#include"singlecore.h"

class OldSingleCore :public SingleCore{
public:
	OldSingleCore(){}
	~OldSingleCore(){}
	void VersionOfCore(){
		cout << "old single version core" << endl;
	}
};

#endif
<pre name="code" class="cpp">//factory.h
#ifndef _FACTORY_H_
#define _FACTORY_H_

#include"core.h"

class Factory{
public:
	Factory(){}
	virtual ~Factory(){}
	virtual Core* CreateSingleCore() = 0;
	virtual Core* CreateMultiCore() = 0;
};

#endif

<pre name="code" class="cpp">//newversionfactory.h
#ifndef _NEWVERSIONFACTORY_H_
#define _NEWVERSIONFACTORY_H_

#include"factory.h"

template<class T>
class NewVersionFactory :public Factory
{
public:
	NewVersionFactory();
	~NewVersionFactory();
	Core* CreateMultiCore();
	Core* CreateSingleCore();
};

template<class T>
NewVersionFactory<T>::NewVersionFactory(){}
template<class T>
NewVersionFactory<T>::~NewVersionFactory(){}
template<class T>
Core* NewVersionFactory<T>::CreateMultiCore()
{
	return new T;
}
template<class T>
Core* NewVersionFactory<T>::CreateSingleCore()
{
	return new T;
}

#endif
//oldversionfactory.h
#ifndef _OLDVERSIONFACTORY_H_
#define _OLDVERSIONFACTORY_H_

#include"factory.h"

template<class T>
class OldVersionFactory :public Factory
{
public:
	OldVersionFactory();
	~OldVersionFactory();
	Core* CreateMultiCore();
	Core* CreateSingleCore();
};

template<class T>
OldVersionFactory<T>::OldVersionFactory(){}
template<class T>
OldVersionFactory<T>::~OldVersionFactory(){}
template<class T>
Core* OldVersionFactory<T>::CreateMultiCore()
{
	return new T;
}
template<class T>
Core* OldVersionFactory<T>::CreateSingleCore()
{
	return new T;
}

#endif
//main.cpp
#include"core.h"
#include"factory.h"
#include"multicore.h"
#include"singlecore.h"
#include"newversionfactory.h"
#include"oldversionfactory.h"
#include"newmulticore.h"
#include"newsinglecore.h"
#include"oldmulticore.h"
#include"oldsinglecore.h"

void main()
{
	Factory* pFactoryMultiCore = new NewVersionFactory<NewMultiCore>();
	pFactoryMultiCore->CreateMultiCore()->NumOfCore();
	pFactoryMultiCore->CreateMultiCore()->SpeedOfCore();
	pFactoryMultiCore->CreateMultiCore()->VersionOfCore();
	delete pFactoryMultiCore;
	pFactoryMultiCore = NULL;

	Factory* pFactorySingleCore = new OldVersionFactory<OldSingleCore>();
	pFactorySingleCore->CreateSingleCore()->NumOfCore();
	pFactorySingleCore->CreateSingleCore()->SpeedOfCore();
	pFactorySingleCore->CreateSingleCore()->VersionOfCore();
	delete pFactorySingleCore;
	pFactorySingleCore = NULL;

}










   
0
0

猜你在找
【直播】机器学习&数据挖掘7周实训--韦玮
【套餐】系统集成项目管理工程师顺利通关--徐朋
【直播】3小时掌握Docker最佳实战-徐西宁
【套餐】机器学习系列套餐(算法+实战)--唐宇迪
【直播】计算机视觉原理及实战--屈教授
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之矩阵--黄博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
【直播】机器学习之凸优化--马博士
【套餐】Javascript 设计模式实战--曾亮
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4304次
    • 积分:81
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:1篇
    • 译文:0篇
    • 评论:1条
    文章存档
    最新评论