Design Patterns for all – Chapter III (Factory Pattern)

Tuesday, June 30, 2009

Design Patterns for all – Chapter III (Factory Pattern)


This is a series of articles which explain Design Patterns in detail with C# examples. In the previous chapter we saw the Singleton Design pattern. In this chapter we are going to see the Factory Pattern in detail with c# coding. Factory pattern is one of the widely used Design Patterns in the Object oriented programming world.



Definition:

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.
www.codecollege.NET

0 comments

Post a Comment

LinkWithin

Blog Widget by LinkWithin

Recommended Books