程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 設計模式之---簡單工廠模式

設計模式之---簡單工廠模式

編輯:C++入門知識

簡單工廠模式:到底要實例化哪個對象,將來會不會增加實例化對象,這時可以考慮用一個單獨的類來創造實例的過程,這就是工廠。
以一個2元操作運算來說明:
operator類:
[cpp] 
#pragma once 
 
template <class T> 
class COperator 

public: 
    COperator(void){}; 
    virtual ~COperator(void){}; 
    virtual T GetResult(T NumberA,T NumberB); 
}; 
 
 
 
#include "Operator.h" 
 
template <class T> 
 T COperator<T>::GetResult(T NumberA,T NumberB) 

  T result=0; 
 
  return result; 

加法類:
[cpp] 
#pragma once 
#include "Operator.h" 
 
template <class T> 
class CAddOperator:public COperator<T> 

public: 
    CAddOperator(void){}; 
    ~CAddOperator(void){}; 
    virtual T GetResult(T NumberA,T NumberB); 
}; 
 
#include "AddOperator.h" 
 
 
template <class T> 
 T CAddOperator<T>::GetResult(T NumberA,T NumberB) 

    T result; 
 
    result = NumberA + NumberB; 
 
    return result; 

減法類:
[cpp] 
#pragma once 
#include "operator.h" 
 
template <class T> 
class CSubOperator :public COperator<T> 

public: 
    CSubOperator(void){}; 
    ~CSubOperator(void){}; 
virtual T GetResult(T NumberA,T NumberB); 
}; 
 
 
#include "SubOperator.h" 
 
template <class T> 
T CSubOperator<T>::GetResult(T NumberA,T NumberB) 

    T result= NumberA -NumberB ; 
 
    return result; 

乘法類:
[cpp] 
#pragma once 
#include "operator.h" 
 
template <class T> 
class CMulOperator :public COperator<T> 

public: 
    CMulOperator(void){}; 
    ~CMulOperator(void){}; 
virtual T GetResult(T NumberA,T NumberB); 
}; 
 
 
#include "Mul.h" 
 
template <class T> 
T CMulOperator<T>::GetResult(T NumberA,T NumberB) 

    T result = NumberA * NumberB; 
 
    return result; 

除法類:
[cpp] 
#pragma once 
#include "operator.h" 
 
template <class T> 
class CDivOperator :public COperator<T> 

public: 
    CDivOperator(void){}; 
    ~CDivOperator(void){}; 
 
    virtual T GetResult(T NumberA,T NumberB); 
}; 
 
#include "Div.h" 
#include <assert.h> 
 
template <class T> 
T CDivOperator<T>::GetResult(T NumberA,T NumberB) 

    assert( NumberB != 0); 
 
    T result =  NumberA/NumberB; 
 
    return result; 

接下來是簡單工廠類,創建具體類的實例
[cpp]
#pragma once 
#include "Operator.h" 
#include "Operator.cpp" 
 
#include "AddOperator.h" 
#include "AddOperator.cpp" 
 
#include "Div.h" 
#include "Div.cpp" 
 
#include "SubOperator.h" 
#include "SubOperator.cpp" 
 
#include "Mul.h" 
#include "Mul.cpp" 
 
template <class T> 
class CFactory 

public: 
    CFactory(void){}; 
    ~CFactory(void){}; 
 
     
    COperator<T>* CreateOperator(const char strOpe); 
}; 
 
#include "Factory.h" 
 
template <class T> 
COperator<T>* CFactory<T>::CreateOperator(const char strOpe) 

  COperator<T> *pOpe = NULL ; 
 
  switch(strOpe) 
  { 
    case '+': 
            pOpe = new CAddOperator<T>; 
            break; 
    case '-': 
            pOpe = new CSubOperator<T>; 
            break; 
    case '*': 
            pOpe=new CMulOperator<T>; 
            break; 
    case '/': 
            pOpe=new CDivOperator<T>; 
            break; 
    default: 
        break; 
  } 
 
  return pOpe; 

在實際應用時可以簡單的調用:
[cpp] 
// SimpleFactory.cpp : Defines the entry point for the console application. 
// 
 
#include "stdafx.h" 
#include "Factory.h" 
#include "Factory.cpp" 
#include <iostream> 
using namespace std; 
 
int _tmain(int argc, _TCHAR* argv[]) 

    CFactory<double> *pFac=new CFactory<double>; 
    COperator<double>* pOpe=NULL; 
    double result; 
 
    pOpe=pFac->CreateOperator('+'); 
    if(pOpe!=NULL) 
    { 
    result = pOpe->GetResult(2,3); 
    cout<<"2 + 3 = "<<result<<endl; 
    delete pOpe; 
    } 
     
    pOpe=pFac->CreateOperator('-'); 
    if(pOpe!=NULL) 
    { 
        result = pOpe->GetResult(2,3); 
        cout<<"2 - 3 = "<<result<<endl; 
        delete pOpe; 
    } 
 
    pOpe=pFac->CreateOperator('*'); 
    if(pOpe!=NULL) 
    { 
        result = pOpe->GetResult(2,3); 
        cout<<"2 * 3 = "<<result<<endl; 
        delete pOpe; 
    } 
 
    pOpe=pFac->CreateOperator('/'); 
    if(pOpe!=NULL) 
    { 
        result = pOpe->GetResult(2,3); 
        cout<<"2 / 3 = "<<result<<endl; 
        delete pOpe; 
    } 
     
    return 0; 

它不需要管具體實例化哪個對象,這個過程都由工廠類實現了,以後如果要增加其他的二元運算,只須讓其繼承至COperator類,再修改工廠類的switch分支即可。


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved