C++ 三种工厂模式

工厂模式是将带有继承于基类的子类的创建过程交于一个工厂来创建,通过赋予不同的创建标识来创建不同的子类。

基于自己的理解和使用这里巩固一下工厂模式。

我们的项目目前使用最多的是简单工厂模式,不过其他两种模式:工厂模式和抽象工厂模式都是由简单工厂模式改进而来,

也很容易使用。

话不多说:见代码

一、简单工厂模式:

操作类: 接口类:CReadDocumentShowHandler,三个具体类:CReadWordShowHandler,CReadPdfShowHandler,CReadHtmlShowHandler继承于CReadDocumentShowHandler

工厂类:CReadDocumentFactory 工厂类通过成员函数CReadDocumentShowHandler * CreateReadDocHandler( int type );

创建对象,type指定具体对对象的类型。

类图:

C++ 三种工厂模式

操作类代码:
 #pragma once
#include <iostream> class CReadDocumentShowHandler
{
public:
CReadDocumentShowHandler();
virtual ~CReadDocumentShowHandler();
public:
virtual bool ReadDocumentShow() = ;
}; typedef enum ReadType
{
WORD,
HTML,
PDF,
UNKNOWN
}; #pragma once
#include "ReadDocumentShowHandler.h" class CReadHtmlShowHandler :
public CReadDocumentShowHandler
{
public:
CReadHtmlShowHandler();
virtual ~CReadHtmlShowHandler();
public:
virtual bool ReadDocumentShow();
};
#pragma once
#include "ReadDocumentShowHandler.h" class CReadPdfShowHandler :
public CReadDocumentShowHandler
{
public:
CReadPdfShowHandler();
virtual ~CReadPdfShowHandler();
public:
virtual bool ReadDocumentShow();
};
#pragma once
#include "ReadDocumentShowHandler.h" class CReadWordShowHandler :
public CReadDocumentShowHandler
{
public:
CReadWordShowHandler();
virtual ~CReadWordShowHandler();
public:
virtual bool ReadDocumentShow();
}; #include "ReadDocumentShowHandler.h" CReadDocumentShowHandler::CReadDocumentShowHandler()
{
} CReadDocumentShowHandler::~CReadDocumentShowHandler()
{
}
#include "ReadHtmlShowHandler.h" CReadHtmlShowHandler::CReadHtmlShowHandler()
{
} CReadHtmlShowHandler::~CReadHtmlShowHandler()
{
} bool CReadHtmlShowHandler::ReadDocumentShow()
{
try
{
//operation ...
std::cout << " Read Html Document Operation ..." <<std::endl;
return true;
}
catch (...)
{
return false;
}
}
#include "ReadPdfShowHandler.h" CReadPdfShowHandler::CReadPdfShowHandler()
{
} CReadPdfShowHandler::~CReadPdfShowHandler()
{
} bool CReadPdfShowHandler::ReadDocumentShow()
{
try
{
std::cout << " Read PDF Document Operation ..." << std::endl;
return true;
}
catch (...)
{
return false;
}
}
#include "ReadWordShowHandler.h" CReadWordShowHandler::CReadWordShowHandler()
{
} CReadWordShowHandler::~CReadWordShowHandler()
{
} bool CReadWordShowHandler::ReadDocumentShow()
{
try
{
std::cout << " Read Word Document Operation ..." << std::endl;
return true;
}
catch (...)
{
return false;
}
}
工厂类代码:
 #pragma once
#include "ReadDocumentShowHandler.h"
class CReadDocumentFactory
{
public:
CReadDocumentFactory();
virtual ~CReadDocumentFactory();
public:
CReadDocumentShowHandler * CreateReadDocHandler( int type );
}; #include "ReadDocumentFactory.h"
#include "ReadWordShowHandler.h"
#include "ReadPdfShowHandler.h"
#include "ReadHtmlShowHandler.h" CReadDocumentFactory::CReadDocumentFactory()
{
} CReadDocumentFactory::~CReadDocumentFactory()
{
} CReadDocumentShowHandler * CReadDocumentFactory::CreateReadDocHandler(int type)
{
CReadDocumentShowHandler * pReadDocHandler = NULL;
switch (type)
{
case WORD:
pReadDocHandler = new CReadWordShowHandler();
break;
case HTML:
pReadDocHandler = new CReadHtmlShowHandler();
break;
case PDF:
pReadDocHandler = new CReadPdfShowHandler();
break;
default:
break;
}
return pReadDocHandler != NULL ? pReadDocHandler : NULL;
}

Client代码

 #include"ReadDocumentFactory.h"
#include"ReadDocumentShowHandler.h"
#include"ReadHtmlShowHandler.h"
#include"ReadWordShowHandler.h"
#include"ReadPdfShowHandler.h" int main()
{
CReadDocumentFactory * pDocumentFactory = new CReadDocumentFactory();
CReadDocumentShowHandler * pRDShow = pDocumentFactory->CreateReadDocHandler(WORD);
pRDShow->ReadDocumentShow();
delete pRDShow;
pRDShow = NULL;
//
system("pause");
return ;
}

二、工厂模式

工厂模式是对简单工厂模式的改进,由于之前的子类的创建都是根据type标识来创建不同的子类,所以如果子类增加,则必须在工厂创建方法中添加创建的type类型和子类类型,这违背了开放-封闭原则,所以我们,对不同的子类创建交由对应的子类工厂去创建,

即:把Switch Case分离,单独独立出CReadPdfFactory,CReadHtmlFactory,CReadWordFactory继承于CReadDocumentFactory,CReadDocumentFactory独立成接口类。

三、抽象工厂

抽象工厂是提供不同类型的类组成一个基本部件,比如一套衣服:有上衣和短裤,抽象工厂是一组类的组合创建的方式。

简单工厂和工厂模式都是指的是一类相同的类,抽象工厂是针对的不同类别的对象的组合,这点更复杂一点。所以在Factory的创建中会有创建一组对线的成员函数,ClassA * CreateClassA();Class B CreateClassB();具体的子类工厂去实现接口成员。

这里给出一个简单的类图:

C++ 三种工厂模式

上一篇:基于Netty和SpringBoot实现一个轻量级RPC框架-Client篇


下一篇:pc版qq登录及移动版qq登录的申请过程