设计模式C++学习笔记之七(AbstractFactory抽象工厂模式)
抽象工厂,提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类。对于工厂方法来说,抽象工厂可实现一系列产品的生产,抽象工厂更注重产品的组合。
看代码:
7.1.解释
main(),女娲
IHuman,产品接口
CYellowHuman,抽象产品之一
CYellowFemaleHuman,具体产品之一
CYellowMaleHuman,具体产品之二
CWhiteHuman,抽象产品之二
CWhiteFemaleHuman,具体产品之三
CWhiteMaleHuman,具体产品之四
CBlackHuman,抽象产品之三
CBlackFemaleHuman,具体产品之五
CBlackMaleHuman,具体产品之六
IHumanFactory,抽象工厂
CStandardHumanFactory,抽象工厂基类(此类可有可无)
CFemaleHumanFactory,工厂之一
CMaleHumanFactory,工厂之二
//IHuman.h
#pragma once
class IHuman
{
public:
IHuman(void)
{
}
virtual ~IHuman(void)
{
}
virtual void Laugh() = 0;
virtual void Cry() = 0;
virtual void Talk() = 0;
virtual void Sex() = 0;
};
//YellowHuman.h
#pragma once
#include "ihuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CYellowHuman :
public IHuman
{
public:
CYellowHuman(void)
{
}
~CYellowHuman(void)
{
}
void Laugh()
{
cout << "黄色人种会大笑,幸福呀!" << endl;
}
void Cry()
{
cout << "黄色人种会哭" << endl;
}
void Talk()
{
cout << "黄色人种会说话,一般说的都是双字节" << endl;
}
virtual void Sex() = 0;
};
//YellowFemaleHuman.h
#pragma once
#include "yellowhuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CYellowFemaleHuman :
public CYellowHuman
{
public:
CYellowFemaleHuman(void)
{
}
~CYellowFemaleHuman(void)
{
}
void Sex()
{
cout << "该黄种人的性别为女..." << endl;
}
};
//YellowMaleHuman.h
#pragma once
#include "yellowhuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CYellowMaleHuman :
public CYellowHuman
{
public:
CYellowMaleHuman(void)
{
}
~CYellowMaleHuman(void)
{
}
void Sex()
{
cout << "该黄种人的性别为男..." << endl;
}
};
//WhiteHuman.h
#pragma once
#include "ihuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CWhiteHuman :
public IHuman
{
public:
CWhiteHuman(void)
{
}
~CWhiteHuman(void)
{
}
void Laugh()
{
cout << "白色人种会大笑,侵略的笑声" << endl;
}
void Cry()
{
cout << "白色人种会哭" << endl;
}
void Talk()
{
cout << "白色人种会说话,一般都是单字节" << endl;
}
virtual void Sex() = 0;
};
//WhiteFemaleHuman.h
#pragma once
#include "whitehuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CWhiteFemaleHuman :
public CWhiteHuman
{
public:
CWhiteFemaleHuman(void)
{
}
~CWhiteFemaleHuman(void)
{
}
void Sex()
{
cout << "该白种人的性别为女..." << endl;
}
};
//WhiteMaleHuman.h
#pragma once
#include "whitehuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CWhiteMaleHuman :
public CWhiteHuman
{
public:
CWhiteMaleHuman(void)
{
}
~CWhiteMaleHuman(void)
{
}
void Sex()
{
cout << "该白种人的性别为男..." << endl;
}
};
//BlackHuman.h
#pragma once
#include "ihuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CBlackHuman :
public IHuman
{
public:
CBlackHuman(void)
{
}
~CBlackHuman(void)
{
}
void Laugh()
{
cout << "黑人会笑" << endl;
}
void Cry()
{
cout << "黑人会哭" << endl;
}
void Talk()
{
cout << "黑人可以说话,一般人听不懂" << endl;
}
virtual void Sex() = 0;
};
//BlackFemaleHuman.h
#pragma once
#include "blackhuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CBlackFemaleHuman :
public CBlackHuman
{
public:
CBlackFemaleHuman(void)
{
}
~CBlackFemaleHuman(void)
{
}
void Sex()
{
cout << "该黑种人的性别为女..." << endl;
}
};
//BlackMaleHuman.h
#pragma once
#include "blackhuman.h"
#include <iostream>
using std::cout;
using std::endl;
class CBlackMaleHuman :
public CBlackHuman
{
public:
CBlackMaleHuman(void)
{
}
~CBlackMaleHuman(void)
{
}
void Sex()
{
cout << "该黑种人的性别为男..." << endl;
}
};
//IHumanFactory.h
#pragma once
#include "IHuman.h"
class IHumanFactory
{
public:
IHumanFactory(void)
{
}
virtual ~IHumanFactory(void)
{
}
virtual IHuman * CreateYellowHuman() = 0;
virtual IHuman * CreateWhiteHuman() = 0;
virtual IHuman * CreateBlackHuman() = 0;
};
//StandardHumanFactory.h
#pragma once
#include "ihumanfactory.h"
#include "IHuman.h"
template<class T>
class CStandardHumanFactory :
public IHumanFactory
{
public:
CStandardHumanFactory(void)
{
}
~CStandardHumanFactory(void)
{
}
IHuman * CreateHuman()
{
return new T;
}
};
//MaleHumanFactory.h
#pragma once
#include "standardhumanfactory.h"
#include "IHumanFactory.h"
template<class T>
class CMaleHumanFactory :
public CStandardHumanFactory<T>
{
public:
CMaleHumanFactory(void);
~CMaleHumanFactory(void);
IHuman * CreateYellowHuman();
IHuman * CreateWhiteHuman();
IHuman * CreateBlackHuman();
};
//MaleHumanFactory.cpp
#include "StdAfx.h"
#include "MaleHumanFactory.h"
template<class T>
CMaleHumanFactory<T>::CMaleHumanFactory(void)
{
}
template<class T>
CMaleHumanFactory<T>::~CMaleHumanFactory(void)
{
}
template<class T>
IHuman * CMaleHumanFactory<T>::CreateYellowHuman()
{
return CreateHuman();
}
template<class T>
IHuman * CMaleHumanFactory<T>::CreateWhiteHuman()
{
return CreateHuman();
}
template<class T>
IHuman * CMaleHumanFactory<T>::CreateBlackHuman()
{
return CreateHuman();
}
//FemaleHumanFactory.h
#pragma once
#include "standardhumanfactory.h"
template<class T>
class CFemaleHumanFactory :
public CStandardHumanFactory<T>
{
public:
CFemaleHumanFactory(void)
{
}
~CFemaleHumanFactory(void)
{
}
IHuman * CreateYellowHuman()
{
return CreateHuman();
}
IHuman * CreateWhiteHuman()
{
return CreateHuman();
}
IHuman * CreateBlackHuman()
{
return CreateHuman();
}
};
//AbstractFactory.cpp
#include "stdafx.h"
#include "IHuman.h"
#include "IHumanFactory.h"
#include "FemaleHumanFactory.h"
#include "MaleHumanFactory.h"
#include "MaleHumanFactory.cpp"
#include "YellowFemaleHuman.h"
#include "YellowMaleHuman.h"
#include "WhiteFemaleHuman.h"
#include "WhiteMaleHuman.h"
#include "BlackFemaleHuman.h"
#include "BlackMaleHuman.h"
void DoIt()
{
IHumanFactory *pFemaleHumanFactory = new CFemaleHumanFactory<CYellowFemaleHuman>();
IHuman *pYellowFemaleHuman = pFemaleHumanFactory->CreateYellowHuman();
pYellowFemaleHuman->Cry();
pYellowFemaleHuman->Laugh();
pYellowFemaleHuman->Talk();
pYellowFemaleHuman->Sex();
delete pYellowFemaleHuman;
delete pFemaleHumanFactory;
IHumanFactory *pMaleHumanFactory = new CMaleHumanFactory<CYellowMaleHuman>();
IHuman *pYellowMaleHuman = pMaleHumanFactory->CreateYellowHuman();
pYellowMaleHuman->Cry();
pYellowMaleHuman->Laugh();
pYellowMaleHuman->Talk();
pYellowMaleHuman->Sex();
delete pYellowMaleHuman;
delete pMaleHumanFactory;
}
int _tmain(int argc, _TCHAR* argv[])
{
DoIt();
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF | _CRTDBG_ALLOC_MEM_DF);
_CrtDumpMemoryLeaks();
return 0;
}
以上类图是本例子中所用到的类的相关图,有助于理解程序实现。抽象工厂属于创建型模式。主要用于生产一系列产品,每一个工厂也可以由单件来实现。这里使用模板方式来传递参数,使代码更简洁,但Java或.Net里面的反射方式,在C++还是不能真正实现。