Click here to Skip to main content
13,591,461 members
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 2 Oct 2010
Licenced CPOL

Strategy Design Pattern in .NET

, 4 Oct 2010
Rate this:
Please Sign up or sign in to vote.
Strategy Design Pattern in .NET

Strategy design pattern falls under the category of Behavioral Design Pattern. In this pattern, we capture abstraction in an Interface or Abstract class called Strategy Base, and we bury implementation details of algorithms in concrete classes called Concrete Strategy. Client code can then call such different implementation methods based upon some strategy or condition during run time. Client is not tied statically or bound to call fixed methods, rather it can change its strategy dynamically. This is because client never calls any methods directly by instantiating concrete classes. Client sets its strategy via some other class called Context.

Let’s see one such example of this pattern.


Figure: Strategy Design Pattern showing all three main components- Strategy Base, Concrete Strategy and Context Classes. 

Coming to the code, we have: 

namespace BehavioralDesignPattern.StrategyDesignPattern
public abstract class StrategyBase
public abstract long Calculate(int x,int y);

public class ConcreteAddStrategy : StrategyBase
public override long Calculate(int x, int y)
return x + y;

public class ConcreteSubtractStrategy : StrategyBase
public override long Calculate(int x, int y)
return x – y;

public class Context
public StrategyBase Strategy { get; set; }
public long CallCalculateMethod(int x, int y)
return (Strategy.Calculate(x, y));

We see each of the concrete strategy class implementing algorithm to calculate upon numbers in its own way- one doing addition, while another doing subtraction. But their overall capability to do arithmetic operations upon numbers is abstracted inside Calculate(int, int) method in StrategyBase class.

See the Context class above. It has a property Strategy to get-set of type StrategyBase type. Alternatively, Context class can get-set instance of StrategyBase by a constructor or some method as well like SetStrategy(StrategyBase objSB).

But why do we require this Context class? Because clients agree to call any ConcreteStrategy method not directly. Clients will only hint out for such concrete strategy. What does this mean? This means a lot- Strategy pattern lets you change the guts of an object.

See the client code below:

private void CallStrategyAddMethod()
Context objCtxt = new Context();
objCtxt.Strategy = new ConcreteAddStrategy();
// Now the object’s strategy is to call Add method.
objCtxt.CallCalculateMethod(10, 15);

As seen from the above code, object “objCtxt” is able to call method in a concrete strategy class.

Whenever modeling a system after Strategy Design Pattern, one has to carefully think of a way to allow client to convey its strategy to context class.

That’s it.

Filed under: .Net Technologies, Architecture, C#/VB.Net, CodeProject, Dot Net Tips Tagged: .Net 4.0, .Net3.5, Behavioral Design Pattern, Design Patterns, Strategy Design Pattern


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Dinesh K Mandal
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India

You may also be interested in...

Comments and Discussions

Question.Net Stratergy Pin
Anshu Singh Surya7-Jan-13 19:00
memberAnshu Singh Surya7-Jan-13 19:00 
GeneralNice Pin
Khaniya3-Oct-10 23:35
memberKhaniya3-Oct-10 23:35 
GeneralRe: Nice Pin
Dinesh K Mandal9-Oct-10 4:14
memberDinesh K Mandal9-Oct-10 4:14 
GeneralMy vote of 3 Pin
maq_rohit3-Oct-10 9:40
membermaq_rohit3-Oct-10 9:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03 | 2.8.180618.1 | Last Updated 4 Oct 2010
Article Copyright 2010 by Dinesh K Mandal
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid