Caveman's Blog

My commitment to learning.

Strategy Pattern

leave a comment »


Strategy pattern is a behavioral software design pattern. In this pattern a family of algorithms can be encapsulated as objects to facilitate the interchangeability of strategies at run-time. In this post I will present an example of a real life scenario where this pattern can be applicable. Brokerage accounts that are used for trading provide the customer with several tax strategies for effective management of gains/losses for a calender year. Typically tax strategies available for a customer of a for Stock/options/Bonds as security types are:

1. First In – First out
2. Last In – First out
3. Highest In – First out
4. Minimum Tax
5. Maximum Gain
6. Average Cost

UML Class diagram:

Considering that all of the above strategies have been implemented using one algorithm per strategy, following is how the code implementation looks like:

    class Program
    {
        static void Main(string[] args)
        {
            CustomerGainCalculator cgc = new CustomerGainCalculator(new FIFOStrategy());
            cgc.ComputeGain();

            cgc = new CustomerGainCalculator(new LIFOStrategy());
            cgc.ComputeGain();

            cgc = new CustomerGainCalculator(new MaximizeGainStrategy());
            cgc.ComputeGain();

            Console.ReadLine();
        }
    }

    //Define an abstract class so that the strategies could be used interchangeability.
    public abstract class StrategyBase
    {
        public abstract void CalculateGain();
    }

    //Implement FIFO Strategy
    public class FIFOStrategy : StrategyBase
    {
        public override void CalculateGain()
        {
            Console.WriteLine("First In - First Out strategy has been called");
        }
    }

    //Implement LIFO Strategy
    public class LIFOStrategy : StrategyBase
    {
        public override void CalculateGain()
        {
            Console.WriteLine("Last In - First Out strategy has been called");
        }
    }

    //Implement Maximum Gain Strategy
    public class MaximizeGainStrategy : StrategyBase
    {
        public override void CalculateGain()
        {
            Console.WriteLine("Maximize Gain strategy has been called");
        }
    }

    //Define the following class with an Aggregated StrategyBase variable. This variable in conjuction
    //with the strategy set at the time of instantiating this class, can be used to compute
    //the gain/loss for a given tax strategy.
    public class CustomerGainCalculator
    {
        private StrategyBase currentStrategy;

        public CustomerGainCalculator(StrategyBase currentStrategy)
        {
            this.currentStrategy = currentStrategy;
        }

        public void ComputeGain()
        {
            this.currentStrategy.CalculateGain();
        }
    }

Following is the output when this program is executed:

Advertisements

Written by cavemansblog

May 4, 2011 at 9:51 pm

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

%d bloggers like this: