<tbody id="86a2i"></tbody>


<dd id="86a2i"></dd>
<progress id="86a2i"><track id="86a2i"></track></progress>

<dd id="86a2i"></dd>
<em id="86a2i"><ruby id="86a2i"><u id="86a2i"></u></ruby></em>

    <dd id="86a2i"></dd>

    單一職責原則:

    就一個類而言,應該只有一個引起它變化的原因,如果一個類承擔的職責過多就等于把這些職責耦合在一起,至少會造成以下兩方面的問題:

    • 我們要去修改該類中的一個職責可能會影響到該類的其它職責。這種耦合會導致脆弱的設計,當變化發生時,設計會遭受到意想不到的破壞。
    • 當客戶端僅需要該對象的某一個職責時,不得不將其他不需要的職責全都包含進來,從而造成冗余代碼或代碼的浪費。

    我們在設計一個類時要學會發現職責,并把那些職責相互分離,其實要去判斷是否應該分離出一個類來并不難,前面說過,一個類應該只有一個引起它變化的原因,如果你能想到其它的原因也能去改變這個類,那么這個類就具有多于1個的職責,就應該考慮類的職責分離。

    在之前的這篇博客中,傳送門,我們實現的計算器實際上也用到了單一職責原則,這里我們選出其中最經典的3.0版本和5.0版本來學習單一職責原則。

    3.0版本計算器代碼如下:

    #include<iostream>
    using namespace std;
    #include<string>
    //業務邏輯
    //異常類用于處理異常情況
    class opeException
    {
    public:
    	void getMessage()
    	{
    		cout << "您的輸入有誤!" << endl;
    	}
    };
    //運算類用于處理運算
    class Operation
    {
    public:
    	Operation(string& _num1, string& _num2, string& _ope) :num1(_num1), num2(_num2), ope(_ope){}
    		//獲取運算結果
    	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;
    	//判斷一個字符串是不是數字
    	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 << "請輸入左操作數:" << endl;
    		cin >> _num1;
    		cout << "請輸入右操作數:" << endl;
    		cin >> _num2;
    		cout << "請輸入操作符" << endl;
    		cin >> _ope;
    		Operation operation(_num1, _num2, _ope);
    		cout << operation.getResult() << endl;
    	}
    	catch (opeException &ex)
    	{
    		ex.getMessage();
    	}
    	return 0;
    }
    

    僅僅一個運算類Operation就實現了加減乘除4種功能,很明顯在這個類中我至少有4個原因去修改這個類,我修改加法算法的時候可能會影響到其它的運算算法,這個類的耦合太高且嚴重違反了單一職責原則。

    修改后的5.0版本如下:

    #include<iostream>
    using namespace std;
    #include<string>
    //業務邏輯
    //異常類用于處理異常情況
    class opeException
    {
    public:
    	void getMessage()
    	{
    		cout << "您的輸入有誤!" << endl;
    	}
    };
    //運算類
    class Operation
    {
    	//判斷一個字符串是不是數字
    	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 << "請輸入左操作數:" << endl;
    		cin >> _strNum1;
    		cout << "請輸入右操作數:" << 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種類,分別是加法類、減法類、乘法類、除法類,還創建了一個工廠類專門用于根據不同情況實例化對象,每個類只有一個職責,我們要修改某個功能只需要去修改對應的類即可,極大降低了代碼之間的耦合。

    單一職責原則的核心就是控制類的粒度大小、將對象解耦、提高其內聚性。如果遵循單一職責原則將有以下優點:

    • 降低類的復雜度。一個類只負責一項職責,其邏輯肯定要比負責多項職責簡單得多。
    • 提高類的可讀性。復雜性降低,自然其可讀性會提高。
    • 提高系統的可維護性??勺x性提高,那自然更容易維護了。
    • 變更引起的風險降低。變更是必然的,如果單一職責原則遵守得好,當修改一個功能時,可以顯著降低對其他功能的影響。
    原文地址:https://blog.csdn.net/weixin_44049823/article/details/129062231

    相關文章:

    免费一级a片在线播放视频|亚洲娇小性XXXX色|曰本无码毛片道毛片视频清|亚洲一级a片视频免费观看
    <tbody id="86a2i"></tbody>

    
    
    <dd id="86a2i"></dd>
    <progress id="86a2i"><track id="86a2i"></track></progress>

    <dd id="86a2i"></dd>
    <em id="86a2i"><ruby id="86a2i"><u id="86a2i"></u></ruby></em>

      <dd id="86a2i"></dd>