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

What is an Index and its Types and Differences between them ?

What is an Index and its Types and Differences between them ?


What is an Index?


Indexing is a mechanism which makes record
fetching faster. It does this by having pre-sorted the rows of the table.


Types of Indexes:




  1. Clustered.


  2. Non-Clustered.




















Sno Clustered Non - Clustered
1 It reorders the physical storage of records in the table. It sorts and maintain a separate storage.
2 There can be only one Clustered index per table. More than one.
3 The leaf nodes contain data The leaf node contains pointer to data

Win Rs 70000 by participating in DesignChef.com Contest





Win Rs 70000 by participating in DesignChef.com Contest


for more details,


www.designchef.com



Win prizes worth $3000 by participating in CodeChef's Contest





CodeChef July Contest - Win prizes worth $3000




http://www.codechef.com/JULY09



Daily Tips- Tip #3 - Automatically closing a connection after datareader finishes execution





To automatically close a connection after datareader finishes execution, you need to specify the CommandBehaviour.CloseConnection parameter to the command object, as


drEmployees = cmdEmployees.ExecuteReader(CommandBehaviour.CloseConnection);



What are the uses of Views?





1. Views are virtual tables (they dont store data physically) which gives a result
of data by joining tables. So you are not storing redundant data.


2. Views are used to produce reports from data


3. Views can be used to provide security to data by giving access only to views.



LinkWithin

Blog Widget by LinkWithin

Recommended Books