Strategy Design Pattern in C#

Read full article

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

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;

  class MainApp
  {
    static void Main()
    {
      Context context;

      // Three contexts following different strategies 
      context = new Context(new ConcreteStrategyA());
      context.ContextInterface();

      context = new Context(new ConcreteStrategyB());
      context.ContextInterface();

      context = new Context(new ConcreteStrategyC());
      context.ContextInterface();

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

  // "Strategy" 
  abstract class Strategy
  {
    public abstract void AlgorithmInterface();
  }

  // "ConcreteStrategyA" 
  class ConcreteStrategyA : Strategy
  {
    public override void AlgorithmInterface()
    {
      Console.WriteLine(
        "Called ConcreteStrategyA.AlgorithmInterface()");
    }
  }

  // "ConcreteStrategyB" 
  class ConcreteStrategyB : Strategy
  {
    public override void AlgorithmInterface()
    {
      Console.WriteLine(
        "Called ConcreteStrategyB.AlgorithmInterface()");
    }
  }

  // "ConcreteStrategyC" 
  class ConcreteStrategyC : Strategy
  {
    public override void AlgorithmInterface()
    {
      Console.WriteLine(
        "Called ConcreteStrategyC.AlgorithmInterface()");
    }
  }

  // "Context" 
  class Context
  {
    Strategy strategy;

    // Constructor 
    public Context(Strategy strategy)
    {
      this.strategy = strategy;
    }

    public void ContextInterface()
    {
      strategy.AlgorithmInterface();
    }
  }
Called ConcreteStrategyA.AlgorithmInterface() Called ConcreteStrategyB.AlgorithmInterface() Called ConcreteStrategyC.AlgorithmInterface()

List of Strategy examples

C# examples

C++ examples

Delphi examples

Java examples

PHP examples

Design Patterns

contents