工厂模式是将带有继承于基类的子类的创建过程交于一个工厂来创建,通过赋予不同的创建标识来创建不同的子类。
基于自己的理解和使用这里巩固一下工厂模式。
我们的项目目前使用最多的是简单工厂模式,不过其他两种模式:工厂模式和抽象工厂模式都是由简单工厂模式改进而来,
也很容易使用。
话不多说:见代码
一、简单工厂模式:
操作类: 接口类:CReadDocumentShowHandler,三个具体类:CReadWordShowHandler,CReadPdfShowHandler,CReadHtmlShowHandler继承于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分离,单独独立出CReadPdfFactory,CReadHtmlFactory,CReadWordFactory继承于CReadDocumentFactory,CReadDocumentFactory独立成接口类。
三、抽象工厂
抽象工厂是提供不同类型的类组成一个基本部件,比如一套衣服:有上衣和短裤,抽象工厂是一组类的组合创建的方式。
简单工厂和工厂模式都是指的是一类相同的类,抽象工厂是针对的不同类别的对象的组合,这点更复杂一点。所以在Factory的创建中会有创建一组对线的成员函数,ClassA * CreateClassA();Class B CreateClassB();具体的子类工厂去实现接口成员。
这里给出一个简单的类图: