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.

Frequency of use:

Medium high

UML class diagram

Participants

The classes and objects participating in this pattern are:

Strategy(SortStrategy)

declares an interface common to all supported algorithms. Context
uses this interface to call the algorithm defined by a ConcreteStrategy

ConcreteStrategy(QuickSort, ShellSort, MergeSort)

implements the algorithm using the Strategy interface

Context(SortedList)

is configured with a ConcreteStrategy
object

maintains a reference to a Strategy
object

may define an interface that lets Strategy access its data.

Structural code in C#

This structural code demonstrates the Strategy pattern which encapsulates functionality in the
form of an object. This allows clients to dynamically change algorithmic
strategies.

using System;

namespace DoFactory.GangOfFour.Strategy.Structural

{

///<summary>

/// MainApp startup class for Structural

/// Strategy Design Pattern.

///</summary>

classMainApp

{

///<summary>

/// Entry point into console application.

///</summary>

staticvoid Main()

{

Context context;

// Three contexts following different strategies

context = newContext(newConcreteStrategyA());

context.ContextInterface();

context = newContext(newConcreteStrategyB());

context.ContextInterface();

context = newContext(newConcreteStrategyC());

context.ContextInterface();

// Wait for user

Console.ReadKey();

}

}

///<summary>

/// The 'Strategy' abstract class

///</summary>

abstractclassStrategy

{

publicabstractvoid AlgorithmInterface();

}

///<summary>

/// A 'ConcreteStrategy' class

///</summary>

classConcreteStrategyA : Strategy

{

publicoverridevoid AlgorithmInterface()

{

Console.WriteLine(

"Called ConcreteStrategyA.AlgorithmInterface()");

}

}

///<summary>

/// A 'ConcreteStrategy' class

///</summary>

classConcreteStrategyB : Strategy

{

publicoverridevoid AlgorithmInterface()

{

Console.WriteLine(

"Called ConcreteStrategyB.AlgorithmInterface()");

}

}

///<summary>

/// A 'ConcreteStrategy' class

///</summary>

classConcreteStrategyC : Strategy

{

publicoverridevoid AlgorithmInterface()

{

Console.WriteLine(

"Called ConcreteStrategyC.AlgorithmInterface()");

}

}

///<summary>

/// The 'Context' class

///</summary>

classContext

{

privateStrategy _strategy;

// Constructor

public Context(Strategy strategy)

{

this._strategy = strategy;

}

publicvoid ContextInterface()

{

_strategy.AlgorithmInterface();

}

}

}

Output

Called ConcreteStrategyA.AlgorithmInterface()
Called ConcreteStrategyB.AlgorithmInterface()
Called ConcreteStrategyC.AlgorithmInterface()

Real-world code in C#

This real-world code demonstrates the Strategy pattern which encapsulates sorting algorithms in the
form of sorting objects. This allows clients to dynamically change sorting
strategies including Quicksort, Shellsort, and Mergesort.

All other patterns (and much more) are available in our .NET Design Pattern Framework 4.5.

Not only does the .NET Design Pattern Framework 4.5 cover GOF and Enterprise patterns, it also includes
.NET pattern architectures that reduce the code you need to write by up to 75%.
This unique package will change your .NET lifestyle -- for only $79. Here's what is included: