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