Factory Patterns in C#


The FACTORY METHOD PATTERN comes under the classification of Creational Patterns. The creational patterns deals with the best way to create objects. The Factory Method provides a simple decision making class that can return the object of one of several subclasses of an abstract base class depending on the information that are provided. 

"Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses." - "Design Patterns" Gamma et al., Addison-Wesley, ISBN:0-201-63361-2" 

Non-software Example 

Injection molding presses demonstrate this pattern. Manufacturers of plastic toys process plastic molding powder, and inject the plastic into molds of the desired shapes. The class of toy (car, action figure, etc.) is determined by the mold. [Michael Duell, "Non-software examples of software design patterns", Object Magazine, Jul 97, p54] 

A factory pattern is one that returns an instance of one of several possible classes depending on the data provided to it. Usually all of the classes it returns should have a common base class and common methods, but implementations of the methods may be different. 

The following is an UML representation of the Factory Method Pattern. In this case we are not directly creating an instance of the class Derived1 and Derived2. Instead we are using the getObject() method of the Factory class to return an appropriate instance depending on the value passed to the getObject() method. This method is commonly knows as the Factory method and Factory method can be either static or non-static in nature.

C# Implementation

//Creational Pattern: The Factory Method
//Author: [email protected]
/* In Factory method pattern, A Factory class contains a factory method is used for creating the object. This factory method can be either static or non-static. */
using System;
class Factory
{
public Base GetObject(int type)
{
Base base1 =
null;
switch(type)
{
case 1:
base1 =
new Derived1();
break;
case 2:
base1 =
new Derived2();
break;
}
return base1;
}
}
interface Base
{
void DoIt();
}
class Derived1 : Base
{
public void DoIt()
{
Console.WriteLine("Derived 1 method");
}
}
class Derived2 : Base
{
public void DoIt()
{
Console.WriteLine("Derived 2 method");
}
}
//Client class//Client class needn't know about instance creation. The creation of Product is
//deferred to the Factory class
class MyClient
{
public static void Main()
{
Factory factory =
new Factory();//Decides which object must create.
Base obj = factory.GetObject(2);
obj.DoIt();
}
}

This is what the fundamental principle of Factory pattern. We create an abstraction, which decides which of several possible classes to return, and returns one. After that we can call the methods of that class instance without ever knowing which derived class is using actually. The object creation happens in a single place that is inside the Factory class.

Remember that the Factory class can contain more than one Factory methods. Even these Factory methods can be either static or non-static.

Up Next
    Ebook Download
    View all
    Learn
    View all