網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
單一職責(zé)原則:
就一個(gè)類而言,應(yīng)該只有一個(gè)引起它變化的原因,如果一個(gè)類承擔(dān)的職責(zé)過(guò)多就等于把這些職責(zé)耦合在一起,至少會(huì)造成以下兩方面的問(wèn)題:
- 我們要去修改該類中的一個(gè)職責(zé)可能會(huì)影響到該類的其它職責(zé)。這種耦合會(huì)導(dǎo)致脆弱的設(shè)計(jì),當(dāng)變化發(fā)生時(shí),設(shè)計(jì)會(huì)遭受到意想不到的破壞。
- 當(dāng)客戶端僅需要該對(duì)象的某一個(gè)職責(zé)時(shí),不得不將其他不需要的職責(zé)全都包含進(jìn)來(lái),從而造成冗余代碼或代碼的浪費(fèi)。
我們?cè)谠O(shè)計(jì)一個(gè)類時(shí)要學(xué)會(huì)發(fā)現(xiàn)職責(zé),并把那些職責(zé)相互分離,其實(shí)要去判斷是否應(yīng)該分離出一個(gè)類來(lái)并不難,前面說(shuō)過(guò),一個(gè)類應(yīng)該只有一個(gè)引起它變化的原因,如果你能想到其它的原因也能去改變這個(gè)類,那么這個(gè)類就具有多于1個(gè)的職責(zé),就應(yīng)該考慮類的職責(zé)分離。
在之前的這篇博客中,傳送門,我們實(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ù)邏輯
//異常類用于處理異常情況
class opeException
{
public:
void getMessage()
{
cout << "您的輸入有誤!" << endl;
}
};
//運(yùn)算類用于處理運(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)算類Operation就實(shí)現(xiàn)了加減乘除4種功能,很明顯在這個(gè)類中我至少有4個(gè)原因去修改這個(gè)類,我修改加法算法的時(shí)候可能會(huì)影響到其它的運(yùn)算算法,這個(gè)類的耦合太高且嚴(yán)重違反了單一職責(zé)原則。
修改后的5.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:
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;
}
};
//運(yùn)算工廠類
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)算類分成了4種類,分別是加法類、減法類、乘法類、除法類,還創(chuàng)建了一個(gè)工廠類專門用于根據(jù)不同情況實(shí)例化對(duì)象,每個(gè)類只有一個(gè)職責(zé),我們要修改某個(gè)功能只需要去修改對(duì)應(yīng)的類即可,極大降低了代碼之間的耦合。
單一職責(zé)原則的核心就是控制類的粒度大小、將對(duì)象解耦、提高其內(nèi)聚性。如果遵循單一職責(zé)原則將有以下優(yōu)點(diǎn):
- 降低類的復(fù)雜度。一個(gè)類只負(fù)責(zé)一項(xiàng)職責(zé),其邏輯肯定要比負(fù)責(zé)多項(xiàng)職責(zé)簡(jiǎn)單得多。
- 提高類的可讀性。復(fù)雜性降低,自然其可讀性會(huì)提高。
- 提高系統(tǒng)的可維護(hù)性。可讀性提高,那自然更容易維護(hù)了。
- 變更引起的風(fēng)險(xiǎn)降低。變更是必然的,如果單一職責(zé)原則遵守得好,當(dāng)修改一個(gè)功能時(shí),可以顯著降低對(duì)其他功能的影響。
原文鏈接:https://blog.csdn.net/weixin_44049823/article/details/129062231
- 上一篇:沒(méi)有了
- 下一篇:沒(méi)有了
相關(guān)推薦
- 2023-03-29 SVM算法的理解及其Python實(shí)現(xiàn)多分類和二分類問(wèn)題_python
- 2022-10-31 sql查詢給表起別名要點(diǎn)小結(jié)(涉及嵌套查詢)_oracle
- 2023-02-06 python常見(jiàn)讀取語(yǔ)音的3種方法速度對(duì)比_python
- 2022-04-14 Python?eval()函數(shù)和ast.literal_eval()的區(qū)別你知道嗎_python
- 2024-03-24 SpringBoot工具庫(kù):解決SpringBoot2.*版本跨域問(wèn)題
- 2022-11-26 Mongodb?如何將時(shí)間戳轉(zhuǎn)換為年月日日期_MongoDB
- 2023-07-27 express 請(qǐng)求方式(常用) / 不完整請(qǐng)求路徑
- 2022-05-12 Android 記錄build打包的時(shí)間并顯示到手機(jī)上面
- 欄目分類
-
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支