Strategy Pattern

Posted: February 24, 2014 in Behavioral Patterns, Design Patterns
Tags: , ,

A strategy is a plan of action designed to achieve a specific goal.

Definition

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

UML class diagram

st_1

Participants

The classes and/or 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.
Code Sample

IStrategy Interface & Discount Algorithm implementation

st_2
namespace StrategyPattern
{
    public interface IStrategy
    {
        int FinalBill(int billAmmount);

    }
}

namespace StrategyPattern
{
    public class HighDiscountStrategy : IStrategy
    {

        public int FinalBill(int billAmmount)
        {
            return (int)(billAmmount - (billAmmount * 0.5));
        }
    }
}

namespace StrategyPattern
{
    public class LowDiscountStrategy : IStrategy
    {
        public int FinalBill(int billAmmount)
        {
            return (int)(billAmmount - (billAmmount * 0.1));
        }
    }
}

namespace StrategyPattern
{
    public class NoDiscountStrategy : IStrategy
    {

        public int FinalBill(int billAmmount)
        {
            return billAmmount;
        }
    }
}

namespace StrategyPattern
{
    public class ShoppingMall
    {
        public string CustomerName { get; set; }
        public int BillAmount { get; set; }
        public IStrategy CurrentStrategy;

        public ShoppingMall(IStrategy strategy)
        {
            CurrentStrategy = strategy;
        }

        public int FinallBill()
        {
            return CurrentStrategy.FinalBill(BillAmount);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        //Monday Customer
        var mall = new ShoppingMall(new LowDiscountStrategy())
        {
            CustomerName = "Kishore",
            BillAmount = 10000
        };

        Console.WriteLine(" Total Bill Amount :" + mall.BillAmount);
        Console.WriteLine(" Final Bill to be paid after discount :" + mall.FinallBill());

        //Wednesday Customer
        var mall1 = new ShoppingMall(new HighDiscountStrategy())
        {
            CustomerName = "Borra",
            BillAmount = 10000
        };

        Console.WriteLine(" Total Bill Amount :" + mall1.BillAmount);
        Console.WriteLine(" Final Bill to be paid after discount :" + mall1.FinallBill());

        //Sunday Customer
        var mall2 = new ShoppingMall(new NoDiscountStrategy())
        {
            CustomerName = "Borra",
            BillAmount = 10000
        };

        Console.WriteLine(" Total Bill Amount :" + mall2.BillAmount);
        Console.WriteLine(" Final Bill to be paid after discount :" + mall2.FinallBill());

        Console.ReadLine();

    }
}
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