+91 20 24210201/301 info@genex.co.in

Factory Design Pattern

Factory Design pattern is based on Encapsulation object oriented concept. Factory method is used to create different object from factory often refereed as Item and it encapsulate the creation code. So instead of having object creation code on client side we encapsulate inside Factory method.

Factory design pattern is used to create objects and it provides loose coupling and high cohesion. Factory pattern encapsulate object creation logic which makes it easy to change it later when you change how object gets created or you can even introduce new object with just change in one class. In GOF pattern list Factory pattern is listed as Creation design pattern. Factory should be an interface and clients first either creates factory or get factory which later used to create objects.

Problem which is solved by Factory method Pattern:
Whenever we talk about object oriented language it will based upon some concept like abstraction, polymorphism etc. and on that encapsulation and delegation are important concept any design will be called good if task are delegated to different object and some kind of encapsulation is there.

Sometime our application or framework will not know that what kind of object it has to create at run-time it knows only the interface or abstract class and as we know we cannot create object of interface or abstract class so main problem is frame work knows when it has to create but don’t know what kind of object.

Whenever we create object using new() we violate principle of programming for interface rather than implementation which eventually result in inflexible code and difficult to change in maintenance. By using Factory design pattern we get rid of this problem.

Another problem we can face is class needs to contain objects of other classes or class hierarchies within it; this can be very easily achieved by just using the new keyword and the class constructor. The problem with this approach is that it is a very hard coded approach to create objects as this creates dependency between the two classes.

So factory pattern solve this problem very easily by model an interface for creating an object which at creation time can let its subclasses decide which class to instantiate, Factory Pattern promotes loose coupling by eliminating the need to bind application-specific classes into the code. The factory methods are typically implemented as virtual methods, so this pattern is also referred to as the “Virtual Constructor”. These methods create the objects of the products or target classes.

When to use Factory design pattern:
  • Static Factory methods are common in frameworks where library code needs to create objects of types which may be sub classed by applications using the framework. 
  • Some or all concrete products can be created in multiple ways, or we want to leave open the option that in the future there may be new ways to create the concrete product.
  • Factory method is used when Products don’t need to know how they are created.
  • We can use factory pattern where we have to create an object of any one of sub-classes depending on the data provided. 

    Advantage of Factory method Pattern:
  • Factory method design pattern decouples the calling class from the target class, which result in less coupled and highly cohesive code?
  • Factory pattern enables the subclasses to provide extended version of an object, because creating an object inside factory is more flexible than creating an object directly in the client. Since client is working on interface level any time you can enhance the implementation and return from Factory.
  • Another benefit of using Factory design pattern is that it encourages consistency in Code since every time object is created using Factory rather than using different constructor at different client side.
  • Code written using Factory design pattern is also easy to debug and troubleshoot because you have a centralized method for object creation and every client is getting object from same place.
Example (C#):

using System;
class Program
{
    abstract class Position
    {
public abstract string Title { get; }
    }
 
    class Manager : Position
    {
public override string Title
{
   get
   {
return "Manager";
   }
}
    }
 
    class Clerk : Position
    {
public override string Title
{
   get
   {
return "Clerk";
   }
}
    }
 
    class Programmer : Position
    {
public override string Title
{
   get
   {
return "Programmer";
   }
}
    }
 
    static class Factory
    {
///
/// Decides which class to instantiate.
///
public static Position Get(int id)
{
   switch (id)
   {
case 0:
   return new Manager();
case 1:
case 2:
   return new Clerk();
case 3:
default:
   return new Programmer();
   }
}
    }
 
    static void Main()
    {
for (int i = 0; i <= 3; i++)
{
   var position = Factory.Get(i);
   Console.WriteLine("Where id = {0}, position = {1} ", i, position.Title);
}
    }
}
 
Comments (0)

Post a comment


Maximum 250 alphanumerals only