Friday, June 3, 2011

Factory Method Design Pattern in .Net (C#)

Factory Method Pattern
Like Abstract Factory Pattern, Factory Method Pattern is also a part of Creational patterns. This pattern is also to create concrete product objects. There are many ways are available to implement Factory Method Pattern in .Net. Here I will demostrate a typical structure of Factory Method Pattern.
Factory Method Pattern Structure


  1. Creator: An interface to create product objects. which contains either factor method declarations or factory methods itself. (In .Net an Abstract class or Interface is used to create Creator).

  2. Concrete Creator: It implements the factory methods declared within the Creator. (It will be class declaration)

  3. Product: Declares an interface to define products. (It will be either an Abstract class or an Interface)

  4. Concrete Product: It implements the Product to define a concrete object. (it will be class).

Have a look on the following example. Here in this example I have created all the above mentioned items.


1. Creating Product. In the example I have used an Interface named "IFruit" to create a Product.


Note: Here in our example I have used Interface instead of base classe, you can use abstract classes because abstract classes offer versioning benefits over pure interfaces.


    public interface IFruit
{
string GetDescription();
}

2. Creating concrete products. In our example I have created two concrete products named "Apple" and "Orange".

    public class Apple : IFruit
{
public string GetDescription()
{
return "I'm an Apple....!";
}
}

public class Orange : IFruit
{
public string GetDescription()
{
return "I'm an Orange....!";
}
}



3. Creating Creator: In our example I have created a creator named "IFruitFactory".

    public interface IFruitFactory
{
IFruit CreateApple();
IFruit CreateOrange();
}
4. Creating Concrete Creator: We have a concrete creator named "FruitFactory" is in our example.
    public class FruitFactory : IFruitFactory
{
public IFruit CreateApple()
{
return new Apple();
}

public IFruit CreateOrange()
{
return new Orange();
}
}
Next we can consume our sample application
            IFruitFactory fruitFactory = new FruitFactory();
IFruit fruit;

fruit = fruitFactory.CreateApple();
Console.WriteLine(fruit.GetDescription());

fruit = fruitFactory.CreateOrange();
Console.WriteLine(fruit.GetDescription());



Hope all of you are clear with Factory Method pattern. Above I mentioned only the typical way of defining Factory Method Pattern; there are several other ways to implement the same. Try it yourself. :-)

9 comments:

Hina said...

Easiest example on web for Factory method. Really like it and was able to understand this pattern in just 2 minutes :)

Thanks!

Sathiya said...

Excellent

Anonymous said...

Nice to learn in a glance :)

Dhandapani M

Anonymous said...

Really helpful. Excellent

Anonymous said...

Can u plz post other articles explaining rest of the design patterns.And Your examples are very easy to understand.Plz use easy examples..........
Thanks...

Paritosh said...

I doubt this example as it violate the Interface Segregation Principle Solid rule..

I might be wrong but just want to check with you one more time...

Anonymous said...

http://dotnet.dzone.com/articles/design-patterns-c-factory

Anonymous said...

got in first attempt. Very easy to understand. Thanks!

Octavius Gomez said...

An interface to create product things. which contains either aspect method conditions or manufacturer methods itself.