A day with .Net

My day to day experince in .net

Strategy Pattern

Posted by vivekcek on May 5, 2015

This pattern enables an algorithm’s behavior to be selected at run-time.

The strategy pattern:

defines a family of algorithms
encapsulates each algorithm, and
makes the algorithms interchangeable within that family.

We can look at practical implementation in an e-commerce website.
The customer make an order and select a shipping method, based on the shipping method we need to calculate the shipping cost.

The above problem can be solved with strategy pattern.

ClassDiagram1

First we will define an interface that represent our strategy

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    public interface IShippingStrategy
    {
        double GetShippingCost(Order order);
    }
}

Now we will define the concrete implementation of our strategy

Blue Dart

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    public class BlueDart : IShippingStrategy
    {
        public double GetShippingCost(Order order)
        {
            return 30.0;
        }
    }
}

Professional

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    public class Professional:IShippingStrategy
    {
        public double GetShippingCost(Order order)
        {
            return 50.0;
        }
    }
}

India Post

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    public class IndiaPost:IShippingStrategy
    {
        public double GetShippingCost(Order order)
        {
            return 10.0;
        }
    }
}

Now we will define our context class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    public class ShippingContext
    {
        private IShippingStrategy _startegy;

        public ShippingContext(IShippingStrategy strategy)
        {
            _startegy = strategy;
        }

        public double GetShippingCostForOrder(Order order)
        {
            return _startegy.GetShippingCost(order);
        }

    }
}

Now we can use our pattern

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StrategyPattern
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<int, IShippingStrategy> _stratagies = new Dictionary<int, IShippingStrategy>();
            _stratagies.Add(1, new BlueDart());
            _stratagies.Add(2, new Professional());
            _stratagies.Add(3, new IndiaPost());

            Console.WriteLine("Your order created");
            Console.WriteLine("Select Shipping Method");
            Console.WriteLine("-----------------------");
            Console.WriteLine("1. BlueDart");
            Console.WriteLine("2. Professional");
            Console.WriteLine("3. India Post");

            int choice=Convert.ToInt32(Console.ReadLine());

            ShippingContext context = new ShippingContext(_stratagies[choice]);
            Console.WriteLine(context.GetShippingCostForOrder(new Order()).ToString());
            Console.Read();
        }
    }
}

Order class is just a mock class.

Advertisements

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