C++單一職責(zé)原則示例代碼淺析
單一職責(zé)原則:
就一個類而言,應(yīng)該只有一個引起它變化的原因,如果一個類承擔(dān)的職責(zé)過多就等于把這些職責(zé)耦合在一起,至少會造成以下兩方面的問題:
- 我們要去修改該類中的一個職責(zé)可能會影響到該類的其它職責(zé)。這種耦合會導(dǎo)致脆弱的設(shè)計,當(dāng)變化發(fā)生時,設(shè)計會遭受到意想不到的破壞。
- 當(dāng)客戶端僅需要該對象的某一個職責(zé)時,不得不將其他不需要的職責(zé)全都包含進來,從而造成冗余代碼或代碼的浪費。
我們在設(shè)計一個類時要學(xué)會發(fā)現(xiàn)職責(zé),并把那些職責(zé)相互分離,其實要去判斷是否應(yīng)該分離出一個類來并不難,前面說過,一個類應(yīng)該只有一個引起它變化的原因,如果你能想到其它的原因也能去改變這個類,那么這個類就具有多于1個的職責(zé),就應(yīng)該考慮類的職責(zé)分離。
在之前的這篇博客中,傳送門,我們實現(xiàn)的計算器實際上也用到了單一職責(zé)原則,這里我們選出其中最經(jīng)典的3.0版本和5.0版本來學(xué)習(xí)單一職責(zé)原則。
3.0版本計算器代碼如下:
#include<iostream>
using namespace std;
#include<string>
//業(yè)務(wù)邏輯
//異常類用于處理異常情況
class opeException
{
public:
void getMessage()
{
cout << "您的輸入有誤!" << endl;
}
};
//運算類用于處理運算
class Operation
{
public:
Operation(string& _num1, string& _num2, string& _ope) :num1(_num1), num2(_num2), ope(_ope){}
//獲取運算結(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;
//判斷一個字符串是不是數(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 << "請輸入左操作數(shù):" << endl;
cin >> _num1;
cout << "請輸入右操作數(shù):" << endl;
cin >> _num2;
cout << "請輸入操作符" << endl;
cin >> _ope;
Operation operation(_num1, _num2, _ope);
cout << operation.getResult() << endl;
}
catch (opeException &ex)
{
ex.getMessage();
}
return 0;
}
僅僅一個運算類Operation就實現(xiàn)了加減乘除4種功能,很明顯在這個類中我至少有4個原因去修改這個類,我修改加法算法的時候可能會影響到其它的運算算法,這個類的耦合太高且嚴(yán)重違反了單一職責(zé)原則。
修改后的5.0版本如下:
#include<iostream>
using namespace std;
#include<string>
//業(yè)務(wù)邏輯
//異常類用于處理異常情況
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:
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;
}
};
//運算工廠類
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 << "請輸入左操作數(shù):" << endl;
cin >> _strNum1;
cout << "請輸入右操作數(shù):" << endl;
cin >> _strNum2;
cout << "請輸入操作符:" << 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版本的計算器代碼中,我們將運算類分成了4種類,分別是加法類、減法類、乘法類、除法類,還創(chuàng)建了一個工廠類專門用于根據(jù)不同情況實例化對象,每個類只有一個職責(zé),我們要修改某個功能只需要去修改對應(yīng)的類即可,極大降低了代碼之間的耦合。
單一職責(zé)原則的核心就是控制類的粒度大小、將對象解耦、提高其內(nèi)聚性。如果遵循單一職責(zé)原則將有以下優(yōu)點:
- 降低類的復(fù)雜度。一個類只負(fù)責(zé)一項職責(zé),其邏輯肯定要比負(fù)責(zé)多項職責(zé)簡單得多。
- 提高類的可讀性。復(fù)雜性降低,自然其可讀性會提高。
- 提高系統(tǒng)的可維護性。可讀性提高,那自然更容易維護了。
- 變更引起的風(fēng)險降低。變更是必然的,如果單一職責(zé)原則遵守得好,當(dāng)修改一個功能時,可以顯著降低對其他功能的影響。
到此這篇關(guān)于C++單一職責(zé)原則示例代碼淺析的文章就介紹到這了,更多相關(guān)C++單一職責(zé)原則內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Linux/C++多線程實例學(xué)習(xí)十字路口車輛調(diào)度
這篇文章主要為大家介紹了Linux/C++多線程實例學(xué)習(xí)十字路口車輛調(diào)度示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-05-05
C語言實現(xiàn)學(xué)生管理系統(tǒng)總結(jié)
這篇文章主要為大家詳細(xì)介紹了C語言實現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-07-07
深入HRESULT與Windows Error Codes的區(qū)別詳解
本篇文章是對HRESULT與Windows Error Codes的區(qū)別進行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05

