所有这些方法签名都对简单工厂模式有效吗?
Are all of these method signatures valid for simple factory pattern?
我正在学习简单工厂模式,我想知道我工厂中的所有方法是否都适用于这种模式:
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 中的哪些方法符合简单工厂模式?
亲切的问候,
"Are they valid" 是的。
"Are they optimal"没有
您没有重载方法,我认为这将有助于解决您的问题。
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.
并且 "does this combo of options exist" 的所有逻辑都在工厂逻辑中。
也可以让 'Color' 成为 'Option'。
注意:这样做的好处是,工厂的用户可以只列出他们想要的选项,要么得到汽车,要么例外。使用起来更简单,但这意味着逻辑必须存在于工厂中(我认为它应该存在于那里)而不是依赖于用户必须编写额外的逻辑才能进入工厂。
我会创建一个 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
class:
public class BMWCarOptions extends CarOptions {
private boolean hasXDrive;
public boolean getHasXDrive() {
return this.hasXDrive;
}
public void setHasXDrive(boolean hasXDrive) {
this.hasXDrive = hasXDrive;
}
}
您仍然可以将其传递给 makeCar
方法。
这完全取决于您的工厂将用于什么。
1) 如果您要将此工厂传递给某个通用 instantiator,那么您的工厂必须实现该实例化器使用的所有工厂通用的一些接口。此接口可能只有带有通用选项的单一方法:
public interface CarFactory {
Car makeCar(CarOptions options);
}
2) 如果您打算从代码的不同部分调用您的工厂 "manually",那么我会说您的方法是正确的。 makeAudi("red", true)
看起来比 makeCar(new CarOptions(CarType.AUDI, "red", ...))
更具可读性。
我正在学习简单工厂模式,我想知道我工厂中的所有方法是否都适用于这种模式:
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 中的哪些方法符合简单工厂模式?
亲切的问候,
"Are they valid" 是的。
"Are they optimal"没有
您没有重载方法,我认为这将有助于解决您的问题。
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.
并且 "does this combo of options exist" 的所有逻辑都在工厂逻辑中。
也可以让 'Color' 成为 'Option'。
注意:这样做的好处是,工厂的用户可以只列出他们想要的选项,要么得到汽车,要么例外。使用起来更简单,但这意味着逻辑必须存在于工厂中(我认为它应该存在于那里)而不是依赖于用户必须编写额外的逻辑才能进入工厂。
我会创建一个 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
class:
public class BMWCarOptions extends CarOptions {
private boolean hasXDrive;
public boolean getHasXDrive() {
return this.hasXDrive;
}
public void setHasXDrive(boolean hasXDrive) {
this.hasXDrive = hasXDrive;
}
}
您仍然可以将其传递给 makeCar
方法。
这完全取决于您的工厂将用于什么。
1) 如果您要将此工厂传递给某个通用 instantiator,那么您的工厂必须实现该实例化器使用的所有工厂通用的一些接口。此接口可能只有带有通用选项的单一方法:
public interface CarFactory {
Car makeCar(CarOptions options);
}
2) 如果您打算从代码的不同部分调用您的工厂 "manually",那么我会说您的方法是正确的。 makeAudi("red", true)
看起来比 makeCar(new CarOptions(CarType.AUDI, "red", ...))
更具可读性。