C++开放封闭原则示例解析

在如那就的设计模式中,不能修改,但可以扩展的实现是一条十分重要的原则,它是开放-封闭原则(The Open-Clossed Principle,简称OCP)或开-关闭原则

我们在做任何系统的时候,都不要指望系统一开始就完全确定需求,然后再也不发生变化,这是不现实,也是不科学的想法,既然需求是一定会发生变化的,那么如何在面对需求的变化时,设计的软件可以相对容易修改,不至于说,新需求一来就要把整个程序都推倒重来呢?

开放-封闭原则可以做到这样,所谓开放-封闭原则就是指软件实体(类、函数、模块等)应该可以扩展,但是不可以修改,即我们设计这个类的时候就尽量让这个类足够好,写好了就不要去修改了,原来的代码能不动则不动,如果新需求来,我们增加一些类就完事了。面对需求的改变,对程序的改动是通过增加新代码进行的,而不是更改现有的代码,这就是开放-封闭原则的精神所在。

在我们最初编写代码时,假设变化不会发生。当变化发生时,我们将创建抽象来隔离以后发生的同类变化。

在之前的这篇博客中,https://blog.csdn.net/weixin_44049823/article/details/128907849,我们实现了计算器的5个版本,这其中就运用了开放-封闭原则,这里,我们通过该篇博客实现的2.0版本和4.0版本来学习开放-封闭原则。

2.0版本:

#include using namespace std; #include class opeException { public: void getMessage() { cout << "您的输入有误!" << endl; } }; //判断一个字符串是不是数字 bool isStringNum(string& s) { bool flag = true; for (auto e : s) if (!isdigit(e)) { flag = false; break; } return flag; } int main() { string num1 = "0"; string num2 = "0"; string ope = " "; try { cout << "请输入左操作数:" << endl; cin >> num1; if (!isStringNum(num1)) throw opeException(); cout << "请输入右操作数:" << endl; cin >> num2; if (!isStringNum(num2)) throw opeException(); cout << "请输入操作符" << endl; cin >> ope; if (ope != "+" && ope != "-" && ope != "*" && ope != "/") throw opeException(); if (ope == "+") { cout<< stoi(num1) + stoi(num2)<

在计算器2.0版本中,如果我们要增加开平方、平方、立方等运算,需要对代码进行大量修改,这显然不满足开放-封闭原则,可维护性很差,面对这些可能的变化,在4.0版本的代码中将各种具体运算,比如加减乘除分别抽象成为加法类、减法类、乘法类、除法类,这样如果我们需要增加一些运算,面对这些变化,我们只需要再创建相应的运算类即可。

4.0版本:

#include using namespace std; #include //业务逻辑 //异常类用于处理异常情况 class opeException { public: void getMessage() { cout << "您的输入有误!" << endl; } }; //运算类 class Operation { //判断一个字符串是不是数字 bool isStringNum(string& s) { bool flag = true; for (auto e : s) if (!(isdigit(e))) { flag = false; break; } return flag; } protected: //判断输入的操作数和操作符是否有误 bool isError(string& _strNum1, string& _strNum2, string& _ope) { if (!(Operation::isStringNum(_strNum1) && Operation::isStringNum(_strNum2) && (_ope == "+" || _ope == "-" || _ope == "*" || _ope == "/"))) { return false; } } public: virtual int getResult() = 0; }; //加法运算类 class addOperation:public Operation { private: string strNum1; string strNum2; string ope; int re; public: addOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1),strNum2(_strNum2),ope(_ope),re(0) {} virtual int getResult() override { if (!isError(strNum1, strNum2, ope)) throw opeException(); else re = stoi(strNum1) + stoi(strNum2); return re; } }; //减法运算类 class subOperation :public Operation { private: string strNum1; string strNum2; string ope; int re; public: subOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {} virtual int getResult() override { if (!isError(strNum1, strNum2, ope)) throw opeException(); else re = stoi(strNum1) - stoi(strNum2); return re; } }; //乘法运算类 class mulOperation :public Operation { private: string strNum1; string strNum2; string ope; int re; public: mulOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {} virtual int getResult() override { if (!isError(strNum1, strNum2, ope)) throw opeException(); else re = stoi(strNum1) * stoi(strNum2); return re; } }; //除法运算类 class divOperation :public Operation { private: string strNum1; string strNum2; string ope; int re; public: divOperation(string& _strNum1, string& _strNum2, string& _ope) :strNum1(_strNum1), strNum2(_strNum2), ope(_ope), re(0) {} virtual int getResult() override { if (!isError(strNum1, strNum2, ope)) throw opeException(); else if (stoi(strNum2) != 0) re = stoi(strNum1) / stoi(strNum2); else throw opeException(); return re; } }; //界面逻辑 int main() { try { string _strNum1 = " "; string _strNum2 = " "; string _ope = " "; cout << "请输入左操作数:" << endl; cin >> _strNum1; cout << "请输入右操作数:" << endl; cin >> _strNum2; cout << "请输入操作符:" << endl; cin >> _ope; if (_ope == "+") { addOperation addoperation(_strNum1, _strNum2, _ope); cout << addoperation.getResult() << endl; } else if (_ope == "-") { subOperation suboperation(_strNum1, _strNum2, _ope); cout << suboperation.getResult() << endl; } else if (_ope == "*") { mulOperation muloperation(_strNum1, _strNum2, _ope); cout << muloperation.getResult() << endl; } else if (_ope == "/") { divOperation muloperation(_strNum1, _strNum2, _ope); cout << muloperation.getResult() << endl; } else cout << "您的输入有误!" << endl; } catch (opeException ex) { cout << "您的输入有误" << endl; } return 0; }

当然,并不是什么时候应对变化都是容易的。我们希望的是在开发工作展开不久就知道可能发生的变化。查明可能发生的变化所等待的时间越长,要创建正确的抽象就越困难。比如,如果加减运算都在很多地方应用了,再考虑抽象、考虑分离,就很困难。

开放-封闭原则是面向对象设计的核心所在。遵循这个原则可以带来面向对象技术所声称的巨大好处,也就是可维护、可扩展、可复用、灵活性好。开发人员应该仅对程序中呈现出频繁变化的那些部分做出抽象,然而,对于应用程序中的每个部分都刻意地进行抽象同样不是一个好主意。拒绝不成熟的抽象和抽象本身一样重要。

到此这篇关于C++开放封闭原则示例解析的文章就介绍到这了,更多相关C++开放封闭原则内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是C++开放封闭原则示例解析的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » C语言