您好,登錄后才能下訂單哦!
這篇文章主要介紹“C++開放封閉原則實(shí)例代碼分析”,在日常操作中,相信很多人在C++開放封閉原則實(shí)例代碼分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C++開放封閉原則實(shí)例代碼分析”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
開放-封閉原則可以做到這樣,所謂開放-封閉原則就是指軟件實(shí)體(類、函數(shù)、模塊等)應(yīng)該可以擴(kuò)展,但是不可以修改,即我們設(shè)計(jì)這個(gè)類的時(shí)候就盡量讓這個(gè)類足夠好,寫好了就不要去修改了,原來的代碼能不動則不動,如果新需求來,我們增加一些類就完事了。面對需求的改變,對程序的改動是通過增加新代碼進(jìn)行的,而不是更改現(xiàn)有的代碼,這就是開放-封閉原則的精神所在。
在我們最初編寫代碼時(shí),假設(shè)變化不會發(fā)生。當(dāng)變化發(fā)生時(shí),我們將創(chuàng)建抽象來隔離以后發(fā)生的同類變化。
2.0版本:
#include<iostream> using namespace std; #include<string> class opeException { public: void getMessage() { cout << "您的輸入有誤!" << endl; } }; //判斷一個(gè)字符串是不是數(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; }
在計(jì)算器2.0版本中,如果我們要增加開平方、平方、立方等運(yùn)算,需要對代碼進(jìn)行大量修改,這顯然不滿足開放-封閉原則,可維護(hù)性很差,面對這些可能的變化,在4.0版本的代碼中將各種具體運(yùn)算,比如加減乘除分別抽象成為加法類、減法類、乘法類、除法類,這樣如果我們需要增加一些運(yùn)算,面對這些變化,我們只需要再創(chuàng)建相應(yīng)的運(yùn)算類即可。
4.0版本:
#include<iostream> using namespace std; #include<string> //業(yè)務(wù)邏輯 //異常類用于處理異常情況 class opeException { public: void getMessage() { cout << "您的輸入有誤!" << endl; } }; //運(yùn)算類 class Operation { //判斷一個(gè)字符串是不是數(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; }; //加法運(yùn)算類 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; } }; //減法運(yùn)算類 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; } }; //乘法運(yùn)算類 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; } }; //除法運(yùn)算類 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; }
當(dāng)然,并不是什么時(shí)候應(yīng)對變化都是容易的。我們希望的是在開發(fā)工作展開不久就知道可能發(fā)生的變化。查明可能發(fā)生的變化所等待的時(shí)間越長,要創(chuàng)建正確的抽象就越困難。比如,如果加減運(yùn)算都在很多地方應(yīng)用了,再考慮抽象、考慮分離,就很困難。
開放-封閉原則是面向?qū)ο笤O(shè)計(jì)的核心所在。遵循這個(gè)原則可以帶來面向?qū)ο蠹夹g(shù)所聲稱的巨大好處,也就是可維護(hù)、可擴(kuò)展、可復(fù)用、靈活性好。開發(fā)人員應(yīng)該僅對程序中呈現(xiàn)出頻繁變化的那些部分做出抽象,然而,對于應(yīng)用程序中的每個(gè)部分都刻意地進(jìn)行抽象同樣不是一個(gè)好主意。拒絕不成熟的抽象和抽象本身一樣重要。
到此,關(guān)于“C++開放封閉原則實(shí)例代碼分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。