Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Strategy Design Pattern in .NET

3.50/5 (2 votes)
4 Oct 2010CPOL2 min read 19.7K  
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)