The next design pattern in the Behavioral Category is the Strategy Design Pattern. Let's take a look at the definition of this design pattern and how to implement it in C# and .Net.
Note: Code can be downloaded in my Github.
1. What is the Strategy Design Pattern?
The Strategy is a behavioral design pattern that lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.
The strategy pattern is a behavioral software design pattern that enables selecting an algorithm at runtime. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of algorithms to use.
2. When to implement the Strategy Design Pattern?
The Strategy Design Pattern should be implemented when:
- you want to use different variants of an algorithm within an object and be able to switch from one algorithm to another during runtime.
- you have a lot of similar classes that only differ in the way they execute some behavior.
- you want to isolate the business logic of a class from the implementation details of algorithms that may not be as important in the context of that logic.
- your class has a massive conditional operator that switches between different variants of the same algorithm.
3. How to implement the Strategy Design Pattern?
The Strategy Design Pattern is implemented by steps below:
Step 1: Create Strategy Interface and Classes
The Strategy interface declares operations common to all supported versions of some algorithm.
Concrete Strategies implement the algorithm while following the base Strategy interface. The interface makes them interchangeable in the Context.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | public interface IStrategy { object DoAlgorithm( object data); } class ConcreteStrategyA : IStrategy { public object DoAlgorithm( object data) { var list = data as List< string >; list.Sort(); return list; } } class ConcreteStrategyB : IStrategy { public object DoAlgorithm( object data) { var list = data as List< string >; list.Sort(); list.Reverse(); return list; } } |
Step 2: Create Context Class
The Context defines the interface of interest to clients.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | class Context { // 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; public Context() { } // Usually, the Context accepts a strategy through the constructor, but // also provides a setter to change it at runtime. public Context(IStrategy strategy) { this ._strategy = strategy; } // Usually, the Context allows replacing a Strategy object at runtime. public void SetStrategy(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. public void DoSomeBusinessLogic() { 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); } } |
Implement the code on client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | class Program { static void Main( 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:
4. Conclusion
The Strategy Design Pattern is simple but it's popular in software programming. I hope this article is helpful for your applications design. Please let me know your thoughts by commenting in the section below.