網(wǎng)站首頁 編程語言 正文
單一職責原則:
就一個類而言,應(yīng)該只有一個引起它變化的原因,如果一個類承擔的職責過多就等于把這些職責耦合在一起,至少會造成以下兩方面的問題:
- 我們要去修改該類中的一個職責可能會影響到該類的其它職責。這種耦合會導(dǎo)致脆弱的設(shè)計,當變化發(fā)生時,設(shè)計會遭受到意想不到的破壞。
- 當客戶端僅需要該對象的某一個職責時,不得不將其他不需要的職責全都包含進來,從而造成冗余代碼或代碼的浪費。
我們在設(shè)計一個類時要學會發(fā)現(xiàn)職責,并把那些職責相互分離,其實要去判斷是否應(yīng)該分離出一個類來并不難,前面說過,一個類應(yīng)該只有一個引起它變化的原因,如果你能想到其它的原因也能去改變這個類,那么這個類就具有多于1個的職責,就應(yīng)該考慮類的職責分離。
在之前的這篇博客中,傳送門,我們實現(xiàn)的計算器實際上也用到了單一職責原則,這里我們選出其中最經(jīng)典的3.0版本和5.0版本來學習單一職責原則。
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個原因去修改這個類,我修改加法算法的時候可能會影響到其它的運算算法,這個類的耦合太高且嚴重違反了單一職責原則。
修改后的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ù)不同情況實例化對象,每個類只有一個職責,我們要修改某個功能只需要去修改對應(yīng)的類即可,極大降低了代碼之間的耦合。
單一職責原則的核心就是控制類的粒度大小、將對象解耦、提高其內(nèi)聚性。如果遵循單一職責原則將有以下優(yōu)點:
- 降低類的復(fù)雜度。一個類只負責一項職責,其邏輯肯定要比負責多項職責簡單得多。
- 提高類的可讀性。復(fù)雜性降低,自然其可讀性會提高。
- 提高系統(tǒng)的可維護性。可讀性提高,那自然更容易維護了。
- 變更引起的風險降低。變更是必然的,如果單一職責原則遵守得好,當修改一個功能時,可以顯著降低對其他功能的影響。
原文鏈接:https://blog.csdn.net/weixin_44049823/article/details/129062231
- 上一篇:沒有了
- 下一篇:沒有了
相關(guān)推薦
- 2022-07-17 一起詳細聊聊C#中的Visitor模式_C#教程
- 2023-02-14 Python實現(xiàn)完全數(shù)的示例詳解_python
- 2022-07-06 pandas實現(xiàn)一行拆分成多行_python
- 2022-07-12 ui追加動態(tài)li
- 2022-07-23 asp.net6?blazor?文件上傳功能_實用技巧
- 2022-09-22 為什么float4個字節(jié)比long8個字節(jié)所表示的數(shù)值范圍廣
- 2022-01-26 使用Guzzle拓展包請求接口失敗重試
- 2022-04-15 python中對正則表達式re包的簡單引用方式_python
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支