简单工厂模式的计算器(C++)

最近学习一本叫做《大话设计模式》的书,书上的代码貌似是JAVA的,本人使用C++来实现了一下,实现方式上面有稍许不同,但是基本原理差不多,用的是简单工厂模式,经测试可以运行,下面贴上代码:(比较简单就不贴上注释了)


io.h文件: //#include "string" #include "logic.h" using namespace std; class CalIO { public: double getNum1(); double getNum2(); void printResult(); char getOperator(); void setNum1(); void setNum2(); void setResult(double result); void setOperator(); CalIO() { _num1 = 0; _num2 = 0; _result = 0; } private: double _num1; double _num2; double _result; char _operator; }; io.cpp文件: #include "io.h" #include "iostream" using namespace std; double CalIO::getNum1() { return _num1; } double CalIO::getNum2() { return _num2; } void CalIO::printResult() { cout << "运算结果为: " << _result << endl; } char CalIO::getOperator() { return _operator; } void CalIO::setNum1() { cout << "请输入第一个数" << endl; cin >> _num1; } void CalIO::setNum2() { cout << "请输入第二个数" << endl; cin >> _num2; } void CalIO::setResult(double result) { _result = result; } void CalIO::setOperator() { cout << "请输入运算符" << endl; cin >> _operator; } logic.h文件: #pragma once #include "string" #include "iostream" using namespace std; class CalLogic { public: CalLogic (double num1, double num2) { _num1 = num1; _num2 = num2; _result = 0; } virtual void calResult() = 0; double getResult(); double getNum1(); double getNum2(); void setResult(double result); protected: private: double _num1; double _num2; double _result; }; class AddCal:public CalLogic { public: void calResult(); AddCal(double num1, double num2):CalLogic(num1, num2){} }; class SubCal:public CalLogic { public: void calResult(); SubCal(double num1, double num2):CalLogic(num1, num2){} }; class MulCal:public CalLogic { public: void calResult(); MulCal(double num1, double num2):CalLogic(num1, num2){} }; class DivCal:public CalLogic { public: void calResult(); DivCal(double num1, double num2):CalLogic(num1, num2){} }; logic.cpp文件: #include "logic.h" double CalLogic::getResult() { return _result; } double CalLogic::getNum1() { return _num1; } double CalLogic::getNum2() { return _num2; } void CalLogic::setResult(double result) { _result = result; } void AddCal::calResult() { setResult(getNum1() + getNum2()); } void SubCal::calResult() { setResult(getNum1() - getNum2()); }; void MulCal::calResult() { setResult(getNum1() * getNum2()); }; void DivCal::calResult() { if (getNum2() != 0) { setResult(getNum1() / getNum2()); }else { cout << "被除数为0,错误" << endl; } }; factory.h文件: #include "logic.h" class OperatorFactory { public: static CalLogic* createCalLogic(char calOperator, double num1, double num2); }; factory.cpp文件: #include "factory.h" CalLogic* OperatorFactory::createCalLogic(char calOperator, double num1, double num2) { CalLogic* calLogic; switch (calOperator) { case '+': calLogic = new AddCal(num1, num2); break; case '-': calLogic = new SubCal(num1, num2); break; case '*': calLogic = new MulCal(num1, num2); break; case '/': calLogic = new DivCal(num1, num2); break; default: cout << "出现未知运算符号" << endl; } return calLogic; } main.cpp文件: #include "io.h" #include "logic.h" #include "factory.h" using namespace std; int main() { CalIO* calIO = new CalIO(); calIO->setNum1(); calIO->setNum2(); calIO->setOperator(); CalLogic* calLogic = OperatorFactory::createCalLogic(calIO->getOperator(), calIO->getNum1(), calIO->getNum2());//使用工厂实例化对应的对象 calLogic->calResult(); calIO->setResult(calLogic->getResult()); calIO->printResult(); system("pause");//输出结果后界面停留 }

你可能感兴趣的:(设计模式(C++),C++,简单工厂模式,设计模式,计算器)