C++設計形式編程中簡略工場與工場辦法形式的實例比較。本站提示廣大學習愛好者:(C++設計形式編程中簡略工場與工場辦法形式的實例比較)文章只能為提供參考,不一定能成為您想要的結果。以下是C++設計形式編程中簡略工場與工場辦法形式的實例比較正文
簡略工場形式實例
標題:完成盤算器的輸出2個數和運算符,獲得成果
工程構造:
(1)頭文件
COperationFactory.h(運算符工場類)
(2)源文件
SimpleFactory.cpp(客戶端運用類,主函數地點)
(3)運算類
COperation.cpp(運算符基類)
COperation.h
COperationAdd.h(加法運算符子類,繼續於COperation)
COperationDiv.h (除法運算符子類,繼續於COperation)
COperationMul.h (乘法運算符子類,繼續於COperation)
COperationSub.h(減法運算符子類,繼續於COperation)
============= 代碼完成部門 =============
COperationFactory.h(運算符工場類)
/************************************************************************/
/* 運算符工場類 */
/************************************************************************/
#ifndef _OPERATION_FACTORY_H_
#define _OPERATION_FACTORY_H_
#include "stdafx.h"
#include "COperation.h"
#include "COperationAdd.h"
#include "COperationSub.h"
#include "COperationMul.h"
#include "COperationDiv.h"
#include "COperationFactory.h"
class COperationFactory
{
public:
COperationFactory(){};
~COperationFactory(){};
// 依據入參的分歧,創立其對應的運算符類指針。就像是個工場,創立用戶指定的運算符類指針
static COperation* NewOperation(const string& strOperate)
{
// 入參正當性斷定,避免前面的strOperate[0]產生越界拜訪
if (strOperate.size() != 1)
{
return NULL;
}
COperation* pOperation = NULL;
switch (strOperate[0])
{
case '+':
pOperation = new COperationAdd();
break;
case '-':
pOperation = new COperationSub();
break;
case '*':
pOperation = new COperationMul();
break;
case '/':
pOperation = new COperationDiv();
break;
default:
break;
}
return pOperation;
};
};
#endif _OPERATION_FACTORY_H_
COperation.cpp(運算符基類)
#include "stdafx.h"
#include "COperation.h"
COperation::COperation()
: _dNumA(0)
, _dNumB(0)
{
}
COperation.h
/************************************************************************/
/* 運算符基類 */
/************************************************************************/
#ifndef _COPERATION_H_
#define _COPERATION_H_
class COperation
{
public:
COperation();
~COperation(){};
// 設置被運算數
void SetNumA(double dNumA)
{
_dNumA = dNumA;
};
// 獲得被運算數
double GetNumA()
{
return _dNumA;
};
// 設置運算數
void SetNumB(double dNumB)
{
_dNumB = dNumB;
};
// 獲得運算數
double GetNumB()
{
return _dNumB;
};
// 盤算成果且在子類中完成各自的運算辦法成果
virtual double Result()
{
double dResult = 0;
return dResult;
}
private:
double _dNumA;
double _dNumB;
};
#endif _COPERATION_H_
COperationAdd.h(加法運算符子類,繼續於COperation)
/************************************************************************/
/* 加法運算符子類,繼續於運算符基類 */
/************************************************************************/
#ifndef _COPERATION_ADD_H_
#define _COPERATION_ADD_H_
#include "COperation.h"
class COperationAdd : public COperation
{
public:
COperationAdd(){};
~COperationAdd(){};
double Result()
{
return (GetNumA() + GetNumB());
};
};
#endif _COPERATION_ADD_H_
COperationDiv.h (除法運算符子類,繼續於COperation)
/************************************************************************/
/* 除法運算符子類,繼續於運算符基類 */
/************************************************************************/
#ifndef _COPERATION_DIV_H_
#define _COPERATION_DIV_H_
#include "COperation.h"
class COperationDiv : public COperation
{
public:
COperationDiv(){};
~COperationDiv(){};
double Result()
{
double dResult = 0;
if (0 != GetNumB())
{
dResult = (GetNumA() / GetNumB());
}
else
{
cout << "error: divisor is ";
}
return dResult;
};
};
#endif _COPERATION_DIV_H_
COperationMul.h (乘法運算符子類,繼續於COperation)
/************************************************************************/
/* 乘法運算符子類,繼續於運算符基類 */
/************************************************************************/
#ifndef _COPERATION_MUL_H_
#define _COPERATION_MUL_H_
#include "COperation.h"
class COperationMul : public COperation
{
public:
COperationMul(){};
~COperationMul(){};
double Result()
{
return (GetNumA() * GetNumB());
};
};
#endif _COPERATION_MUL_H_
COperationSub.h(減法運算符子類,繼續於COperation)
/************************************************************************/
/* 減法運算符子類,繼續於運算符基類 */
/************************************************************************/
#ifndef _COPERATION_SUB_H_
#define _COPERATION_SUB_H_
#include "COperation.h"
class COperationSub : public COperation
{
public:
COperationSub(){};
~COperationSub(){};
double Result()
{
return (GetNumA() - GetNumB());
};
};
#endif _COPERATION_SUB_H_
SimpleFactory.cpp(客戶端運用類,主函數地點)
// SimpleFactory.cpp : 界說掌握台運用法式的進口點。
//
#include "stdafx.h"
#include "COperationFactory.h"
int _tmain(int argc, _TCHAR* argv[])
{
// 經由過程運算符工場創立加法運算
COperation* OperAdd = COperationFactory::NewOperation("+");
if (NULL != OperAdd)
{
OperAdd->SetNumA(168); // 設置被加數
OperAdd->SetNumB(105); // 設置加數
cout << "168 + 105 = " << (OperAdd->Result()) << endl;
}
// 經由過程運算符工場創立減法運算
COperation* OperSub = COperationFactory::NewOperation("-");
if (NULL != OperSub)
{
OperSub->SetNumA(168); // 設置被減數
OperSub->SetNumB(105); // 設置減數
cout << "168 - 105 = " << (OperSub->Result()) << endl;
}
// 經由過程運算符工場創立乘法運算
COperation* OperMul = COperationFactory::NewOperation("*");
if (NULL != OperMul)
{
OperMul->SetNumA(168); // 設置被乘數
OperMul->SetNumB(105); // 設置乘數
cout << "168 * 105 = " << (OperMul->Result()) << endl;
}
// 經由過程運算符工場創立除法運算
COperation* OperDiv = COperationFactory::NewOperation("/");
if (NULL != OperDiv)
{
OperDiv->SetNumA(168); // 設置被除數
OperDiv->SetNumB(105); // 設置除數
cout << "168 / 105 = " << (OperDiv->Result()) << endl;
OperDiv->SetNumB(0); // 轉變除數
cout << (OperDiv->Result()) << endl;
}
// 阻攔掌握台過程停止,便於檢查成果
int nEnd = 0;
cin >> nEnd;
return 0;
}
籠統工場形式實例
工程構造:
(1)籠統產物類
IFruit.h
(2)籠統工場類
IFruitGardener.h
(3)詳細產物類
CApple.h
CGrape.h
CStrawberry.h
(4)詳細工場類
CAppleGardener.h
CGrapeGardener.h
CStrawberryGardener.h
(5)客戶端
FactoryMethodApplication.cpp
(1)籠統產物類
IFruit.h
/************************************************************************/
/* 籠統生果類(abstract Product) */
/************************************************************************/
#ifndef _IFRUIT_H_
#define _IFRUIT_H_
#include <string>
#include <iostream>
using namespace std;
class IFruit
{
public:
virtual void grow() = 0;
virtual void harvest() = 0;
virtual void plant() = 0;
};
#endif _IFRUIT_H_
(2)籠統工場類
IFruitGardener.h
/************************************************************************/
/* 籠統生果花匠類(abstract Factory) */
/************************************************************************/
#ifndef _IFRUIT_GARDENER_H_
#define _IFRUIT_GARDENER_H_
#include "IFruit.h"
class IFruitGardener
{
public:
virtual IFruit* Factory() = 0;
};
#endif _IFRUIT_GARDENER_H_
(3)詳細產物類
CApple.h
/************************************************************************/
/* 詳細的蘋果類(Concrete Product) */
/************************************************************************/
#ifndef _APPLE_H_
#define _APPLE_H_
#include "IFruit.h"
class CApple : public IFruit
{
public:
void grow()
{
cout << "Apple is growing..." << endl;
};
void harvest()
{
cout << "Apple has been harvested." << endl;
};
void plant()
{
cout << "Apple has been planted." << endl;
};
int GetTreeAge()
{
return m_iAppleTreeAge;
};
void SetTreeAge(const int iAge)
{
m_iAppleTreeAge = iAge;
}
private:
int m_iAppleTreeAge;
};
#endif _APPLE_H_
CGrape.h
/************************************************************************/
/* 詳細的葡萄類(Concrete Product) */
/************************************************************************/
#ifndef _GRAPE_H_
#define _GRAPE_H_
#include "IFruit.h"
class CGrape : public IFruit
{
public:
void grow()
{
cout << "Grape is growing..." << endl;
};
void harvest()
{
cout << "Grape has been harvested." << endl;
};
void plant()
{
cout << "Grape has been planted." << endl;
};
bool GetSeedless()
{
return m_bSeedless;
};
void SetSeedless(const bool bSeedless)
{
m_bSeedless = bSeedless;
};
private:
bool m_bSeedless;
};
#endif _GRAPE_H_
CStrawberry.h
/************************************************************************/
/* 詳細的草莓類(Concrete Product) */
/************************************************************************/
#ifndef _STRAWBERRY_H_
#define _STRAWBERRY_H_
#include "IFruit.h"
class CStrawberry : public IFruit
{
public:
void grow()
{
cout << "Strawberry is growing..." << endl;
};
void harvest()
{
cout << "Strawberry has been harvested." << endl;
};
void plant()
{
cout << "Strawberry has been planted." << endl;
};
};
#endif _STRAWBERRY_H_
(4)詳細工場類
CAppleGardener.h
/************************************************************************/
/* 詳細的蘋果花匠類(Concrete Factory) */
/************************************************************************/
#ifndef _APPLE_GARDENER_H_
#define _APPLE_GARDENER_H_
#include "IFruitGardener.h"
#include "CApple.h"
class CAppleGardener : public IFruitGardener
{
public:
CAppleGardener():m_pApple(NULL){};
IFruit* Factory()
{
if (NULL == m_pApple)
{
m_pApple = new CApple();
}
return m_pApple;
};
private:
CApple* m_pApple;
};
#endif _APPLE_GARDENER_H_
CGrapeGardener.h
/************************************************************************/
/* 詳細的葡萄花匠類(Concrete Factory) */
/************************************************************************/
#ifndef _GRAPE_GARDENER_H_
#define _GRAPE_GARDENER_H_
#include "IFruitGardener.h"
#include "CGrape.h"
class CGrapeGardener : public IFruitGardener
{
public:
CGrapeGardener():m_pGrape(NULL){};
IFruit* Factory()
{
if (NULL == m_pGrape)
{
m_pGrape = new CGrape();
}
return m_pGrape;
};
private:
CGrape* m_pGrape;
};
#endif _GRAPE_GARDENER_H_
CStrawberryGardener.h
/************************************************************************/
/* 詳細的草莓花匠類(Concrete Factory) */
/************************************************************************/
#ifndef _STRAWBERRY_GARDENER_H_
#define _STRAWBERRY_GARDENER_H_
#include "IFruitGardener.h"
#include "CStrawberry.h"
class CStrawberryGardener : public IFruitGardener
{
public:
CStrawberryGardener():m_pStrawberry(NULL){};
IFruit* Factory()
{
if (NULL == m_pStrawberry)
{
m_pStrawberry = new CStrawberry();
}
return m_pStrawberry;
};
private:
CStrawberry* m_pStrawberry;
};
#endif _STRAWBERRY_GARDENER_H_
(5)客戶端
FactoryMethodApplication.cpp
// FactoryMethodApplication.cpp : 界說掌握台運用法式的進口點。
//
#include "stdafx.h"
#include <Windows.h>
#include "IFruitGardener.h"
#include "CAppleGardener.h"
#include "CGrapeGardener.h"
#include "CStrawberryGardener.h"
int _tmain(int argc, _TCHAR* argv[])
{
static IFruitGardener* pFruitFactory1 = NULL;
static IFruitGardener* pFruitFactory2 = NULL;
static IFruit* pFruit1 = NULL;
static IFruit* pFruit2 = NULL;
pFruitFactory1 = new CAppleGardener();
if (NULL != pFruitFactory1)
{
pFruit1 = pFruitFactory1->Factory();
if (NULL != pFruit1)
{
pFruit1->grow();
pFruit1->harvest();
pFruit1->plant();
}
}
pFruitFactory2 = new CGrapeGardener();
if (NULL != pFruitFactory2)
{
pFruit2 = pFruitFactory2->Factory();
if (NULL != pFruit2)
{
pFruit2->grow();
pFruit2->harvest();
pFruit2->plant();
}
}
Sleep(10000);
return 0;
}
總結
起首不管是簡略工場形式照樣工場辦法形式都是把不變的處所提掏出來,把輕易產生變更的封裝起來。以到達做年夜水平的復用,和順應用戶的更改,和項目標擴大。
1、簡略工場形式
1.懂得
又稱為靜態工場形式,它專門界說一個類來擔任創立其他類的實例,被創立的實例平日都具有相通的父類。由工場類依據傳入的參數靜態決議應當創立哪個產物類的實例。它包括需要的斷定邏輯,能依據外界給定的信息,決議應當穿件誰人詳細類的對象。簡略工場形式可以懂得為父親給兒子留了一筆錢,劃定這筆錢可以用於上學、買房或許買車,然後讓兒子本身選擇用於哪個。
2.長處
工場類包括需要的邏輯斷定,可以決議在甚麼時刻創立哪個類的實例,客戶端可以免直接創立對象。如許便可以完成對義務的朋分,下降耦合性,明白了詳細的職責和權利,有益於全部體系的優化。
3.缺陷
當產物具有比擬龐雜的多層構造時,它的工場類只要一個,這時候候再以不變應萬變就成為它最年夜的缺陷了。由於工場類是全部組織的焦點,它集合了一切產物的創立邏輯,一旦工場不克不及正常任務,全部體系都邑遭到影響,可擴大性較差。擴大性差一旦有新的需求,就不能不修正工場邏輯,如許就會招致工場邏輯過為龐雜,違反了開——閉准繩。同時靜態工場辦法晦氣於構成基於繼續的品級構造。
2、工場辦法形式
1.懂得
它是一個粒度很小的設計形式,由於形式的表示只是一個籠統的辦法。工場辦法形式界說了一個用於創立對象的界面,讓子類決議詳細實例化哪個類。也就是在工場和產物之間增長界面,工場不再擔任產物的完成,有托言針對分歧前提前往分歧的類實例,再由詳細類實例去完成。工場辦法時簡略工場的衍生,改良了很多簡略工場的缺陷,遵守了開——閉准繩,完成了可擴大,可以用於更加龐雜的產物成果場所。工場辦法可以懂得為異樣是父親給兒子留了一筆錢,然後直接讓兒子去安排,怎樣花父親一概不論。
2.長處
工場辦法形式客服了簡略工場的許多缺陷,它每一個詳細工場只完成單一義務,並且遵守開——閉准繩,代碼簡練並且具有優越的擴大性。
3.缺陷
假如有產物類須要修正,對應的工場類也須要停止修正。一旦有多個產物類都須要修正的時刻,對號入坐的成績就湧現了,這是對工場類的修正就會變得相當龐雜。是以工場辦法形式固然有益於擴大然則晦氣於保護。
綜上所述,我們便可以曉得針對分歧的情形詳細采取哪一種形式對編程更有益了。當須要創立的對象比擬少,客戶只曉得傳入工場的參數,其實不關懷若何創立對象的時刻便可以采取簡略工場形式;當類將創立對象的職責拜托給多個贊助子類中的某一個時便可以采取工場辦法形式了。