DotNetAge - Mvc & jQuery CMS
Hide sidebar

Strategy


Definition


Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.

UML class diagram


strategy

Participants


The classes and/or objects participating in this pattern are:
  • Strategy - declares an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.
  • ConcreteStrategy - implements the algorithm using the Strategy interface
Context
is configured with a ConcreteStrategy object

maintains a reference to a Strategy object

may define an interface that lets Strategy access its data.



Sample code in C#


This allows clients to dynamically change sorting strategies including Quicksort, Shellsort, and Mergesort.




/// <summary>
/// MainApp startup class for Real-World
/// Strategy Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
static void Main()
{
// Two contexts following different strategies
SortedList studentRecords = new SortedList();

studentRecords.Add("Samual");
studentRecords.Add("Jimmy");
studentRecords.Add("Sandra");
studentRecords.Add("Vivek");
studentRecords.Add("Anna");

studentRecords.SetSortStrategy(new QuickSort());
studentRecords.Sort();

studentRecords.SetSortStrategy(new ShellSort());
studentRecords.Sort();

studentRecords.SetSortStrategy(new MergeSort());
studentRecords.Sort();

// Wait for user
Console.ReadKey();
}
}


/// <summary>
/// The 'Strategy' abstract class
/// </summary>
abstract class SortStrategy
{
public abstract void Sort(List<string> list);
}

/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class QuickSort : SortStrategy
{
public override void Sort(List<string> list)
{
list.Sort(); // Default is Quicksort
Console.WriteLine("QuickSorted list ");
}
}

/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class ShellSort : SortStrategy
{
public override void Sort(List<string> list)
{
//list.ShellSort(); not-implemented
Console.WriteLine("ShellSorted list ");
}
}

/// <summary>
/// A 'ConcreteStrategy' class
/// </summary>
class MergeSort : SortStrategy
{
public override void Sort(List<string> list)
{
//list.MergeSort(); not-implemented
Console.WriteLine("MergeSorted list ");
}
}

/// <summary>
/// The 'Context' class
/// </summary>
class SortedList
{
private List<string> _list = new List<string>();
private SortStrategy _sortstrategy;

public void SetSortStrategy(SortStrategy sortstrategy)
{
this._sortstrategy = sortstrategy;
}

public void Add(string name)
{
_list.Add(name);
}

public void Sort()
{
_sortstrategy.Sort(_list);

// Iterate over list and display results
foreach (string name in _list)
{
Console.WriteLine(" " + name);
}
Console.WriteLine();
}
}

 


    Average:
  • Reads
    (1926)
  • Permalink
Share to: Add to del.icio.us Digg! Share on Google Buzz Share on Facebook Reddit! Stumble it! Share on Twitter

Tag cloud

Anything in here will be replaced on browsers that support the canvas element