您好,登錄后才能下訂單哦!
本文小編為大家詳細(xì)介紹“C++中的策略模式怎么實(shí)現(xiàn)”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“C++中的策略模式怎么實(shí)現(xiàn)”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來(lái)學(xué)習(xí)新知識(shí)吧。
策略模式主要解決在有多種算法相似的情況下,使用 if…else 所帶來(lái)的復(fù)雜和難以維護(hù),其實(shí)際就是用來(lái)抽象變化的(和開(kāi)放-封閉原則是一個(gè)原理),只要在分析過(guò)程中我們發(fā)現(xiàn)需要在不同的時(shí)間運(yùn)用不同類(lèi)型的業(yè)務(wù)規(guī)則或者代碼中可能會(huì)出現(xiàn)很多變化,就可以考慮使用策略模式來(lái)處理這種變化。
策略模式通常的使用方法就是一個(gè)抽象策略類(lèi),若干具體策略類(lèi)和一個(gè)Context類(lèi),同時(shí)Conetext類(lèi)可以結(jié)合簡(jiǎn)單工廠模式讓用戶(hù)與策略類(lèi)完全解耦,比如可以向Context類(lèi)的構(gòu)造函數(shù)中傳入?yún)?shù)而不是策略類(lèi),然后在Conext的構(gòu)造函數(shù)里用簡(jiǎn)單工廠模式根據(jù)傳遞的參數(shù)初始化策略類(lèi),甚至還可以什么都不傳,定義一個(gè)默認(rèn)策略供用戶(hù)使用(簡(jiǎn)單工廠不一定是要一個(gè)單獨(dú)的類(lèi))。Conetext類(lèi)中包含一個(gè)策略類(lèi)的指針指向簡(jiǎn)單工廠實(shí)例化出的具體策略類(lèi)對(duì)象,還包含一個(gè)contextDeloy接口用于通過(guò)策略類(lèi)指針去調(diào)用實(shí)例化出的具體策略類(lèi)對(duì)象的接口,可以讓用戶(hù)面對(duì)Context的接口編程,而不與策略類(lèi)接口直接耦合 ,方便策略類(lèi)日后更改接口,同時(shí)還需要一個(gè)get接口,用于獲取簡(jiǎn)單工廠中實(shí)例化出的對(duì)象。在業(yè)務(wù)邏輯層,我們先判斷簡(jiǎn)單工廠模式實(shí)例化的具體對(duì)象是否為空,如果不為空,我們就可以通過(guò)contextDeloy接口去訪問(wèn)實(shí)例化的具體策略類(lèi)對(duì)象的接口。
接下來(lái)我將用策略模式改寫(xiě)之前的計(jì)算器5.0版本。
#include<iostream> using namespace std; #include<string> //業(yè)務(wù)邏輯 //異常類(lèi)用于處理異常情況 class opeException { public: void getMessage() { cout << "您的輸入有誤!" << endl; } }; //運(yùn)算類(lèi) 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() { return 0; } }; //加法運(yùn)算類(lèi) 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)算類(lèi) 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)算類(lèi) 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)算類(lèi) 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; } }; //Conetext結(jié)合簡(jiǎn)單工廠模式 class Context { Operation *operation; public: Context(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; } Operation* get() { return operation; } int contextResult() { return operation->getResult(); } }; //界面邏輯 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; Context context(_strNum1, _strNum2, _ope); if (context.get() != nullptr) cout << context.contextResult() << endl; else cout << "您的輸入有誤!" << endl; } catch (opeException ex) { cout << "您的輸入有誤" << endl; } return 0; }
總結(jié)策略模式的優(yōu)缺點(diǎn):
優(yōu)點(diǎn):
1、算法可以自由切換。
2、避免使用多重條件判斷。
3、擴(kuò)展性良好。
缺點(diǎn):
1、策略類(lèi)會(huì)增多。
2、所有策略類(lèi)都需要對(duì)外暴露。
注意事項(xiàng):如果一個(gè)系統(tǒng)的策略多于四個(gè),就需要考慮使用混合模式,解決策略類(lèi)膨脹的問(wèn)題。
讀到這里,這篇“C++中的策略模式怎么實(shí)現(xiàn)”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過(guò)才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。