C++單一職責(zé)原則示例代碼淺析
單一職責(zé)原則:
就一個(gè)類(lèi)而言,應(yīng)該只有一個(gè)引起它變化的原因,如果一個(gè)類(lèi)承擔(dān)的職責(zé)過(guò)多就等于把這些職責(zé)耦合在一起,至少會(huì)造成以下兩方面的問(wèn)題:
- 我們要去修改該類(lèi)中的一個(gè)職責(zé)可能會(huì)影響到該類(lèi)的其它職責(zé)。這種耦合會(huì)導(dǎo)致脆弱的設(shè)計(jì),當(dāng)變化發(fā)生時(shí),設(shè)計(jì)會(huì)遭受到意想不到的破壞。
- 當(dāng)客戶(hù)端僅需要該對(duì)象的某一個(gè)職責(zé)時(shí),不得不將其他不需要的職責(zé)全都包含進(jìn)來(lái),從而造成冗余代碼或代碼的浪費(fèi)。
我們?cè)谠O(shè)計(jì)一個(gè)類(lèi)時(shí)要學(xué)會(huì)發(fā)現(xiàn)職責(zé),并把那些職責(zé)相互分離,其實(shí)要去判斷是否應(yīng)該分離出一個(gè)類(lèi)來(lái)并不難,前面說(shuō)過(guò),一個(gè)類(lèi)應(yīng)該只有一個(gè)引起它變化的原因,如果你能想到其它的原因也能去改變這個(gè)類(lèi),那么這個(gè)類(lèi)就具有多于1個(gè)的職責(zé),就應(yīng)該考慮類(lèi)的職責(zé)分離。
在之前的這篇博客中,傳送門(mén),我們實(shí)現(xiàn)的計(jì)算器實(shí)際上也用到了單一職責(zé)原則,這里我們選出其中最經(jīng)典的3.0版本和5.0版本來(lái)學(xué)習(xí)單一職責(zé)原則。
3.0版本計(jì)算器代碼如下:
#include<iostream> using namespace std; #include<string> //業(yè)務(wù)邏輯 //異常類(lèi)用于處理異常情況 class opeException { public: void getMessage() { cout << "您的輸入有誤!" << endl; } }; //運(yùn)算類(lèi)用于處理運(yùn)算 class Operation { public: Operation(string& _num1, string& _num2, string& _ope) :num1(_num1), num2(_num2), ope(_ope){} //獲取運(yùn)算結(jié)果 int getResult() { if (!(isStringNum(num1) && isStringNum(num2) && (ope == "+" || ope == "-" || ope == "*" || ope == "/"))) throw opeException(); if (ope == "+") { re = stoi(num1) + stoi(num2); } else if (ope == "-") { re = stoi(num1) - stoi(num2); } else if (ope == "*") { re = stoi(num1) * stoi(num2); } else if (ope == "/") { if (stoi(num2) != 0) { re = stoi(num1) / stoi(num2); } else throw opeException(); } return re; } private: int re; string num1; string num2; string ope; //判斷一個(gè)字符串是不是數(shù)字 bool isStringNum(string& s) { bool flag = true; for (auto e : s) if (!(isdigit(e))) { flag = false; break; } return flag; } }; //界面邏輯 int main() { try { string _num1 = " "; string _num2 = " "; string _ope = " "; cout << "請(qǐng)輸入左操作數(shù):" << endl; cin >> _num1; cout << "請(qǐng)輸入右操作數(shù):" << endl; cin >> _num2; cout << "請(qǐng)輸入操作符" << endl; cin >> _ope; Operation operation(_num1, _num2, _ope); cout << operation.getResult() << endl; } catch (opeException &ex) { ex.getMessage(); } return 0; }
僅僅一個(gè)運(yùn)算類(lèi)Operation就實(shí)現(xiàn)了加減乘除4種功能,很明顯在這個(gè)類(lèi)中我至少有4個(gè)原因去修改這個(gè)類(lèi),我修改加法算法的時(shí)候可能會(huì)影響到其它的運(yùn)算算法,這個(gè)類(lèi)的耦合太高且嚴(yán)重違反了單一職責(zé)原則。
修改后的5.0版本如下:
#include<iostream> using namespace std; #include<string> //業(yè)務(wù)邏輯 //異常類(lèi)用于處理異常情況 class opeException { public: void getMessage() { cout << "您的輸入有誤!" << endl; } }; //運(yùn)算類(lèi) class Operation { //判斷一個(gè)字符串是不是數(shù)字 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; }; //加法運(yùn)算類(lèi) 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)算類(lèi) 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)算類(lèi) 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)算類(lèi) 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; } }; //運(yùn)算工廠類(lèi) class OpeFactory { public: Operation& choose(string &_strNum1,string &_strNum2,string &_ope) { if (_ope == "+") { operation = new addOperation(_strNum1, _strNum2, _ope); } else if (_ope == "-") operation = new subOperation(_strNum1, _strNum2, _ope); else if (_ope == "*") operation = new mulOperation(_strNum1, _strNum2, _ope); else if (_ope == "/") { operation = new divOperation(_strNum1, _strNum2, _ope); } else operation = nullptr; return *operation; } private: Operation* operation; }; //界面邏輯 int main() { try { string _strNum1 = " "; string _strNum2 = " "; string _ope = " "; cout << "請(qǐng)輸入左操作數(shù):" << endl; cin >> _strNum1; cout << "請(qǐng)輸入右操作數(shù):" << endl; cin >> _strNum2; cout << "請(qǐng)輸入操作符:" << endl; cin >> _ope; OpeFactory factory; Operation* re = &factory.choose(_strNum1, _strNum2, _ope); if (re != nullptr) cout << (*re).getResult() << endl; else cout << "您的輸入有誤!" << endl; } catch (opeException ex) { cout << "您的輸入有誤" << endl; } return 0; }
在5.0版本的計(jì)算器代碼中,我們將運(yùn)算類(lèi)分成了4種類(lèi),分別是加法類(lèi)、減法類(lèi)、乘法類(lèi)、除法類(lèi),還創(chuàng)建了一個(gè)工廠類(lèi)專(zhuān)門(mén)用于根據(jù)不同情況實(shí)例化對(duì)象,每個(gè)類(lèi)只有一個(gè)職責(zé),我們要修改某個(gè)功能只需要去修改對(duì)應(yīng)的類(lèi)即可,極大降低了代碼之間的耦合。
單一職責(zé)原則的核心就是控制類(lèi)的粒度大小、將對(duì)象解耦、提高其內(nèi)聚性。如果遵循單一職責(zé)原則將有以下優(yōu)點(diǎn):
- 降低類(lèi)的復(fù)雜度。一個(gè)類(lèi)只負(fù)責(zé)一項(xiàng)職責(zé),其邏輯肯定要比負(fù)責(zé)多項(xiàng)職責(zé)簡(jiǎn)單得多。
- 提高類(lèi)的可讀性。復(fù)雜性降低,自然其可讀性會(huì)提高。
- 提高系統(tǒng)的可維護(hù)性??勺x性提高,那自然更容易維護(hù)了。
- 變更引起的風(fēng)險(xiǎn)降低。變更是必然的,如果單一職責(zé)原則遵守得好,當(dāng)修改一個(gè)功能時(shí),可以顯著降低對(duì)其他功能的影響。
到此這篇關(guān)于C++單一職責(zé)原則示例代碼淺析的文章就介紹到這了,更多相關(guān)C++單一職責(zé)原則內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
OpenCV圖像特征提取之Shi-Tomasi角點(diǎn)檢測(cè)算法詳解
Harris角點(diǎn)檢測(cè)算法就是對(duì)角點(diǎn)響應(yīng)函數(shù)R進(jìn)行閾值處理,Shi-Tomasi原理幾乎和Harris一樣的,只不過(guò)最后計(jì)算角點(diǎn)響應(yīng)的公式發(fā)生了變化。本文將和大家詳細(xì)說(shuō)說(shuō)Shi-Tomasi角點(diǎn)檢測(cè)算法的原理與實(shí)現(xiàn),需要的可以參考一下2022-09-09QT中QStringListModel類(lèi)的應(yīng)用介紹
QStringListModel是最簡(jiǎn)單的模型類(lèi),具備向視圖提供字符串?dāng)?shù)據(jù)的能力,本文主要介紹了QT中QStringListModel類(lèi)的應(yīng)用介紹,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01C++高級(jí)數(shù)據(jù)結(jié)構(gòu)之優(yōu)先隊(duì)列
這篇文章主要介紹了C++高級(jí)數(shù)據(jù)結(jié)構(gòu)之優(yōu)先隊(duì)列,文章圍繞主題的相關(guān)資料展開(kāi)詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05C++ API功能設(shè)計(jì)的實(shí)現(xiàn)
C++ API中看似很小的修改,都可能會(huì)影響到生成的對(duì)象和庫(kù)文件的二進(jìn)制表示,如果客戶(hù)想替換共享庫(kù)使之工作,就不能簡(jiǎn)單的替換庫(kù)文件了事,而往往需要重新編譯2022-08-08C++實(shí)現(xiàn)正態(tài)隨機(jī)分布的方法
本篇介紹了,使用c++實(shí)現(xiàn)正態(tài)隨機(jī)分布的實(shí)現(xiàn)方法。需要的朋友參考下2013-05-05用C/C++實(shí)現(xiàn)linux下檢測(cè)網(wǎng)絡(luò)接口狀態(tài)
這篇文章主要為大家詳細(xì)介紹了用c/c++實(shí)現(xiàn)linux下檢測(cè)網(wǎng)絡(luò)接口狀態(tài),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06C++基礎(chǔ)入門(mén)教程(七):一些比較特別的基礎(chǔ)語(yǔ)法總結(jié)
這篇文章主要介紹了C++基礎(chǔ)入門(mén)教程(七):一些比較特別的基礎(chǔ)語(yǔ)法總結(jié),本文總結(jié)的都是一些特殊的語(yǔ)法,需要的朋友可以參考下2014-11-11C語(yǔ)言入門(mén)篇--注釋,關(guān)鍵字typedef及轉(zhuǎn)義字符詳解
本篇文章是c語(yǔ)言基礎(chǔ)篇,主要為大家介紹了C語(yǔ)言的關(guān)鍵字typedef,注釋?zhuān)D(zhuǎn)義字符的基本理論知識(shí),希望可以幫助大家快速入門(mén)c語(yǔ)言的世界,更好的理解c語(yǔ)言2021-08-08