Design Pattern --Factory Patterns

A Factory Patterns creates an Interface for creating object and lets subclass do the instantiation. A Factory Pattern lets a class to defer instantiation to the subclasses. Simply saying it abstracts the instance creation.
 
Overview:
It lets the developer create an interface and retains which class needs to be instantiated. The Factory pattern comes under the category of Creational Patterns.
Generally when we create a class, we will provide constructors to the users of the class so they can create instances for the classes. But on some situations we need or should not give them the ability to create instances with the available classes. On such situations we can create Factory Pattern. That is rather than calling the constructors for creating objects, you are calling the Factory to create objects.

Its primary purpose is to create objects like a real work Factory.
Logical Model:
The prime actors of the Factory Pattern are:
· Client

· Factory

· Product

Product:
A product can be anything you produce in the factory. In programmatic angle it will the actual object which needs to be created.
Factory:
A Factory is the one who creates object of the product.
Client:
A client instead of directly creating objects of the product uses the Factory to create objects for it. The client is not aware about how the objects are created.
Factory Pattern in .net Framework:
Some of the areas where Factory Pattern is used are given below,
 IClassFactory is an interface used to create instances of COM classes.
· System.Web.IHttpHandlerFactory class
· System.Convert
Example in C#:
In this example we are going to create we use Abstract classes instead of Interfaces as Abstract classes has the benefits like versioning, etc., over Interfaces.

using System;
using System.Collections.Generic;
using System.Text;
namespace FactoryDemo
{
abstract class Car
{
public abstract int Speed { get; }
}
abstract class CarFactory
{
public abstract Car ShowCar();
}
//Concrete is nothing but the implementaion for the absract classes
class ConcreteCar : Car
{
int _speed = 300;
public override int Speed
{
get { return _speed; }
}
}

class ConcreteCarFactory : CarFactory
{
public override Car ShowCar()
{
return new ConcreteCar();
}
}

class CarCompany
{
public void ProduceCar(CarFactory objfac)
{
Car mycar = objfac.ShowCar();
Console.Write("Car Produced with speed: " + mycar.Speed);
Console.Read();
}
}
class Program
{
static void Main(string[] args)
{
CarFactory mycarfactory = new ConcreteCarFactory();
new CarCompany().ProduceCar(mycarfactory);
}
}
}

Here the abstract classes Car and CarFactory provides the interface and the classes ConcreteCar and ConcreteCarFactory provides the implementation for the abstract classes. The CarCompany class uses all these to make the factory work.
In the simillar way you can create as many of Cars with different model like RollsRoye, Honda and there by creating classes for them by inheriting the classes Car and CarFactory.
But remember that the CarCompany class is abstracted from other classes.

No comments: