Assume I have 2 classes
public class NormalSedan : ICar
{
public NormalSedan()
{
}
}
public class PremiumSedan : ICar
{
PremiumSedan(SpoilerColor color)
{
}
}
public enum CarType
{
NORMALSEDAN,
PREMIUMSEDAN
}
public static class CarFactory
{
public static ICar Create(CarType type, SpoilerColor color)
{
if(type == NORMALSEDAN)
{
return new NormalSedan();
}
else if(type == PREMIUMSEDAN)
{
return new PremiumSedan(color);
}
}
}
So the above representation is an example where the implementation classes might take different arguments based on their type. So what is the better way of designing the Factory? Or for factory arguments should I put a separate class to factory method which can be used based on the purpose like,
public class FactoryArgs
{
CarType type;
SpoilerColor color;
....
..
etc
}
and
public static class CarFactory
{
public static ICar Create(FactoryArgs arg)
{
if(arg.type == NORMALSEDAN)
{
return new NormalSedan();
}
else if(arg.type == PREMIUMSEDAN)
{
return new PremiumSedan(arg.color);
}
}
}
Aucun commentaire:
Enregistrer un commentaire