I have code as shown below
public interface ICar
{
void Created();
}
public class BigCar : ICar
{
public void Created()
{
}
}
public class SmallCar : ICar
{
public void Created()
{
}
}
public class LuxaryCar : ICar
{
public void Created()
{
}
}
public class CarFactory
{
public ICar CreateCar(int carType)
{
switch (carType)
{
case 0:
return new BigCar();
case 1:
return new SmallCar();
case 2:
return new LuxaryCar();
default:
break;
}
return null;
}
}
In this code I have a factory which is returning the concrete instances. But every time I need to have a new implementation of the ICar interface, I have to change the CreateCar() method of the CarFactory. It seems like I am not supporting the Open Closed Principle of the SOLID principles. Please suggest is there a better way to handle this scenario.
Aucun commentaire:
Enregistrer un commentaire