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