using System; namespaceStrategyPatternDemo { // The Context defines the interface of interest to clients. classContext { // The Context maintains a reference to one of the Strategy objects. The // Context does not know the concrete class of a strategy. It should // work with all strategies via the Strategy interface. private IStrategy _strategy;
publicContext() { }
// Usually, the Context accepts a strategy through the constructor, but // also provides a setter to change it at runtime. publicContext(IStrategy strategy) { this._strategy = strategy; }
// Usually, the Context allows replacing a Strategy object at runtime. publicvoidSetStrategy(IStrategy strategy) { this._strategy = strategy; }
// The Context delegates some work to the Strategy object instead of // implementing multiple versions of the algorithm on its own. publicvoidDoSomeBusinessLogic() { Console.WriteLine("Context: Sorting data using the strategy (not sure how it'll do it)"); var result = this._strategy.DoAlgorithm(new List<string> { "a", "b", "c", "d", "e" });
string resultStr = string.Empty; foreach (var element in result as List<string>) { resultStr += element + ","; }
Console.WriteLine(resultStr); } }
// The Strategy interface declares operations common to all supported // versions of some algorithm. // // The Context uses this interface to call the algorithm defined by Concrete // Strategies. publicinterfaceIStrategy { objectDoAlgorithm(object data); }
// Concrete Strategies implement the algorithm while following the base // Strategy interface. The interface makes them interchangeable in the // Context. classConcreteStrategyA : IStrategy { publicobjectDoAlgorithm(object data) { var list = data as List<string>; list.Sort();
return list; } }
classConcreteStrategyB : IStrategy { publicobjectDoAlgorithm(object data) { var list = data as List<string>; list.Sort(); list.Reverse();
return list; } }
classProgram { staticvoidMain(string[] args) { // The client code picks a concrete strategy and passes it to the // context. The client should be aware of the differences between // strategies in order to make the right choice. var context = new Context();
Console.WriteLine("Client: Strategy is set to normal sorting."); context.SetStrategy(new ConcreteStrategyA()); context.DoSomeBusinessLogic();
Console.WriteLine();
Console.WriteLine("Client: Strategy is set to reverse sorting."); context.SetStrategy(new ConcreteStrategyB()); context.DoSomeBusinessLogic(); // output
// Client: Strategy is set to normal sorting. // Context: Sorting data using the strategy (not sure how it'll do it) // a,b,c,d,e
// Client: Strategy is set to reverse sorting. // Context: Sorting data using the strategy (not sure how it'll do it) // e,d,c,b,a } } }