Please enable Javascript to correctly display the contents on Dot Net Tricks!

Strategy Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 24 Feb 2017
Total Views : 8,028   
Updated On : 24 Feb 2017

Strategy Design Pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE etc. In this article, I would like to share what is strategy pattern and how is it work?

What is Strategy Design pattern?

This pattern allows a client to choose an algorithm from a family of algorithms at run-time and gives it a simple way to access it.

This pattern involves the removal of an algorithm from its host class and putting it in a separate class. As you know, there may be multiple strategies which are applicable for a given problem. So, if the algorithms will exist in the host class, then it will result a messy code with lots of conditional statements.

Strategy Design Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the Strategy Design Pattern is given below:

Strategy Design Pattern C#

The classes, interfaces and objects in the above UML class diagram are as follows:

  1. Context

    This is a class that contains a property to hold the reference of a Strategy object. This property will be set at run-time according to the algorithm that is required.

  2. Strategy

    This is an interface that is used by the Context object to call the algorithm defined by a ConcreteStrategy.

  3. ConcreteStrategyA/B

    These are classes that implements Strategy interface.

C# - Implementation Code

public class Client
 public IStrategy Strategy { get; set; }

 public void CallAlgorithm()

public interface IStrategy
 string Algorithm();

public class ConcreteStrategyA : IStrategy
 public string Algorithm()
 return "Concrete Strategy A";

public class ConcreteStrategyB : IStrategy
 public string Algorithm()
 return "Concrete Strategy B";

Real Life Example:

Real Life Example of Strategy Design Pattern C#

When to use it?

  1. There are multiple strategies for a given problem and the selection criteria of a strategy is defined a run-time.

  2. Many related classes are only differ in their behaviors.

  3. The strategies use the data to which the client has no access.

What do you think?

I hope you will enjoy the Strategy Design Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.


Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
14 OCT
Angular2 and Angular4 (Online)
03:00 PM-05:00 PM IST (+5.30GMT)
12 OCT
ASP.NET Core (Online)
09:00 PM - 11:00 PM IST(+5:30 GMT)
10 OCT
Microsoft Azure Infrastructure Solutions (Online)
08:00 AM-09:30 AM IST / 09:30 PM -11:00 PM CST
30 SEP
Angular2 and Angular4 (Classroom)
08:30 AM-11:30 AM IST
20 SEP
MEAN Stack (Online)
07:00 AM-09:00 AM IST
20 SEP
ASP.NET MVC with Angular4 (Online)
9:00PM- 11:00PM IST(+5:30GMT)
16 SEP
Angular2 and Angular4 (Online)
08:00 AM-10:00 AM IST(+5.30 GMT)
22 AUG
ASP.NET Core with Angular4 (Online)
07:00 AM - 9:00 AM IST(+5:30 GMT)