日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C++單一職責原則示例代碼淺析_C 語言

作者:北冥有魚丶丶 ? 更新時間: 2023-06-19 編程語言

單一職責原則:

就一個類而言,應(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

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新