Program.cs 2.4 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Strategy
{
    /// <summary>

    /// MainApp startup class for Structural

    /// Strategy Design Pattern.

    /// </summary>
    /// 

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

    class MainApp
    {
        /// <summary>

        /// Entry point into console application.

        /// </summary>

        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.ReadKey();
        }
    }

    /// <summary>

    /// The 'Strategy' abstract class

    /// </summary>

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

    /// <summary>

    /// A 'ConcreteStrategy' class

    /// </summary>

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

    /// <summary>

    /// A 'ConcreteStrategy' class

    /// </summary>

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

    /// <summary>

    /// A 'ConcreteStrategy' class

    /// </summary>

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

    /// <summary>

    /// The 'Context' class

    /// </summary>

    class Context
    {
        private Strategy _strategy;

        // Constructor

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

        public void ContextInterface()
        {
            _strategy.AlgorithmInterface();
        }
    }
}