首页 > 代码库 > 简单工厂模式

简单工厂模式

简单工厂模式:

    自己的理解:一个类有多种子类,牵涉到要实例化某种子类,用工厂模式来生成对应的实例对象.

 

比如如下的计算器的设计:

Operation:操作类的基类

 1 class Operation
 2 {
 3 public:
 4     Operation(double lhs,double rhs):_numberA(lhs),_numberB(rhs){}
 5     ~Operation(void);
 6     virtual double GetResult()
 7     {
 8         double result = 0;
 9         return 0;
10     }
11     double GetA()
12     {
13         return _numberA;
14     }
15     double GetB()
16     {
17         return _numberB;
18     }
19 private:
20     double _numberA;
21     double _numberB;
22 };

接下来对操作类有两个子类,分别实现加法和减法

 1 class OperationAdd :
 2     public Operation
 3 {
 4 public:
 5     OperationAdd(double lhs,double rhs):Operation(lhs,rhs){}
 6     double GetResult()
 7     {
 8         double result = 0;
 9         result = GetA() +GetB();
10         return result;
11     }
12     ~OperationAdd(void);
13 };
14 
15 class OperationSub:public Operation
16 {
17 public:
18     OperationSub(double lhs,double rhs):Operation(lhs,rhs){}
19     double GetResult()
20     {
21         double result=0;
22         result = GetA()-GetB();
23         return result;
24     }
25     ~OperationSub(void);
26 };

然后实现工厂类,针对不同的输入产生不同的实例对象:

 1 class OperationFactory
 2 {
 3 public:
 4     static Operation* createOperate(double lhs,char operate,double rhs)
 5     {
 6         Operation *oper = NULL;
 7         switch (operate)
 8         {
 9         case +:
10             oper = new OperationAdd(lhs,rhs);
11             break;
12         case -:
13             oper = new OperationSub(lhs,rhs);
14             break;
15 
16         }
17         return oper;
18     }
19     OperationFactory(void);
20     ~OperationFactory(void);
21 };

代码调用:

int _tmain(int argc, _TCHAR* argv[])
{
    Operation *ptr;
    ptr = OperationFactory::createOperate(1.23,+,2.34);
    cout<<ptr->GetResult()<<endl;

    Operation *ptr2;
    ptr2 = OperationFactory::createOperate(1.23,-,2.34);
    cout<<ptr2->GetResult()<<endl;
    return 0;
}

 

银行收银系统的工厂模式设计方法:(之后改成策略模式)

class CashSuper
{
public:
    virtual double acceptCash(double money)=0;
    CashSuper(void);
    ~CashSuper(void);
};
#pragma once
//不打折扣的情况
#include "cashsuper.h"
class CashNormal :
    public CashSuper
{
public:
    virtual double acceptCash(double money)
    {
        return money;
    }
    CashNormal(void);
    ~CashNormal(void);
};
#pragma once
//打折扣的情况
#include "cashsuper.h"
class CashRebate :
    public CashSuper
{
public:
    CashRebate(double rebate=1.0):moneyRebate(rebate){}
    virtual double acceptCash(double money)
    {
        return money*moneyRebate;
    }
    ~CashRebate(void);
private:
    double moneyRebate;
};
#pragma once
//满多少减多少的情况
#include "cashsuper.h"
class CashReturn :
    public CashSuper
{
public:
    CashReturn(double condition=0.0,double returns =0.0):moneyCondition(condition),moneyReturn(returns){}
    virtual double acceptCash(double money)
    {
        double result = money;
        if(money>=moneyCondition)
            result = money-(money/moneyCondition)*moneyReturn;
        return result;
    }
    ~CashReturn(void);
private:
    double moneyCondition;
    double moneyReturn;
};
#pragma once
#include"CashSuper.h"
#include"CashNormal.h"
#include"CashRebate.h"
#include"CashReturn.h"
#include<string>
class CashFactory
{
public:
    static CashSuper* createCashAccept(int type)
    {
        CashSuper *cs=NULL;
        switch(type)
        {
        case 1:
            cs = new CashNormal();
            break;
        case 2:
            cs = new CashReturn(300,100);
            break;
        case 3:
            cs = new CashRebate(0.8);
            break;
        }
        return cs;
    }
    CashFactory(void);
    ~CashFactory(void);
};

调用:

#include "stdafx.h"
#include <iostream>
#include "cashFactory.h"
#include "CashSuper.h"
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    CashSuper *ptr;
    ptr=CashFactory::createCashAccept(1);
    cout<<ptr->acceptCash(200)<<endl<<endl;

    CashSuper *ptr2;
    ptr2=CashFactory::createCashAccept(2);
    cout<<ptr2->acceptCash(600)<<endl;
    cout<<ptr2->acceptCash(200)<<endl<<endl;

    CashSuper *ptr3;
    ptr3=CashFactory::createCashAccept(3);
    cout<<ptr3->acceptCash(600)<<endl<<endl;
    return 0;
}

输出的结果为:

200

 

400

200

 

400