I am learning design patterns and I have implemented the abstract factory using generics C#.NET. Is this implementation correct or not? Or is there any SOLID principle violation?
using System;
namespace GenericAbstractFactory
{
public enum vehicles { car, bicycle, truck }
interface IFourWheeler
{
void DriveFourWheeler();
}
interface ITwoWheeler
{
void DriveTwoWheeler();
}
interface IVehcileAbstractFactory<VehicleTypeFactory> //Abstract Factory
{
VehicleTypeFactory GetVehicleType();
}
class FourWheeler<VehicleType> : IVehcileAbstractFactory<IFourWheeler>
where VehicleType : IFourWheeler, new() // ConcreteFactory
{
public IFourWheeler GetVehicleType()
{
return new VehicleType();
}
}
class TwoWheeler<VehicleType> : IVehcileAbstractFactory<ITwoWheeler>
where VehicleType : ITwoWheeler, new() // ConcreteFactory
{
public ITwoWheeler GetVehicleType()
{
return new VehicleType();
}
}
class Car : IFourWheeler
{
public void DriveFourWheeler()
{
Console.WriteLine("Drive Car");
}
}
class Truck : IFourWheeler
{
public void DriveFourWheeler()
{
Console.WriteLine("Drive Truck");
}
}
class Bicycle : ITwoWheeler
{
public void DriveTwoWheeler()
{
Console.WriteLine("Drive Bicycle");
}
}
class Program
{
static void Main(string[] args)
{
vehicles veh = vehicles.bicycle;
IVehcileAbstractFactory<IFourWheeler> getFourWheeler = null;
IVehcileAbstractFactory<ITwoWheeler> getTwoWheeler = null;
IFourWheeler actionFourWheeler = null;
ITwoWheeler actionTwoWheeler = null;
if (veh == vehicles.car)
{
getFourWheeler = new FourWheeler<Car>();
actionFourWheeler = getFourWheeler.GetVehicleType();
}
else if (veh == vehicles.bicycle)
{
getTwoWheeler = new TwoWheeler<Bicycle>();
actionTwoWheeler = getTwoWheeler.GetVehicleType();
}
else if (veh == vehicles.truck)
{
getFourWheeler = new FourWheeler<Truck>();
actionFourWheeler = getFourWheeler.GetVehicleType();
}
else
{
throw new Exception("vehicle type not supported");
}
if (actionFourWheeler != null)
{
actionFourWheeler.DriveFourWheeler();
}
else if (actionTwoWheeler != null)
{
actionTwoWheeler.DriveTwoWheeler();
}
Console.Read();
}
}
}