我正在学习简单的工厂模式,我想知道工厂中的所有方法是否都适用于这种模式:
public class Bmw implements Car {
private String color;
private boolean hasXDrive;
public Bmw() {
}
public Bmw(String color) {
this.color = color;
}
public Bmw(String color, boolean hasXDrive) {
this.color = color;
this.hasXDrive = hasXDrive;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public boolean isHasXDrive() {
return hasXDrive;
}
public void setHasXDrive(boolean hasXDrive) {
this.hasXDrive = hasXDrive;
}
}
public class Audi implements Car {
private String color;
private int turnAssistLevel;
public Audi() {
}
public Audi(String color) {
this.color = color;
}
public Audi(String color, int turnAssistLevel) {
this.color = color;
this.turnAssistLevel = turnAssistLevel;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getTurnAssistLevel() {
return turnAssistLevel;
}
public void setTurnAssistLevel(int turnAssistLevel) {
this.turnAssistLevel = turnAssistLevel;
}
}
public class SimpleCarFactory {
// 1. make empty cars
public Car makeCar(CarType carType) {
switch (carType) {
case AUDI:
return new Audi();
case BMW:
return new Bmw();
default:
throw new RuntimeException("No such car type!");
}
}
// 2. make cars with colors
public Car makeCarWithColor(CarType carType, String color) {
switch (carType) {
case AUDI:
return new Audi(color);
case BMW:
return new Bmw(color);
default:
throw new RuntimeException("No such car type!");
}
}
// 3. BMW has an option that differentiate it from any other car. We cannot use a general car factory anymore
public Car makeBmw(String color, boolean hasXDrive) {
return new Bmw(color, hasXDrive);
}
// 4. Audi has a turnAssistLevel option
public Car makeAudi(String color, int turnAssistLevel) {
return new Audi(color, turnAssistLevel);
}
// 5. The same as #1, only it is static now make empty cars
public static Car staticMakeCar(CarType carType) {
switch (carType) {
case AUDI:
return new Audi();
case BMW:
return new Bmw();
default:
throw new RuntimeException("No such car type!");
}
}
}我在代码中添加了方法的变体。我之所以问这些问题,是因为通常情况下,您会创建一个基于某些判别器(CarType)的子类。但是,您也可以具有构造函数参数。
另外,当相关对象有不同的构造函数时,我也不知道该怎么办。
请告诉我SimpleCarFactory的哪一种方法符合简单的工厂模式?
致以亲切的问候,
发布于 2017-01-13 15:48:04
这完全取决于你的工厂将用来做什么。
1)如果要将该工厂传递给某个泛型实例器,那么您的工厂必须实现与该实例器使用的所有工厂相同的接口。这个接口可能有一个带有泛型选项的方法:
public interface CarFactory {
Car makeCar(CarOptions options);
}2)如果要从代码的不同部分“手动”调用工厂,那么我会说您的方法是正确的。makeAudi("red", true)看起来比makeCar(new CarOptions(CarType.AUDI, "red", ...))可读性强得多。
发布于 2017-01-13 15:33:18
我会创建一个CarOptions对象并使用它,而不是拥有这么多不同的方法。
public class CarOptions {
private String color;
private CarType carType;
public String getColor() {
return this.color;
}
public void setColor(String color) {
this.color = color;
}
public CarType getCarType() {
return this.carType;
}
public void setCarType(CarType carType) {
this.carType = carType;
}
}然后是一个简单的makeCar方法,它接受CarOptions对象。
public Car makeCar(CarOptions options) {
switch (options.getCarType()) {
case AUDI:
return new Audi(options.getColor());
case BMW:
return new Bmw(options.getColor());
default:
throw new RuntimeException("No such car type!");
}
}这样做的好处是您可以创建一个BMWCarOptions类:
public class BMWCarOptions extends CarOptions {
private boolean hasXDrive;
public boolean getHasXDrive() {
return this.hasXDrive;
}
public void setHasXDrive(boolean hasXDrive) {
this.hasXDrive = hasXDrive;
}
}然后仍然可以将它传递到makeCar方法中。
发布于 2017-01-13 15:24:57
“他们有效吗”是的。
“他们是最优的”不。
您没有重载这些方法,我认为这有助于解决您的问题。
public Car makeCar(CarType carType)
throws NoCarExistsException
public Car makeCar(CarType carType, Color color)
throws NoCarExistsException
public Car makeCar(CarType carType, Color color, Options options)
throws NoCarExistsException
// where Options is a container class with hashmap
// (or one of many other valid impl. possibilities) with all the options. 而只有所有的逻辑“这组合的选项存在”是在工厂的逻辑。
也可以有‘颜色’也是一个‘选项’。
注意:这样做的好处是,工厂的用户只需列出他们想要的选项,就可以得到汽车或异常。使用起来要简单得多,但这意味着逻辑必须存在于工厂(我认为应该存在的地方),而不是依赖用户编写额外的逻辑才能到达工厂。
https://stackoverflow.com/questions/41637621
复制相似问题