Home
Gallery
GuestBook
SourceCode
Contact

Design Patterns for Dummies. The Strategy Pattern

Posted in Design Patterns
This post has been read 3552 times

Continuing with the series on Design patterns, I will take up Behavioral Patterns from today. I have written about Structural Patterns and Creational Patterns in my previous posts. You can get all the links for them here and here.

Behavioral Patterns

Behavioral patterns are concerned with algorithms and communication between them. An algorithm that may have been spilt into complex classes could be hard to maintain. Behavioral patterns helps in identifying the basis for this split and deals with communication between these classes.

Strategy Pattern

The strategy pattern deals with removing algorithms from its host and placing them separately. The client program is given a context to select the desired algorithm in a simple way.

    1 namespace PatternsConsole

    2 {

    3     class StrategyPattern

    4     {

    5         // Strategy Interface

    6         interface ISortStrategy

    7         {

    8             void Sort( List<Person> list );

    9         }

   10 

   11         class Person

   12         {

   13             public string Name { get; set; }

   14             public int Age { get; set; }

   15         }

   16 

   17         // Concrete Strategies

   18         class SelectionSort : ISortStrategy

   19         {

   20             public void Sort( List<Person> list )

   21             {

   22                 for (var i = 0; i < list.Count - 1; i++)

   23                 {

   24                     for ( var j = 1 + 1 ; j < list.Count; j++ )

   25                     {

   26                         if(list[i].Age > list[j].Age)

   27                         {

   28                             Swap(i, list, j);

   29                         }

   30                     }

   31                 }

   32             }

   33 

   34             private static void Swap(int i, IList<Person> list, int j)

   35             {

   36                 var temp = list[i];

   37                 list[i] = list[j];

   38                 list[j] = temp;

   39             }

   40         }

   41 

   42         class LINQSort : ISortStrategy

   43         {

   44             public void Sort( List<Person> list )

   45             {

   46                 list.Sort( ( p1, p2 ) => p1.Age.CompareTo( p2.Age ) );

   47             }

   48         }

   49 

   50         // Context

   51         class SortedList

   52         {

   53             private readonly List<Person> list = new List<Person>();

   54             public ISortStrategy SortStrategy { private get; set; }

   55 

   56             public void Add( Person person )

   57             {

   58                 list.Add( person );

   59             }

   60 

   61             public void Sort()

   62             {

   63                 SortStrategy.Sort( list );

   64                 Console.WriteLine( SortStrategy.GetType().Name);

   65                 foreach ( var person in list )

   66                 {

   67                     Console.WriteLine( " " + person.Name );

   68                 }

   69                 Console.WriteLine();

   70             }

   71         }

   72 

   73         private static void Main()

   74         {

   75             var list = new SortedList();

   76 

   77             list.Add( new Person { Name = "Person1", Age = 25 } );

   78             list.Add( new Person { Name = "Person2", Age = 28 } );

   79             list.Add( new Person { Name = "Person3", Age = 26 } );

   80             list.Add( new Person { Name = "Person4", Age = 20 } );

   81 

   82             list.SortStrategy = new SelectionSort();

   83             list.Sort();

   84 

   85             list.SortStrategy = new LINQSort();

   86             list.Sort();

   87             // Output : SelectionSort

   88             //          Person4

   89             //          Person1

   90             //          Person3

   91             //          Person2

   92 

   93             //          LINQSort

   94             //          Person4

   95             //          Person1

   96             //          Person3

   97             //          Person2

   98 

   99             Console.Read();

  100         }

  101     }

  102 }

As you can see in the above example, we have 2 strategies to sort a list. There is a context class which is exposed to the client where it can select a particular strategy. We can use the strategy pattern when we have different classes only differing in behavior or when there are different algorithms and we have to give the selection to the client.

In my next post I will write about the State Pattern.




Name:
E-mail:
Website:
Comment:
 
Anti Bot Image:

Insert Cancel


Subscribe

Random Photo

My Tweets


Top Posts

Source Code

The source code to this site is open-source. You can download the code from here.

Categories


Recent Blogs


Archives


Blogroll