C++ 三種工廠模式


工廠模式是將帶有繼承於基類的子類的創建過程交於一個工廠來創建,通過賦予不同的創建標識來創建不同的子類。

基於自己的理解和使用這里鞏固一下工廠模式。

我們的項目目前使用最多的是簡單工廠模式,不過其他兩種模式:工廠模式和抽象工廠模式都是由簡單工廠模式改進而來,

也很容易使用。

話不多說:見代碼

一、簡單工廠模式:

操作類: 接口類:CReadDocumentShowHandler,三個具體類:CReadWordShowHandlerCReadPdfShowHandlerCReadHtmlShowHandler繼承於CReadDocumentShowHandler

工廠類:CReadDocumentFactory 工廠類通過成員函數CReadDocumentShowHandler * CreateReadDocHandler( int type );

創建對象,type指定具體對對象的類型。

類圖:

操作類代碼:
 1 #pragma once
 2 #include <iostream>
 3 
 4 class CReadDocumentShowHandler  5 {  6 public:  7  CReadDocumentShowHandler();  8     virtual ~CReadDocumentShowHandler();  9 public:  10     virtual bool ReadDocumentShow() = 0;  11 };  12 
 13 typedef enum ReadType  14 {  15  WORD,  16  HTML,  17  PDF,  18  UNKNOWN  19 };  20 
 21 #pragma once
 22 #include "ReadDocumentShowHandler.h"
 23 
 24 class CReadHtmlShowHandler :  25     public CReadDocumentShowHandler  26 {  27 public:  28  CReadHtmlShowHandler();  29     virtual ~CReadHtmlShowHandler();  30 public:  31     virtual  bool ReadDocumentShow();  32 };  33 #pragma once
 34 #include "ReadDocumentShowHandler.h"
 35 
 36 class CReadPdfShowHandler :  37     public CReadDocumentShowHandler  38 {  39 public:  40  CReadPdfShowHandler();  41     virtual ~CReadPdfShowHandler();  42 public:  43     virtual  bool ReadDocumentShow();  44 };  45 #pragma once
 46 #include "ReadDocumentShowHandler.h"
 47 
 48 class CReadWordShowHandler :  49     public CReadDocumentShowHandler  50 {  51 public:  52  CReadWordShowHandler();  53     virtual ~CReadWordShowHandler();  54 public:  55     virtual  bool ReadDocumentShow();  56 };  57 
 58 #include "ReadDocumentShowHandler.h"
 59 
 60 
 61 CReadDocumentShowHandler::CReadDocumentShowHandler()  62 {  63 }  64 
 65 
 66 CReadDocumentShowHandler::~CReadDocumentShowHandler()  67 {  68 }  69 #include "ReadHtmlShowHandler.h"
 70 
 71 
 72 CReadHtmlShowHandler::CReadHtmlShowHandler()  73 {  74 }  75 
 76 
 77 CReadHtmlShowHandler::~CReadHtmlShowHandler()  78 {  79 }  80 
 81 bool CReadHtmlShowHandler::ReadDocumentShow()  82 {  83     try
 84  {  85         //operation ...
 86         std::cout << " Read Html Document Operation ..." <<std::endl;  87         return true;  88  }  89     catch (...)  90  {  91         return false;  92  }  93 }  94 #include "ReadPdfShowHandler.h"
 95 
 96 
 97 CReadPdfShowHandler::CReadPdfShowHandler()  98 {  99 } 100 
101 
102 CReadPdfShowHandler::~CReadPdfShowHandler() 103 { 104 } 105 
106 bool CReadPdfShowHandler::ReadDocumentShow() 107 { 108     try
109  { 110         std::cout << " Read PDF Document Operation ..." << std::endl; 111         return true; 112  } 113     catch (...) 114  { 115         return false; 116  } 117 } 118 #include "ReadWordShowHandler.h"
119 
120 
121 CReadWordShowHandler::CReadWordShowHandler() 122 { 123 } 124 
125 
126 CReadWordShowHandler::~CReadWordShowHandler() 127 { 128 } 129 
130 bool CReadWordShowHandler::ReadDocumentShow() 131 { 132     try
133  { 134         std::cout << " Read Word Document Operation ..." << std::endl; 135         return true; 136  } 137     catch (...) 138  { 139         return false; 140  } 141 }

工廠類代碼:

 

 1 #pragma once
 2 #include "ReadDocumentShowHandler.h"
 3 class CReadDocumentFactory  4 {  5 public:  6  CReadDocumentFactory();  7     virtual ~CReadDocumentFactory();  8 public:  9     CReadDocumentShowHandler * CreateReadDocHandler( int type ); 10 }; 11 
12 
13 
14 #include "ReadDocumentFactory.h"
15 #include "ReadWordShowHandler.h"
16 #include "ReadPdfShowHandler.h"
17 #include "ReadHtmlShowHandler.h"
18 
19 CReadDocumentFactory::CReadDocumentFactory() 20 { 21 } 22 
23 
24 CReadDocumentFactory::~CReadDocumentFactory() 25 { 26 } 27 
28 CReadDocumentShowHandler * CReadDocumentFactory::CreateReadDocHandler(int type) 29 { 30     CReadDocumentShowHandler * pReadDocHandler = NULL; 31     switch (type) 32  { 33     case WORD: 34         pReadDocHandler = new CReadWordShowHandler(); 35         break; 36     case HTML: 37         pReadDocHandler = new CReadHtmlShowHandler(); 38         break; 39     case PDF: 40         pReadDocHandler = new CReadPdfShowHandler(); 41         break; 42     default: 43         break; 44  } 45     return pReadDocHandler != NULL ? pReadDocHandler : NULL; 46 }

Client代碼

 

 1 #include"ReadDocumentFactory.h"
 2 #include"ReadDocumentShowHandler.h"
 3 #include"ReadHtmlShowHandler.h"
 4 #include"ReadWordShowHandler.h"
 5 #include"ReadPdfShowHandler.h"
 6 
 7 
 8 int main()  9 { 10     CReadDocumentFactory * pDocumentFactory = new CReadDocumentFactory(); 11     CReadDocumentShowHandler * pRDShow = pDocumentFactory->CreateReadDocHandler(WORD); 12     pRDShow->ReadDocumentShow(); 13     delete pRDShow; 14     pRDShow = NULL; 15     // 16     system("pause"); 17     return 0; 18 }

 

 

二、工廠模式

工廠模式是對簡單工廠模式的改進,由於之前的子類的創建都是根據type標識來創建不同的子類,所以如果子類增加,則必須在工廠創建方法中添加創建的type類型和子類類型,這違背了開放-封閉原則,所以我們,對不同的子類創建交由對應的子類工廠去創建,

即:把Switch Case分離,單獨獨立出CReadPdfFactoryCReadHtmlFactory,CReadWordFactory繼承於CReadDocumentFactory,CReadDocumentFactory獨立成接口類。

三、抽象工廠

抽象工廠是提供不同類型的類組成一個基本部件,比如一套衣服:有上衣和短褲,抽象工廠是一組類的組合創建的方式。

簡單工廠和工廠模式都是指的是一類相同的類,抽象工廠是針對的不同類別的對象的組合,這點更復雜一點。所以在Factory的創建中會有創建一組對線的成員函數,ClassA * CreateClassA();Class B CreateClassB();具體的子類工廠去實現接口成員。

這里給出一個簡單的類圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM