gpt4 book ai didi

java - 所有这些方法签名对于简单工厂模式都有效吗?

转载 作者:行者123 更新时间:2023-11-30 02:41:15 25 4
gpt4 key购买 nike

我正在学习简单工厂模式,我想知道我的工厂中的所有方法是否都对这种模式有效:

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 中的哪些方法符合简单工厂模式?

亲切的问候,

最佳答案

我会创建一个 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 方法中。

关于java - 所有这些方法签名对于简单工厂模式都有效吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41637621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com