Factory Pattern Examples

Posted by Sksamantaray under ASP.NET on 12/31/2011 | Points: 10 | Views : 5312 | Status : [Member] | Replies : 4
Hello ,
I need a sample code for Factory Pattern.

Thanks,
Sanjay



Responses

Posted by: Naraayanan on: 1/1/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
It may be useful .Please check this link
http://www.codeguru.com/csharp/.net/net_general/patterns/article.php/c4673

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/

Sksamantaray, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Arefin on: 1/1/2012 [Member] Starter | Points: 25

Up
0
Down
You can see the below link
http://www.dofactory.com/Patterns/PatternFactory.aspx

Thanks,
Arefin

Sksamantaray, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Anishsethu on: 4/3/2012 [Member] Starter | Points: 25

Up
0
Down
C# example of the Factory Pattern

// A Simple Interface

public interface IVehicle

{

void Drive(int miles);

}



// The Vehicle Factory

public class VehicleFactory

{

public static IVehicle getVehicle(string Vehicle)

{

switch (Vehicle) {

case "Car":

return new Car();

case "Lorry":

return new Lorry();

default:

throw new ApplicationException(string.Format("Vehicle '{0}' cannot be created", Vehicle));

break;

}

}

}



// A Car Class that Implements the IVehicle Interface

public class Car : IVehicle

{

public void IVehicle.Drive(int miles)

{

// Drive the Car

}

}



// A Lorry Class that Implements the IVehicle Interface

public class Lorry : IVehicle

{

public void IVehicle.Drive(int miles)

{

// Drive the Lorry

}

}



Sksamantaray, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Puneet20884 on: 6/12/2014 [Member] Bronze | Points: 25

Up
0
Down
thanks !!

Best Regards,
Puneet Sharma - Infosys
Pune, India

Sksamantaray, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response