码迷,mamicode.com
首页 > 其他好文 > 详细

简单工厂模式

时间:2014-05-01 13:17:25      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   java   javascript   color   get   string   int   rgb   

简单工厂模式:

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

 

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

Operation:操作类的基类

mamicode.com,码迷
 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 };
mamicode.com,码迷

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

mamicode.com,码迷
 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 };
mamicode.com,码迷

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

mamicode.com,码迷
 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 };
mamicode.com,码迷

代码调用:

mamicode.com,码迷
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;
}
mamicode.com,码迷

 

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

mamicode.com,码迷
class CashSuper
{
public:
    virtual double acceptCash(double money)=0;
    CashSuper(void);
    ~CashSuper(void);
};
mamicode.com,码迷
mamicode.com,码迷
#pragma once
//不打折扣的情况
#include "cashsuper.h"
class CashNormal :
    public CashSuper
{
public:
    virtual double acceptCash(double money)
    {
        return money;
    }
    CashNormal(void);
    ~CashNormal(void);
};
mamicode.com,码迷
mamicode.com,码迷
#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;
};
mamicode.com,码迷
mamicode.com,码迷
#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;
};
mamicode.com,码迷
mamicode.com,码迷
#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);
};
mamicode.com,码迷

调用:

mamicode.com,码迷
#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;
}
mamicode.com,码迷

输出的结果为:

200

 

400

200

 

400

简单工厂模式,码迷,mamicode.com

简单工厂模式

标签:style   blog   class   code   java   javascript   color   get   string   int   rgb   

原文地址:http://www.cnblogs.com/crazycodehzp/p/3701310.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!