1.工厂需求
2.代码实现
schoolfactory.h
#ifndef _SCHOOLFACTORY_H__
#define _SCHOOLFACTORY_H__
#include <map>
#include <vector>
#include <string>
class CSchool;
typedef CSchool* (*CreateClass)();
typedef std::vector<CSchool*> VECSCHOOL;
typedef std::map<std::string, CreateClass> MAPCREATESCHOOL;
class CSchoolFactory
{
private:
VECSCHOOL m_vecSaveSchool;
MAPCREATESCHOOL m_mapCreateSchool;
public:
static CSchoolFactory* GetInstance();
CSchool* CreateSchool(std::string strSchoolTypeName);
void RegisterSchool(std::string strSchoolTypeName, CreateClass pfnCreateClass);
void DestorySchool();
public:
static CSchoolFactory* m_s_pobjFactory;
class CFreeSchoolFactory
{
public:
~CFreeSchoolFactory()
{
if (nullptr != CSchoolFactory::m_s_pobjFactory)
{
delete CSchoolFactory::m_s_pobjFactory;
}
}
};
static CFreeSchoolFactory m_s_objFreeFactory;
private:
CSchoolFactory();
~CSchoolFactory();
CSchoolFactory(const CSchoolFactory&);//禁类赋值
CSchoolFactory& operator=(const CSchoolFactory&);//禁类拷贝
};
/*申明*/
#define DECLARE_SCHOOL(SchoolTypeName) \
public: \
static CSchool* CreateSchool();
/*定义*/
#define IMPLEMENT_SCHOOL(SchoolTypeName) \
CSchool* SchoolTypeName::CreateSchool() { return new SchoolTypeName; }
/*类比静态成员变量的申明和定义*/
/*注册*/
#define REGISTER_SCHOOL(SchoolTypeName) \
CSchoolFactory::GetInstance()->RegisterSchool(#SchoolTypeName, (CreateClass)SchoolTypeName::CreateSchool);
/*内部注册*/
#define INNER_REGISTER_SCHOOL(SchoolTypeName) \
RegisterSchool(#SchoolTypeName, (CreateClass)SchoolTypeName::CreateSchool);
#endif/*_SCHOOLFACTORY_H__*/
schoolfactory.cpp
#include "stdafx.h"
#include "schoolfactory.h"
#include "school.h"
CSchoolFactory::CSchoolFactory()
{
INNER_REGISTER_SCHOOL(CSchool);
INNER_REGISTER_SCHOOL(CPrimary);
INNER_REGISTER_SCHOOL(CSecondary);
INNER_REGISTER_SCHOOL(CSenior);
INNER_REGISTER_SCHOOL(CUniversity);
}
CSchoolFactory::~CSchoolFactory()
{
}
CSchool* CSchoolFactory::CreateSchool(std::string strSchoolTypeName)
{
MAPCREATESCHOOL::iterator iter = m_mapCreateSchool.find(strSchoolTypeName);
if (m_mapCreateSchool.end() != iter)
{
CSchool *pobjSchool = (iter->second)();
m_vecSaveSchool.push_back(pobjSchool);
return pobjSchool;
}
return nullptr;
}
void CSchoolFactory::RegisterSchool(std::string strSchoolTypeName, CreateClass pfnCreateClass)
{
m_mapCreateSchool.insert(MAPCREATESCHOOL::value_type(strSchoolTypeName, pfnCreateClass));
}
CSchoolFactory* CSchoolFactory::GetInstance()
{
return m_s_pobjFactory;
}
void CSchoolFactory::DestorySchool()
{
for (int iNum = 0; iNum < m_vecSaveSchool.size(); iNum++)
{
if (nullptr != m_vecSaveSchool[iNum])
{
delete m_vecSaveSchool[iNum];
}
}
}
/*定义*/
CSchoolFactory* CSchoolFactory::m_s_pobjFactory = new CSchoolFactory();
CSchoolFactory::CFreeSchoolFactory CSchoolFactory::m_s_objFreeFactory;
school.h
#ifndef _SCHOOL_H__
#define _SCHOOL_H__
#include "schoolfactory.h"
/*==========学校公用类===================*/
class CSchool
{
DECLARE_SCHOOL(CSchool)
protected:
int m_iGradeNum;
public:/*common characteristics*/
virtual int GetGradeNum();
virtual void SetGradeNum(int iGradeNum);
/*common attribute set*/
virtual void Test();
public:
CSchool();
virtual ~CSchool();
};
/*============小学类====================*/
class CPrimary : public CSchool
{
DECLARE_SCHOOL(CPrimary)
public:
virtual void Test();
public:
CPrimary();
virtual ~CPrimary();
};
/*============初中类====================*/
class CSecondary : public CSchool
{
DECLARE_SCHOOL(CSecondary)
public:
virtual void Test();
public:
CSecondary();
virtual ~CSecondary();
};
/*============高中类====================*/
class CSenior : public CSchool
{
DECLARE_SCHOOL(CSenior)
public:
virtual void Test();
public:
CSenior();
virtual ~CSenior();
};
/*============大学类====================*/
class CUniversity : public CSchool
{
DECLARE_SCHOOL(CUniversity)
public:
virtual void Test();
public:
CUniversity();
virtual ~CUniversity();
};
#endif /*_SCHOOL_H__*/
school.cpp
#include "stdafx.h"
#include "school.h"
IMPLEMENT_SCHOOL(CSchool)
CSchool::CSchool()
{
}
CSchool::~CSchool()
{
}
int CSchool::GetGradeNum()
{
printf("m_iGradeNum: %d\n", m_iGradeNum);
return m_iGradeNum;
}
void CSchool::SetGradeNum(int iGradeNum)
{
m_iGradeNum = iGradeNum;
}
void CSchool::Test()
{
printf("school type: CSchool\n");
}
/*============小学类====================*/
IMPLEMENT_SCHOOL(CPrimary)
CPrimary::CPrimary()
{
}
CPrimary::~CPrimary()
{
}
void CPrimary::Test()
{
printf("school type: CPrimary\n");
}
/*============初中类====================*/
IMPLEMENT_SCHOOL(CSecondary)
CSecondary::CSecondary()
{
}
CSecondary::~CSecondary()
{
}
void CSecondary::Test()
{
printf("school type: CSecondary\n");
}
/*============高中类====================*/
IMPLEMENT_SCHOOL(CSenior)
CSenior::CSenior()
{
}
CSenior::~CSenior()
{
}
void CSenior::Test()
{
printf("school type: CSenior\n");
}
/*============大学类====================*/
IMPLEMENT_SCHOOL(CUniversity)
CUniversity::CUniversity()
{
}
CUniversity::~CUniversity()
{
}
void CUniversity::Test()
{
printf("school type: CUniversity\n");
}
main.cpp
// FactoryTest.cpp : 定义控制台应用程序的入口点。
#include "stdafx.h"
#include "school.h"
#include "schoolfactory.h"
#define SCHOOL "CSchool"
#define PRIMARY "CPrimary"
#define SECONDARY "CSecondary"
#define SENIOR "CSenior"
#define UNIVERSITY "CUniversity"
int _tmain(int argc, _TCHAR* argv[])
{
CPrimary* pobjPrimary = static_cast<CPrimary*>(CSchoolFactory::GetInstance()->CreateSchool(PRIMARY));
CSecondary* pobjSecondary = static_cast<CSecondary*>(CSchoolFactory::GetInstance()->CreateSchool(SECONDARY));
CSenior* pobjSenior = static_cast<CSenior*>(CSchoolFactory::GetInstance()->CreateSchool(SENIOR));
CUniversity* pobjUniversity = static_cast<CUniversity*>(CSchoolFactory::GetInstance()->CreateSchool(UNIVERSITY));
pobjPrimary->Test();
pobjSecondary->Test();
pobjSenior->Test();
pobjUniversity->Test();
CPrimary* pobjTest1 = static_cast<CPrimary*>(CSchoolFactory::GetInstance()->CreateSchool(PRIMARY));
CPrimary* pobjTest2 = static_cast<CPrimary*>(CSchoolFactory::GetInstance()->CreateSchool(PRIMARY));
pobjTest1->SetGradeNum(3);
pobjTest2->SetGradeNum(4);
pobjTest1->GetGradeNum();
pobjTest2->GetGradeNum();
CSchoolFactory::GetInstance()->DestorySchool();
return 0;
}
3.实现结果