Factory Pattern

This is a continuation of Object Builder pattern. Before heading towards Object builder pattern in MVC, a quick look at Factory pattern and Abstract Factory pattern will help us a lot. So lets look at factory pattern first and then elevate it to Abstract Factory.

In real world, a factory is something that manufactures some goods. For instance shoe factory manufactures shoes. Shoes can have different styles, sports shoes and office shoes.

Why the factory is needed when the shoes can be made at shoe stores given raw materials? Factory with machinery can produce thousands of shoes per day, with varying patterns. Also when you want to change the pattern, its easily done in the factory. Change in the pattern doesn’t affect any stake holders.

Something similar is in the scenario of factory pattern in the software paradigm. Factory is built in mind using specific family of product (like shoes). Based on the order, for example sports shoes or office shoes, it fulfills the order. Whoever goes to store gets the shoe (an abstract product), specifically a sports or office shoes(concrete product). Lets implement this in terms of program.


namespace TestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            //Store is ordering the factory to get office shoes
            Shoe shoe = ShoeFactory.GetShoes("OFFICE");
            Console.WriteLine(shoe.GetType()); //Will be office shoes

            //Store is ordering the factory to get sports shoes
            shoe = ShoeFactory.GetShoes("SPORTS");
            Console.WriteLine(shoe.GetType()); //Will be sports shoes
        }
    }

    //This is the factory supplying shoes. It can supply sports shoes or office shoes
    //based on demand
    class ShoeFactory
    {
        public static Shoe GetShoes(string strShoeType)
        {
            switch (strShoeType)
            {
                case "SPORTS": return new SportShoe();
                    break;
                case "OFFICE": return new OfficeShoe();
                    break;
                default:
                    return null;
            }
        }
    }

    //This is an abstract class representing product family
    //In this example, its shoe
    public abstract class Shoe
    {

    }

    //Office shoe is a concrete class belongs to shoe family
    class OfficeShoe : Shoe
    {   

    }

    //Sports shoe is a concrete class belongs to shoe family
    class SportShoe : Shoe
    {

    }
}

Like I said, if shoe factory has to produce another pattern of shoes (like Army shoes), all that needs to be done is put one more case (just like in real factory machinery is customized) and return a new pattern shoe. Remember, a factory can produce only one family of product. Customization within the boundary of the family is allowed. For example, shoe size, color can be customized.

Mainly factory is used when we want to hide the complexity of object construction. For example you want to get list of cars. So each car object is initialized by factory based on its build and appearance. So list of cars contain different cars, initialized by CarFactory.

Factory can contain as many methods for construction as you want, provided the product returned belongs to the same family. For example, you want a car which is built in 80s. So you may add a function called Get_Me_A_Car_BuiltIn80s(). This function will return a car, and is part of CarFactory. Factory should only be a place where the product should be created.

Factory class can be made into a component, which then can be used across multiple applications. This is similar to orders from multiple shoe stores across the country, which the shoe factory fulfills.

In my next post, I will explain abstract factory pattern, and its application.

About these ads

4 comments on “Factory Pattern

  1. [...] Factory method pattern is bit different from simple factory pattern. [...]

  2. Greg says:

    It would be better to use constraints on that Factory as opposed to a switch statement. Makes it so someone could create 100 new types of shoes and you wouldn’t have to change the code at all.

  3. Hi my friend! I wish to say that this article is amazing, great written and come with almost all important infos. I would like to peer more posts like this .

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