当前位置:编程学习 > 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;&n

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,