Design Patterns(C#): Basic example Strategy pattern

by Pieter Brinkman 24. November 2010 07:21

In this example I´m going to explane the strategy pattern. With this example you can do a calculation with two numbers (-/+) and expand the number of operators (/, *, etc.).

First create a interface which defines the interface of the operator classes. For this example an operator can only calculate two values.

[code:c#]
//The interface for the strategies
 public interface ICalculateInterface
 {
        //define method
        int Calculate(int value1, int value2);
  }
[/code]


Next create the operators (strategies) Minus (which subtract value 2 from value 1) and Plussus (which addition value 1 with value 2). The classes need to inherit from the interface ICalculateInterface.
[code:c#]
//Strategy 1: Minus
class Minus : ICalculateInterface
{
        public int Calculate(int value1, int value2)
        {
            //define logic
            return value1 - value2;
        }
}

//Strategy 2: Plussus
class Plussus : ICalculateInterface
{
        public int Calculate(int value1, int value2)
        {
            //define logic
            return value1 + value2;
        }
}
[/code]


At last we need to create a Client that will execute the strategy.
[code:c#]
//The client
class CalculateClient
{
        private ICalculateInterface calculateInterface;

        //Constructor: assigns strategy to interface
        public CalculateClient(ICalculateInterface strategy)
        {
            calculateInterface = strategy;
        }

        //Executes the strategy
        public int Calculate(int value1, int value2)
        {
            return calculateInterface.Calculate(value1, value2);
        }
}    
[/code]


Now we have two operators (minus & plussus) and a client (CalculateClient) that can execute the operators. Let’s test the code. Create a new webapplication, console app or something else that can write output. For this example I will use a webpage.

Initialize a new CalculateClient with argument operator Minus of Plussus and Calculate two values.
[code:c#]
protected void Page_Load(object sender, EventArgs e)
{
            CalculateClient minusClient = new CalculateClient(new Minus());
            Response.Write("<br />Minus: " + minusClient.Calculate(7, 1).ToString());

            CalculateClient plusClient = new CalculateClient(new Plussus());
            Response.Write("<br />Plussus: " + plusClient.Calculate(7, 1).ToString());
}
[/code]


This code will give the following output.
[code:html]<br />Minus: 6<br />Plussus: 8[/code]


The great thing about this pattern is that you can easily add new opertators (strategies) to your code.

Cheers,
Pieter

Tags: , , ,

ASP.Net | Microsoft | Visual Studio | Design Patterns

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen

About Me

My name is Pieter Brinkman I am Solution Architect for Sitecore in The Netherlands. My interests are mainly ASP.NET, MSSQL and Content Management Systems.

Calendar

<<  February 2012  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
2728291234
567891011

View posts in large calendar

RecentComments

Comment RSS

Most comments