DCSIMG
Factory Method Pattern - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2012 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Factory Method Pattern

Factory Method Pattern

Today, the factory method one of my favorite patterns will be explained.
You can read my previous posts about design patterns here:
Structural patterns
Decorator pattern 
Proxy pattern
Facade pattern
Adapter pattern
Composite pattern
Bridge pattern
Flyweight pattern

Creational patterns
Singleton pattern
Abstract Factory pattern
Prototype Pattern

What is Factory Method
Factory method is a way to delegate the instantiation of a class to subclasses.
The subclass decide which concrete class to instantiate according to its state
or by information supplied by the client.
The pattern helps to connect parallel class hierarchies. 
Use the pattern in the following situations:

  • A client delegate the responsibilities to its subclasses.
  • You need flexible way to create objects.
  • There are good reasons to choose one subclass over another.

For a UML diagram of the pattern go to dofactory site.

Example in C#
Lets look at an example of factory method:

    #region Helper Enum

    public enum eMaterialType

    {

        Wood,

        Iron

    }

    #endregion

    #region Products

    public abstract class Chair

    {

        #region Members

        private float _nHeight;

        private float _nWidth;

        #endregion

        #region Properties

        /// <summary>

        /// The table material

        /// </summary>

        public abstract eMaterialType Material

        {

            get;

        }

        /// <summary>

        /// The height of the table

        /// </summary>

        public float Height

        {

            get

            {

                return _nHeight;

            }

            set

            {

                _nHeight = value;

            }

        }

        /// <summary>

        /// The width of the table

        /// </summary>

        public float Width

        {

            get

            {

                return _nWidth;

            }

            set

            {

                _nWidth = value;

            }

        }

        #endregion

    }

    public class WoodChair : Chair

    {

        #region Properties

        /// <summary>

        /// The chair material

        /// </summary>

        public override eMaterialType Material

        {

            get

            {

                return eMaterialType.Wood;

            }

        }

        #endregion

    }

    public class IronChair : Chair

    {

        #region Properties

        /// <summary>

        /// The chair material

        /// </summary>

        public override eMaterialType Material

        {

            get

            {

                return eMaterialType.Iron;

            }

        }

        #endregion

    }

    #endregion

    #region Creator

    public class ChairCreator

    {

        #region Methods

        /// <summary>

        /// The factory method to create the

        /// chairs

        /// </summary>

        /// <param name="type">The material type</param>

        /// <returns>A chair object</returns>

        public Chair CreateChair(eMaterialType type)

        {

            switch (type)

            {

                case eMaterialType.Iron:

                    {

                        return new IronChair();                   

                    }

                case eMaterialType.Wood:

                    {

                        return new WoodChair();                   

                    }

                default:

                    {

                        return null;                       

                    }

            }

        }

        #endregion

    }

    #endregion

As you can see the example is similar to the example I gave in the
abstract factory post. This time I have built a chair product and the factory
method
is responsible to build the relevant chair. The chairs are build
by the given state type. The ChairCreator could be a subclass of a Creator
abstract class that delegate a Create method but I chose to make a simpler
example.

Summary
To sum up the post, the factory method is simple to implement. The pattern is very
popular. Probably you used it a lot while developing and didn't even know.
I use the pattern in a lot of situations that I need the concrete classes to be chosen
by a creator class which handle their creation.  
In the next post in this series I'm going to write about the last creational
pattern
- the builder pattern.

Comments

Microsoft » Blog Archive » Factory Method Pattern said:

Pingback from  Microsoft  &raquo; Blog Archive   &raquo; Factory Method Pattern

# May 10, 2008 5:06 PM