C++開放封閉原則示例解析
我們在做任何系統(tǒng)的時候,都不要指望系統(tǒng)一開始就完全確定需求,然后再也不發(fā)生變化,這是不現(xiàn)實,也是不科學的想法,既然需求是一定會發(fā)生變化的,那么如何在面對需求的變化時,設計的軟件可以相對容易修改,不至于說,新需求一來就要把整個程序都推倒重來呢?
開放-封閉原則可以做到這樣,所謂開放-封閉原則就是指軟件實體(類、函數(shù)、模塊等)應該可以擴展,但是不可以修改,即我們設計這個類的時候就盡量讓這個類足夠好,寫好了就不要去修改了,原來的代碼能不動則不動,如果新需求來,我們增加一些類就完事了。面對需求的改變,對程序的改動是通過增加新代碼進行的,而不是更改現(xiàn)有的代碼,這就是開放-封閉原則的精神所在。
在我們最初編寫代碼時,假設變化不會發(fā)生。當變化發(fā)生時,我們將創(chuàng)建抽象來隔離以后發(fā)生的同類變化。
在之前的這篇博客中,https://blog.csdn.net/weixin_44049823/article/details/128907849,我們實現(xiàn)了計算器的5個版本,這其中就運用了開放-封閉原則,這里,我們通過該篇博客實現(xiàn)的2.0版本和4.0版本來學習開放-封閉原則。
2.0版本:
#include<iostream> using namespace std; #include<string> class opeException { public: void getMessage() { cout << "您的輸入有誤!" << endl; } }; //判斷一個字符串是不是數(shù)字 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 << "請輸入左操作數(shù):" << endl; cin >> num1; if (!isStringNum(num1)) throw opeException(); cout << "請輸入右操作數(shù):" << 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)<<endl; } else if (ope == "-") { cout << stoi(num1) - stoi(num2) << endl; } else if (ope == "*") { cout << stoi(num1) * stoi(num2) << endl; } else if (ope == "/") { if (stoi(num2) != 0) { cout << stoi(num1) / stoi(num2) << endl; } else cout << "除數(shù)不能為0" << endl; } } catch (opeException ex) { ex.getMessage(); } return 0; }
在計算器2.0版本中,如果我們要增加開平方、平方、立方等運算,需要對代碼進行大量修改,這顯然不滿足開放-封閉原則,可維護性很差,面對這些可能的變化,在4.0版本的代碼中將各種具體運算,比如加減乘除分別抽象成為加法類、減法類、乘法類、除法類,這樣如果我們需要增加一些運算,面對這些變化,我們只需要再創(chuàng)建相應的運算類即可。
4.0版本:
#include<iostream> using namespace std; #include<string> //業(yè)務邏輯 //異常類用于處理異常情況 class opeException { public: void getMessage() { cout << "您的輸入有誤!" << endl; } }; //運算類 class Operation { //判斷一個字符串是不是數(shù)字 bool isStringNum(string& s) { bool flag = true; for (auto e : s) if (!(isdigit(e))) { flag = false; break; } return flag; } protected: //判斷輸入的操作數(shù)和操作符是否有誤 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 << "請輸入左操作數(shù):" << endl; cin >> _strNum1; cout << "請輸入右操作數(shù):" << 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; }
當然,并不是什么時候應對變化都是容易的。我們希望的是在開發(fā)工作展開不久就知道可能發(fā)生的變化。查明可能發(fā)生的變化所等待的時間越長,要創(chuàng)建正確的抽象就越困難。比如,如果加減運算都在很多地方應用了,再考慮抽象、考慮分離,就很困難。
開放-封閉原則是面向對象設計的核心所在。遵循這個原則可以帶來面向對象技術所聲稱的巨大好處,也就是可維護、可擴展、可復用、靈活性好。開發(fā)人員應該僅對程序中呈現(xiàn)出頻繁變化的那些部分做出抽象,然而,對于應用程序中的每個部分都刻意地進行抽象同樣不是一個好主意。拒絕不成熟的抽象和抽象本身一樣重要。
到此這篇關于C++開放封閉原則示例解析的文章就介紹到這了,更多相關C++開放封閉原則內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++map,set,multiset,multimap詳細解析
在C++標準模板庫(STL)中,容器分為關聯(lián)式容器和序列式容器兩大類,關聯(lián)式容器主要包括set、map、multiset和multimap,通過索引來訪問元素,本文給大家介紹C++?map,set,multiset,multimap的相關知識,感興趣的朋友跟隨小編一起看看吧2024-09-09