Design patterns - Part 1-Factory Pattern in .NET

What is a factory? .. A factory is a place where objects are created. Factory Pattern is a well known pattern which will help you to create objects when the following scenario may arrive.
 
Suppose we have a factory of cars which builds cars of type Ford, Ambassador, Mclaren, Ferrari.  The existing code has a method CreateCar() that takes in cartype as an argument and returns the required Car object. The existing code may be as follows….
 
                        public enum CarType{
                 
                  Ford=0,
                  Ambassador=1,
                  Mclaren=2,
                  Ferrari=3
                       
                           }
 
            public Car(CarType cartype)
                  {
 
                        if(cartype==CarType.Ford)
                              {
                                    // return Ford object
                              }
                        else if(cartype==CarType.Ambassador)
                              {
                                    //return Ambassador object
                              }
                              …
                              …
                              …
                  }    
 
The above code works pretty well as of now. But it has problems in terms of scalability and maintainability. If now we need to increase the type of cars available, the only way we can do that is to keep on increasing the number of if/else-if constructs. This produces more of un-maintainable code. Here comes the need of Factory Pattern.
 
Encapsulating the logic inside a CarFactory solves a part of the problem , but our factory also need to be both scalable and maintainable in the sense that it needs to provide a generic solution to creational logic , and not just the current workflow scope.
 
Now we are increasing our range of cars , as told before. Therefore to start of with are increasing the enum items.
 
           
 
                 enum CarType
      {
            Ford,
            Mitsubishi,
            Ambassador,
            Peugot
      }
 
Now we create the required classes
 
      public abstract class Car
      {
      }
 
      public class Ford:Car
      {
           
      }
 
      public class Mitsubishi:Car
      {
           
      }
      public class Ambassador:Car
      {
           
      }
      public class Peugot:Car
      {
           
      }
 
Car here is an abstract class that is being inherited by the child classes whose instance we are trying to create.
 
Now we create the actual CarFactory.
 
class CarFactory
      {
            Hashtable _registeredcars = new Hashtable();
            Hashtable _loadedcars = new Hashtable();
 
            ///<summary>
            /// Asscociating the CarType with the actual object
            ///</summary>
            ///<param name="cartype"></param>
            ///<param name="car"></param>
            public void Register(CarType cartype,Type car)
            {
                  _registeredcars.Add(cartype,car);
            }
 
            public void Register(string cartype,Type car)
            {
                  _registeredcars.Add(cartype,car);
            }
            public Car CreateCar(CarType cartype)
            {
                  if(_loadedcars[cartype]==null)
                        LoadCar();
                  return (Car)_loadedcars[cartype];
            }
 
            public void LoadCar()
            {
                  foreach(DictionaryEntry obj in _registeredcars)
                  {
                        Type type = (Type)obj.Value;
                        //  could have used Activator class
ConstructorInfo info = type.GetConstructor(new Type[] {});
_loadedcars.Add(obj.Key,(Car)info.Invoke(new object[]{}));
                  }
 
            }
      }
 
 
The CarFactory class consists of two Hashtables registeredcars and loadedcars.
The Register method add the registerd cars to the hashtable. This is the full list of objects that you can create out of this factory. It is quite evident from the Register method that it adds a cartype and car in the Hashtable and performs the logical connection between the two.
 
The CreateCar method takes a cartype as argument and returns a car object. While creating a car it checks whether the asked-for car object is already loaded, it returns the object from the loadedcars Hashtable, otherwise it first loads it and then returns the object.
 
The LoadCar method does the main job in creating the asked-for car object and returning it. It loops in the registeredcars Hashtable to check whether the object is there , already registered in the Hashtable. It uses Reflection to create the type and adds it in the loadedcars Hashtable.
 
Now as the factory is implemented , it start using it …
 
      static void Main(string[] args)
            {
                  CarFactory factory = new CarFactory();
                 
                 
                  //registering cars
factory.Register(CarType.Ambassador,typeof(Ambassador));
                  factory.Register(CarType.Ford,typeof(Ford));
 
                  Car _car = factory.CreateCar(CarType.Ford);
                  Console.WriteLine(_car.GetType().ToString());
 
                  Console.ReadLine();
 
            }
 
The cars that are needed are first registered, using a call to the Register method. Then we create the reuired car object by calling the Createcar() and passing the suitable cartype.
 
You can clearly see that any object that is registered ,can be created on runtime without the use of the earlier if-else… blocks.
 
 
 
 
Print | posted on Monday, May 21, 2007 8:58 PM

Feedback

# re: Design patterns - Part 1-Factory Pattern in .NET

left by vishnusumanth at 7/8/2008 12:21 AM Gravatar
good explanation about factory

Thank u

# re: Design patterns - Part 1-Factory Pattern in .NET

left by suresh at 7/8/2008 12:42 AM Gravatar
it helped me to understand this pattern well. Thank u

# re: Design patterns - Part 1-Factory Pattern in .NET

left by LaxmiNarayanan.C at 3/3/2009 6:34 AM Gravatar
Very simple for new comers and self explanatory!!!

# re: Design patterns - Part 1-Factory Pattern in .NET

left by Sanjay at 3/16/2009 9:06 PM Gravatar
Very nicely and uniquely explained .

# re: Design patterns - Part 1-Factory Pattern in .NET

left by Kalyan at 7/26/2009 1:19 PM Gravatar
Very nice Explanation..

Thank U

# re: Design patterns - Part 1-Factory Pattern in .NET

left by Manoj Tayde at 9/2/2009 10:51 AM Gravatar
Its nice! I got clear understanding of Factory Design Pattern.
Thanx :D

# re: Design patterns - Part 1-Factory Pattern in .NET

left by Hiran at 11/10/2009 11:26 PM Gravatar
Fantastic...It realy helped me to understand the basic Concepts. Also the example that used is realy simple to understand..Thanks buddy..Good Work

# re: Design patterns - Part 1-Factory Pattern in .NET

left by Santosh at 1/1/2010 7:52 AM Gravatar
Thank you.. very neat..

# re: Design patterns - Part 1-Factory Pattern in .NET

left by Mahadevi at 1/12/2010 3:08 AM Gravatar
Very nice Explanation...!

Thank U very much

# re: Design patterns - Part 1-Factory Pattern in .NET

left by Phani at 2/7/2010 11:33 AM Gravatar
It really helped me to understand well. I like the approach of yours towards this topic... hope many more to get from u
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification: