none
Factory and Strategy Pattern

    Question

  • Hello every one.

    I am started learning design pattrens. Initially  for me Factory and Stratefy patterns looks much similar. Could any one explain the difference between two and also when to use factory pattren and when to use strategy pattren.

    Thanks,
    Venkat
    venkat
    Friday, August 14, 2009 6:09 PM

Answers

  • They're very similar indeed. 

    Short answer:

    Factory pattern is for objects.
    Strategy pattern is for methods/functions/specific algorithms.

    Longer answer:

    The difference between them is that strategy pattern is used to dynamically select an algorithm at runtime, while the factory pattern is used to dynamically construct an object at runtime.  In one situation, you'd probably return (in C# or VB anyways) a delegate to a method, whereas in the other, you would return a constructed object. 

    In strategy pattern, the delegate could be passed around to other methods which could then call the method, without having to care about the implementation, whereas with factory pattern, the object is passed around.

    In object oriented programming, factory pattern is more common.
    In functional programming, strategy pattern is more common.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    Friday, August 14, 2009 6:14 PM
  • I like the Head First example of the Strategy Pattern. [It is what finally got me to understand]

    //Example

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace StrategyPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Mallard Duck
                Duck mallard = new MallardDuck();
                mallard.Display();           
                mallard.PerformFly();
                mallard.PerformQuack();

                Console.WriteLine("");

                //Decoy Duck
                Duck decoy = new DecoyDuck();
                decoy.Display();
                decoy.PerformFly();
                decoy.PerformQuack();
                Console.ReadLine();
            }
        }

       #region Interfaces

        interface IFlyBehavior
        {
            void Fly();
        }

        interface IQuackBehavior
        {
            void Quack();
        }

       #endregion

       #region Duck Class

        abstract class Duck
        {
            protected IFlyBehavior flyBehavior;
            protected IQuackBehavior quachBehavior;

            public abstract void Display();

            public void Swim()
            {
               Console.WriteLine("I am swimming");
            }

            public void PerformFly()
            {
                flyBehavior.Fly();
            }

            public void PerformQuack()
            {
                quachBehavior.Quack();
            }

            public void SetFlyBehavior(IFlyBehavior fb)
            {
                flyBehavior = fb;
            }

            public void SetQuackBehavior(IQuackBehavior qb)
            {
                quachBehavior = qb;
            }
        }

       #endregion

       #region Behaviors

        class FlyWithWings : IFlyBehavior
        {
            public void Fly()
            {
                Console.WriteLine("I am flying with wings");
            }
        }

        class FlyNoWay : IFlyBehavior
        {
            public void Fly()
            {
                Console.WriteLine("I cannot fly");
            }
        }

        class Quacking : IQuackBehavior
        {
            public void Quack()
            {
                Console.WriteLine("I am quacking");
            }
        }

        class MuteQuack : IQuackBehavior
        {
            public void Quack()
            {
                Console.WriteLine("I cannot quack");
            }
        }

        class Squeak : IQuackBehavior
        {
            public void Quack()
            {
                Console.WriteLine("I am squeaking");
            }
        }

       #endregion

       #region Ducks

        class MallardDuck : Duck
        {
            public MallardDuck()
            {
                flyBehavior = new FlyWithWings();
                quachBehavior = new Quacking();
            }

            public override void Display()
            {
                Console.WriteLine("I am a Mallard Duck");
            }
        }

        class DecoyDuck : Duck
        {
            public DecoyDuck()
            {
                flyBehavior = new FlyNoWay();
                quachBehavior = new MuteQuack();
            }

            public override void Display()
            {
                Console.WriteLine("I am a Decoy Duck");
            }
        }

       #endregion
    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Saturday, August 15, 2009 4:12 PM
  • And adding to this - the combination of a factory pattern and a strategy pattern, i.e. selecting an implementation based on an interface, then instantiating that implementation is also known as a provider model.  This is a very useful thing to get to know.

    Thanks,

    Martin.

    MCSD, MCTS, MCPD. Please mark my post as helpful if you find the information good!
    Thursday, August 20, 2009 2:49 AM

All replies

  • They're very similar indeed. 

    Short answer:

    Factory pattern is for objects.
    Strategy pattern is for methods/functions/specific algorithms.

    Longer answer:

    The difference between them is that strategy pattern is used to dynamically select an algorithm at runtime, while the factory pattern is used to dynamically construct an object at runtime.  In one situation, you'd probably return (in C# or VB anyways) a delegate to a method, whereas in the other, you would return a constructed object. 

    In strategy pattern, the delegate could be passed around to other methods which could then call the method, without having to care about the implementation, whereas with factory pattern, the object is passed around.

    In object oriented programming, factory pattern is more common.
    In functional programming, strategy pattern is more common.
    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiTwitterLinkedInForumsBrowser
    Friday, August 14, 2009 6:14 PM
  • Hi,
    Thanks David for short and powerful answer.
    We are volunteers, if the reply help you mark it as your answer. thanks!!
    My Blog
    Saturday, August 15, 2009 1:06 PM
  • studying the design pattern is good but the try to implement the design pattern in real time scenario where you need to solve a software engineering problem.
    Saturday, August 15, 2009 2:19 PM
  • I like the Head First example of the Strategy Pattern. [It is what finally got me to understand]

    //Example

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace StrategyPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                //Mallard Duck
                Duck mallard = new MallardDuck();
                mallard.Display();           
                mallard.PerformFly();
                mallard.PerformQuack();

                Console.WriteLine("");

                //Decoy Duck
                Duck decoy = new DecoyDuck();
                decoy.Display();
                decoy.PerformFly();
                decoy.PerformQuack();
                Console.ReadLine();
            }
        }

       #region Interfaces

        interface IFlyBehavior
        {
            void Fly();
        }

        interface IQuackBehavior
        {
            void Quack();
        }

       #endregion

       #region Duck Class

        abstract class Duck
        {
            protected IFlyBehavior flyBehavior;
            protected IQuackBehavior quachBehavior;

            public abstract void Display();

            public void Swim()
            {
               Console.WriteLine("I am swimming");
            }

            public void PerformFly()
            {
                flyBehavior.Fly();
            }

            public void PerformQuack()
            {
                quachBehavior.Quack();
            }

            public void SetFlyBehavior(IFlyBehavior fb)
            {
                flyBehavior = fb;
            }

            public void SetQuackBehavior(IQuackBehavior qb)
            {
                quachBehavior = qb;
            }
        }

       #endregion

       #region Behaviors

        class FlyWithWings : IFlyBehavior
        {
            public void Fly()
            {
                Console.WriteLine("I am flying with wings");
            }
        }

        class FlyNoWay : IFlyBehavior
        {
            public void Fly()
            {
                Console.WriteLine("I cannot fly");
            }
        }

        class Quacking : IQuackBehavior
        {
            public void Quack()
            {
                Console.WriteLine("I am quacking");
            }
        }

        class MuteQuack : IQuackBehavior
        {
            public void Quack()
            {
                Console.WriteLine("I cannot quack");
            }
        }

        class Squeak : IQuackBehavior
        {
            public void Quack()
            {
                Console.WriteLine("I am squeaking");
            }
        }

       #endregion

       #region Ducks

        class MallardDuck : Duck
        {
            public MallardDuck()
            {
                flyBehavior = new FlyWithWings();
                quachBehavior = new Quacking();
            }

            public override void Display()
            {
                Console.WriteLine("I am a Mallard Duck");
            }
        }

        class DecoyDuck : Duck
        {
            public DecoyDuck()
            {
                flyBehavior = new FlyNoWay();
                quachBehavior = new MuteQuack();
            }

            public override void Display()
            {
                Console.WriteLine("I am a Decoy Duck");
            }
        }

       #endregion
    }


    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Saturday, August 15, 2009 4:12 PM
  • And adding to this - the combination of a factory pattern and a strategy pattern, i.e. selecting an implementation based on an interface, then instantiating that implementation is also known as a provider model.  This is a very useful thing to get to know.

    Thanks,

    Martin.

    MCSD, MCTS, MCPD. Please mark my post as helpful if you find the information good!
    Thursday, August 20, 2009 2:49 AM