Strategy Pattern


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

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

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

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

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

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

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

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

Real world Example:

<pre>
   public abstract class SortingStrategy
    {
        public abstract void Sort(ArrayList list);
    }

    public class MergeSort : SortingStrategy
    {
        public override void Sort(ArrayList list)
        {
            Console.WriteLine("Sorting using MergeSort");
            //Logic to implement Merge Sort here
            list.Sort();
        }
    }

    public class QuickSort : SortingStrategy
    {
        public override void Sort(ArrayList list)
        {
            Console.WriteLine("Sorting using QuickSort");
            //Logic to implement Quick Sort here
            list.Sort();
        }
    }

    public class SortedList
    {
        private ArrayList list;

        private SortingStrategy strategy;

        public SortedList()
        {
            list = new ArrayList();
        }

        public void SetSortingStrategy(SortingStrategy strategy)
        {
            this.strategy = strategy;
        }

        public void Add(string item)
        {
            list.Add(item);
        }

        public void SortList()
        {
            strategy.Sort(list);
            foreach (object item in list)
            {
                Console.WriteLine(item.ToString());
            }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            SortedList test = new SortedList();
            test.Add("a");
            test.Add("d");
            test.Add("b");

            test.SetSortingStrategy(new MergeSort());
            test.SortList();

            test.SetSortingStrategy(new QuickSort());
            test.SortList();

            Console.Read();

        }
    }

Source: dofactory.com

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: