设计模式——工厂方法模式(Factory Method Pattern)

来源:互联网 发布:南京旅游 知乎 编辑:程序博客网 时间:2024/05/16 07:29

工厂方法模式不同于简单工厂模式的地方在于工厂方法模式把对象的创建过程放到里子类里。这样工厂父对象和产品父对象一样,可以是抽象类或者接口,只定义相应的规范或操作,不涉及具体的创建或实现细节。

 其类图如下:


 实例代码为:

[cpp] view plain copy
  1. #pragma once  
  2. class IProduct  
  3. {  
  4. public:  
  5.     IProduct(void);  
  6.     virtual ~IProduct(void);  
  7. };  
  8.   
  9. #pragma once  
  10. #include "iproduct.h"  
  11. class IPad :  
  12.     public IProduct  
  13. {  
  14. public:  
  15.     IPad(void);  
  16.     ~IPad(void);  
  17. };  
  18.   
  19. #pragma once  
  20. #include "iproduct.h"  
  21. class IPhone :  
  22.     public IProduct  
  23. {  
  24. public:  
  25.     IPhone(void);  
  26.     ~IPhone(void);  
  27. };  

[cpp] view plain copy
  1. #pragma once  
  2. #include"IProduct.h"  
  3.   
  4. class IFactory  
  5. {  
  6. public:  
  7.     IFactory(void);  
  8.     virtual ~IFactory(void);  
  9.   
  10.     virtual IProduct* getProduct();  
  11. };  
  12.   
  13.   
  14. #pragma once  
  15. #include "ifactory.h"  
  16. class IPadFactory :  
  17.     public IFactory  
  18. {  
  19. public:  
  20.     IPadFactory(void);  
  21.     ~IPadFactory(void);  
  22.   
  23.     virtual IProduct* getProduct();  
  24. };  
  25.   
  26.   
  27. #pragma once  
  28. #include "ifactory.h"  
  29. class IPhoneFactory :  
  30.     public IFactory  
  31. {  
  32. public:  
  33.     IPhoneFactory(void);  
  34.     ~IPhoneFactory(void);  
  35.   
  36.     virtual IProduct* getProduct();  
  37. };  

关键的实现:

[cpp] view plain copy
  1. #include "StdAfx.h"  
  2. #include "IPadFactory.h"  
  3. #include"IPad.h"  
  4.   
  5. IPadFactory::IPadFactory(void)  
  6. {  
  7. }  
  8.   
  9.   
  10. IPadFactory::~IPadFactory(void)  
  11. {  
  12. }  
  13.   
  14. IProduct* IPadFactory::getProduct()  
  15. {  
  16.     return new IPad();  
  17. }  
  18.   
  19.   
  20. #include "StdAfx.h"  
  21. #include "IPhoneFactory.h"  
  22. #include"IPhone.h"  
  23.   
  24. IPhoneFactory::IPhoneFactory(void)  
  25. {  
  26. }  
  27.   
  28.   
  29. IPhoneFactory::~IPhoneFactory(void)  
  30. {  
  31. }  
  32.   
  33.   
  34. IProduct* IPhoneFactory::getProduct()  
  35. {  
  36.     return new IPhone();  
  37. }  

调用方式:

[cpp] view plain copy
  1. #include "stdafx.h"  
  2. #include"IFactory.h"  
  3. #include"IPadFactory.h"  
  4. #include"IPhoneFactory.h"  
  5. #include"IProduct.h"  
  6.   
  7.   
  8. int _tmain(int argc, _TCHAR* argv[])  
  9. {  
  10.     IFactory *fac = new IPadFactory();  
  11.     IProduct *pro = fac->getProduct();  
  12.   
  13.     fac = new IPhoneFactory();  
  14.     pro = fac->getProduct();  
  15.     return 0;  
  16. }  


应用场景:

1.      .net里面的数据库连接对象就是产生数据命令对象的工厂。每种数据库的connection对象里(继承自IDbConnection)都有对自己createCommand(定义在IDbCommand里)的实现。

2.      .net里面的迭代器,IEnumerable定义了迭代器的接口,即工厂方法,每一个继承自IEnumerable的类都要实现GetEnumerator。可以参看ArrayList,String的GetEnumerator方法。他们都继承自IEnumerable。


参考资料:

1.Dot Net设计模式—工厂方法模式 http://fineboy.cnblogs.com/archive/2005/08/04/207459.html

2.工厂方法模式 http://www.cnblogs.com/cbf4life/archive/2009/12/20/1628494.html


LCL_data原创于CSDN.Net【http://blog.csdn.net/lcl_data/article/details/8712834】

6
0 0
原创粉丝点击