美文网首页
简单工厂模式

简单工厂模式

作者: 小蜗牛Snail丶 | 来源:发表于2018-04-08 12:13 被阅读0次

    设计模式的基本组成和基本目的:

    1.命名规范

    2.减少不必要的判断 ,尽量让计算机少做无用功

    3.注意异常case

    封装 继承 多态

    紧耦合 和 松耦合

    第一个设计模式;  简单工厂模式

    类图:


    简单工厂类:

    .h文件

    /* * OperationFactory.h * * Created on: 2018年4月2日 * Author: jiangshang */

    #include#include#include#include "simpleFactroy.h"

    using namespace std;

    enum OPERATE{

    };

    #ifndef OPERATIONFACTORY_H_

    #define OPERATIONFACTORY_H_

    class OperationFactory {

    public:

    OperationFactory();

    ~OperationFactory();

    static Operation* createOperate(string operate);

    };

    #endif /* OPERATIONFACTORY_H_ */

    .cpp文件以及main函数入口

    /*

    * OperationFactory.cpp

    *

    *  Created on: 2018年4月2日

    *      Author: jiangshang

    */

    #include "OperationFactory.h"

    #include "SimpleFactoryAdd.h"

    #include "SimpleFactoryDev.h"

    #include "SimpleFactorySub.h"

    #include "SimpleFactoryMul.h"

    OperationFactory::OperationFactory() {

    }

    OperationFactory::~OperationFactory() {

    }

    Operation*  OperationFactory::createOperate(string operate) {

    Operation *ope = NULL;

    int operateNum = 0;

    if (operate == "+")

    {

    operateNum = 1;

    }

    else if (operate == "-"){

    operateNum = 2;

    }

    else if (operate == "*"){

    operateNum = 3;

    }

    else if (operate == "/"){

    operateNum = 4;

    }

    switch (operateNum) {

    case 1:

    ope = new SimpleFactoryAdd();

    break;

    case 2:

    ope = new SimpleFactorySub();

    break;

    case 3:

    ope = new SimpleFactoryMul();

    break;

    case 4:

    ope = new SimpleFactoryDev();

    break;

    default:

    break;

    }

    return ope;

    }

    int main()

    {

    Operation* oper = NULL;

    oper = OperationFactory::createOperate("+");

    oper->setA(3);

    oper->setB(4);

    double result = oper->getResult();

    cout<<result<<endl;

    return 0

    运算类:

    .h文件

    #ifndef SIMPLEFACTROY_H_

    #define SIMPLEFACTROY_H_

    class Operation {

    private:

    double numberA;

    double numberB;

    public:

    Operation() ;

    ~Operation();

      double getA();

      void setA(double value);

      double getB();

      void setB(double value);

      virtual double getResult(){};

    };

    .cpp文件

    #include "simpleFactroy.h"

    Operation::Operation():numberA(0),numberB(0)

    {

    }

    Operation::~Operation(){}

    double Operation::getA()

    {

    return numberA;

    }

    void Operation::setA(double value)

    {

    this->numberA = value;

    }

    double Operation::getB()

    {

    return numberB;

    }

    void Operation::setB(double value)

    {

    this->numberB = value;

    }

    加法类:

    .h

    #ifndef SIMPLEFACTORYADD_H_

    #define SIMPLEFACTORYADD_H_

    #include "simpleFactroy.h"

    class SimpleFactoryAdd: public Operation {

    public:

    SimpleFactoryAdd();

    ~SimpleFactoryAdd();

    double getResult();

    };

    #endif /* SIMPLEFACTORYADD_H_ */

    .cpp

    #include "SimpleFactoryAdd.h"

    SimpleFactoryAdd::SimpleFactoryAdd()

    {

    }

    SimpleFactoryAdd::~SimpleFactoryAdd()

    {

    }

    double SimpleFactoryAdd::getResult()

    {

    double result = 0;

    result = getA() + getB();

    return result;

    }

    除法类:

    .h

    #ifndef SIMPLEFACTORYDEV_H_

    #define SIMPLEFACTORYDEV_H_

    #include "simpleFactroy.h"

    class SimpleFactoryDev: public Operation {

    public:

    SimpleFactoryDev();

    ~SimpleFactoryDev();

    double getResult();

    };

    #endif /* SIMPLEFACTORYDEV_H_ */

    .cpp

    #include "SimpleFactoryDev.h"

    SimpleFactoryDev::SimpleFactoryDev()

    {

    }

    SimpleFactoryDev::~SimpleFactoryDev()

    {

    }

    double SimpleFactoryDev::getResult()

    {

    double result = 0;

    result = getA() + getB();

    return result;

    }

    减法类:

    .h

    #ifndef SIMPLEFACTORYSUB_H_

    #define SIMPLEFACTORYSUB_H_

    #include "simpleFactroy.h"

    class SimpleFactorySub: public Operation {

    public:

    SimpleFactorySub();

    ~SimpleFactorySub();

    double getResult();

    };

    #endif /* SIMPLEFACTORYSUB_H_ */

    .cpp

    #include "SimpleFactorySub.h"

    SimpleFactorySub::SimpleFactorySub()

    {

    }

    SimpleFactorySub::~SimpleFactorySub()

    {

    }

    double SimpleFactorySub::getResult()

    {

    double result = 0;

    result = getA() + getB();

    return result;

    }

    乘法类:

    .h

    #ifndef SIMPLEFACTORYMul_H_

    #define SIMPLEFACTORYMul_H_

    #include "simpleFactroy.h"

    class SimpleFactoryMul: public Operation {

    public:

    SimpleFactoryMul();

    ~SimpleFactoryMul();

    double getResult();

    };

    #endif /* SIMPLEFACTORYMul_H_ */

    .cpp

    #include "SimpleFactoryMul.h"

    SimpleFactoryMul::SimpleFactoryMul()

    {

    }

    SimpleFactoryMul::~SimpleFactoryMul()

    {

    }

    double SimpleFactoryMul::getResult()

    {

    double result = 0;

    result = getA() + getB();

    return result;

    }

    相关文章

      网友评论

          本文标题:简单工厂模式

          本文链接:https://www.haomeiwen.com/subject/jnwucftx.html