我有汽车厂生产不同尺寸的汽车。我有 2 家工厂:美国和泰国,生产大、中、小汽车尺寸。但我有一个问题:泰国工厂不生产大型汽车。
代码:
public enum CarSize { Big, Middle, Little,}
public interface ICarFactory {
ICar CreateCar(CarSize carSize);
}
public class USACarFactory : ICarFactory {
public ICar CreateCar(CarSize carSize) {
ICar car = null;
switch (carSize) {
case CarSize.Little:
car = new USALittleCar();
break;
case CarSize.Middle:
car = new USAMiddleCar();
break;
case CarSize.Big:
car = new USABigCar();
break;
}
return car;
}
}
public class ThailandCarFactory : ICarFactory {
public ICar CreateCar(CarSize carSize){
ICar car = null;
switch (carSize) {
case CarSize.Little:
car = new ThailandLittleCar();
break;
case CarSize.Middle:
car = new ThailandMiddleCar();
break;
case CarSize.Big:
//no big cars in Thailand!!!!
break;
}
return car;
}
}
public interface ICar { }
public class USABigCar : ICar { }
public class USAMiddleCar : ICar { }
public class USALittleCar : ICar { }
public class ThailandMiddleCar : ICar { }
public class ThailandLittleCar : ICar { }
我应该怎么办?我的抽象工厂设计得很糟糕吗?