08 大话设计模式C++实现之工厂方法模式

简单工厂UML

工厂方法模式UML(多了一个工厂基类)

----------→ 依赖关系

实现如下:

#include<iostream>
#include<string>
#include<stdexcept>

using namespace std;

/*
书中的例子: 雷锋生病了,由雷锋的同学们去照顾老人
*/

//首先来实现工厂方法模式

//定义抽象运算类
class Operation
{
private:
    double A;
    double B;
public:
    virtual double getResult() = 0;
    double getA()
    {
        return this->A;
    }
    double getB()
    {
        return this->B;
    }
    void setA(double a)
    {
        this->A = a;
    }
    void setB(double b)
    {
        this->B = b;
    }
};

//定义具体的运算类
class Add : public Operation
{
public:
    virtual double getResult()
    {
        return getA() + getB();
    }
};
class Mul : public Operation
{
public:
    virtual double getResult()
    {
        return getA() * getB();
    }
};
class Sub : public Operation
{
public:
    virtual double getResult()
    {
        return getA() - getB();
    }
};
class Div : public Operation
{
public:
    virtual double getResult()
    {
        if (getB() == 0) throw overflow_error("除数不能为0");
        return getA() / getB();
    }
};

//创建工厂类接口
class IFactory
{
public:
    virtual Operation& CreateOperation() = 0;
};
//由具体的工厂子类接管具体的运算类对象创建工作
class AddFactory : public IFactory
{
public:
    virtual Operation& CreateOperation()
    {
        Operation *a = new Add;
        return *a;
    }
};

class SubFactory : public IFactory
{
public:
    virtual Operation& CreateOperation()
    {
        Operation *a = new Sub;
        return *a;
    }
};

class MulFactory : public IFactory
{
public:
    virtual Operation& CreateOperation()
    {
        Operation *a = new Mul;
        return *a;
    }
};

class DivFactory : public IFactory
{
public:
    virtual Operation& CreateOperation()
    {
        Operation *a = new Div;
        return *a;
    }
};

//客户端
void Clinet()
{
    IFactory *operFactory = new AddFactory;
    Operation &oper = operFactory->CreateOperation();
    oper.setA(10.0);
    oper.setB(1.0);
    cout << oper.getResult() << endl;
    if (operFactory != NULL)
    {
        delete operFactory;
        operFactory = NULL;
    }
    try
    {
        IFactory *operFactory = new DivFactory;
        Operation &oper = operFactory->CreateOperation();
        oper.setA(10.0);
        oper.setB(0);
        cout << oper.getResult() << endl;
        if (operFactory != NULL)
        {
            delete operFactory;
            operFactory = NULL;
        }
    }
    catch(overflow_error& e)
    {
        cout << e.what() << endl;
    }
}

int main()
{
    Clinet();
    system("pause");
    return EXIT_SUCCESS;
}

/*
工厂方法模式总结:
简单工厂模式的最大的优点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态的实例化相关类
对于客户端来说,去除了与具体产品的依赖
工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是
说,工厂方法把简单的内部逻辑判断移到了客户端代码来进行。你想要加功能,本来是改工厂类的,现在是修
改客户端。克服了简单工厂模式违背开放-封闭原则的缺点,又保持了封装对象创建的过程。
*/

//简单工厂模式的做法
/*
在运算工厂中完成判断逻辑
class OperationFactory
{
public:
    static Operation* createOperatecreate(char operate)
    {
        //c++不支持对字符串进行switch  case操作
        Operation *oper = NULL;
        switch (operate)
        {
        case '+':
            oper = new Add;
            break;
        case '-':
            oper = new Sub;
            break;
        case '*':
            oper = new Mul;
            break;
        case '/':
            oper = new Dev;
        default:
            break;
        }
        return oper;
    }
};
//工厂方法模式将判断逻辑交给客户端去实现,简单工厂模式如果需要增加其他的运算操作,如log, e等运算就必须要修改运算工厂类,而工厂方法模式不需要修改原有的类,仅仅需要添加新的操作类去继承工厂基类即可
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值