意圖
定義一系列的算法,把它們一個個封裝起來, 并且使它們可相互替換。本模式使得算法可獨立于使用它的客戶而變化。
動機
策略模式為了適應不同的需求,只把變化點封裝了,這個變化點就是實現不同需求的算法,但是,用戶需要知道各種算法的具體情況。就像上面的加班工資,不同的加班情況,有不同的算法。我們不能在程序中將計算工資的算法進行硬編碼,而是能自由的變化的。這就是策略模式。
剛剛加班回來;哎,公司規定平時加班只有10塊錢的餐補;星期六和星期天加班,只給串休假;在國家規定的節假日按照3倍工資發放。那么對于這么多的計算加班費的方法,公司的OA系統是如何進行做的呢?各種計算加班費的情況就可以認為是策略模式
適用性
- 許多相關的類僅僅是行為有異。 “策略”提供了一種用多個行為中的一個行為來配置一個類的方法。
- 需要使用一個算法的不同變體。例如,你可能會定義一些反映不同的空間 /時間權衡的算法。當這些變體實現為一個算法的類層次時,可以使用策略模式
- 算法使用客戶不應該知道的數據。可使用策略模式以避免暴露復雜的、與算法相關的數據結構。
- 一個類定義了多種行為 , 并且這些行為在這個類的操作中以多個條件語句的形式出現。將相關的條件分支移入它們各自的Strategy類中以代替這些條件語句。
結構
?
參與者
Strategy:定義所有支持的算法的公共接口。Context使用這個接口來調用某ConcreteStrategy定義的算法;
ConcreteStrategy:實現Strategy接口的具體算法;
Context:使用一個ConcreteStrategy對象來配置;維護一個對Stategy對象的引用,同時,可以定義一個接口來讓Stategy訪問它的數據。
效果
相關算法系列Strategy類層次為Context定義了一系列的可供重用的算法或行為。繼承有助于析取出這些算法中的公共功能。
實現
純策略模式,實際上就是父類為純虛函數,子類根據需求實現各種方法
1 #include <iostream> 2 using namespace std; 3 4 // The abstract strategy 5 class Strategy 6 { 7 public: 8 virtual void AlgorithmInterface() = 0; 9 }; 10 11 class ConcreteStrategyA : public Strategy 12 { 13 public: 14 void AlgorithmInterface() 15 { 16 cout<<"I am from ConcreteStrategyA."<<endl; 17 } 18 }; 19 20 class ConcreteStrategyB : public Strategy 21 { 22 public: 23 void AlgorithmInterface() 24 { 25 cout<<"I am from ConcreteStrategyB."<<endl; 26 } 27 }; 28 29 class ConcreteStrategyC : public Strategy 30 { 31 public: 32 void AlgorithmInterface() 33 { 34 cout<<"I am from ConcreteStrategyC."<<endl; 35 } 36 }; 37 38 class Context 39 { 40 public: 41 Context(Strategy *pStrategyArg) : pStrategy(pStrategyArg) 42 { 43 } 44 void ContextInterface() 45 { 46 pStrategy->AlgorithmInterface(); 47 } 48 private: 49 Strategy *pStrategy; 50 }; 51 52 int main() 53 { 54 // Create the Strategy 55 Strategy *pStrategyA = new ConcreteStrategyA; 56 Strategy *pStrategyB = new ConcreteStrategyB; 57 Strategy *pStrategyC = new ConcreteStrategyC; 58 Context *pContextA = new Context(pStrategyA); 59 Context *pContextB = new Context(pStrategyB); 60 Context *pContextC = new Context(pStrategyC); 61 pContextA->ContextInterface(); 62 pContextB->ContextInterface(); 63 pContextC->ContextInterface(); 64 65 if (pStrategyA) delete pStrategyA; 66 if (pStrategyB) delete pStrategyB; 67 if (pStrategyC) delete pStrategyC; 68 69 if (pContextA) delete pContextA; 70 if (pContextB) delete pContextB; 71 if (pContextC) delete pContextC; 72 }
在實際操作的過程中,我們會發現,在main函數中,也就是在客戶端使用策略模式時,會創建非常多的Strategy,而這樣就莫名的增加了客戶端的壓力,讓客戶端的復雜度陡然增加了。那么,我們就可以借鑒簡單工廠模式,使策略模式和簡單工廠模式相結合,從而減輕客戶端的壓力,代碼實現如下:
1 #include <iostream> 2 using namespace std; 3 4 // Define the strategy type 5 typedef enum StrategyType 6 { 7 StrategyA, 8 StrategyB, 9 StrategyC 10 }STRATEGYTYPE; 11 12 // The abstract strategy 13 class Strategy 14 { 15 public: 16 virtual void AlgorithmInterface() = 0; 17 virtual ~Strategy() = 0; // 謝謝hellowei提出的bug,具體可以參見評論 18 }; 19 20 Strategy::~Strategy() 21 {} 22 23 class ConcreteStrategyA : public Strategy 24 { 25 public: 26 void AlgorithmInterface() 27 { 28 cout << "I am from ConcreteStrategyA." << endl; 29 } 30 31 ~ConcreteStrategyA(){} 32 }; 33 34 class ConcreteStrategyB : public Strategy 35 { 36 public: 37 void AlgorithmInterface() 38 { 39 cout << "I am from ConcreteStrategyB." << endl; 40 } 41 42 ~ConcreteStrategyB(){} 43 }; 44 45 class ConcreteStrategyC : public Strategy 46 { 47 public: 48 void AlgorithmInterface() 49 { 50 cout << "I am from ConcreteStrategyC." << endl; 51 } 52 53 ~ConcreteStrategyC(){} 54 }; 55 56 class Context 57 { 58 public: 59 Context(STRATEGYTYPE strategyType) 60 { 61 switch (strategyType) 62 { 63 case StrategyA: 64 pStrategy = new ConcreteStrategyA; 65 break; 66 67 case StrategyB: 68 pStrategy = new ConcreteStrategyB; 69 break; 70 71 case StrategyC: 72 pStrategy = new ConcreteStrategyC; 73 break; 74 75 default: 76 break; 77 } 78 } 79 80 ~Context() 81 { 82 if (pStrategy) delete pStrategy; 83 } 84 85 void ContextInterface() 86 { 87 if (pStrategy) 88 pStrategy->AlgorithmInterface(); 89 } 90 91 private: 92 Strategy *pStrategy; 93 }; 94 95 int main() 96 { 97 Context *pContext = new Context(StrategyA); 98 pContext->ContextInterface(); 99 100 if (pContext) delete pContext; 101 }
?