Click here to Skip to main content
15,891,529 members
Articles / Programming Languages / C#

Strategy Design Pattern in .NET

Rate me:
Please Sign up or sign in to vote.
3.50/5 (2 votes)
4 Oct 2010CPOL2 min read 19.3K   4   4
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.

StrategyDesignPattern

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

Coming to the code, we have: 

C#
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:

C#
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

License

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


Written By
Technical Lead Imfinity India Pte Ltd, Noida (Excelsoft Company)
India India
http://www.imfinity.com/

Comments and Discussions

 
Question.Net Stratergy Pin
Anshu Singh Surya7-Jan-13 19:00
Anshu Singh Surya7-Jan-13 19:00 
GeneralNice Pin
Khaniya3-Oct-10 23:35
professionalKhaniya3-Oct-10 23:35 
GeneralRe: Nice Pin
Dinesh K Mandal9-Oct-10 4:14
professionalDinesh K Mandal9-Oct-10 4:14 
GeneralMy vote of 3 Pin
maq_rohit3-Oct-10 9:40
professionalmaq_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.