SIGN UP MEMBER LOGIN:    
ARTICLE

Factory Patterns in C#

Posted by Rajesh VS Articles | Coding Best Practices December 10, 2001
The FACTORY METHOD PATTERN comes under the classification of Creational Patterns. The creational patterns deals with the best way to create objects.
Reader Level:

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: rajeshvs@msn.com
/* 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.

Login to add your contents and source code to this article
Article Extensions
Contents added by Pradeep Kumar on Jan 07, 2010
share this article :
post comment
 

Do u know how implement Factory pattern using XML ? Like in Java

Posted by anish abrah Nov 21, 2007

Thanks for the article. Could you please explain what is Facade Factory Pattern and how it works with a simple example

Posted by Chris R Sep 06, 2007

A very simple n humble article whicch helps newbies like me. Thank you very much n expecting more like these.

Posted by Omar Jun 06, 2007
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor