gpt4 book ai didi

具有条件步骤的 Java 步骤生成器

转载 作者:行者123 更新时间:2023-12-02 02:22:31 25 4
gpt4 key购买 nike

我正忙于将 Step Builder 实现到 Java 应用程序中,并且编写了一些可怕的代码。我很确定我错过了一个必要的步骤。

例如,我将使用可构建类Machine.java

public class Machine {

private String type;;
private boolean mobile;
private final String mobileType;

public Machine(MachineBuilder builder) {
this.type = builder.type;
this.mobile = builder.mobile;
this.mobileType = builder.mobileType;
}

public String getType() { return this.type; }
public boolean getMobile() { return this.mobile; }
public String getMobileType() { return this.mobileType; }

}

其步骤构建器为 MachineBuilder.java

public class MachineBuilder {

public String type;
public boolean mobile;
public String mobileType;

public MachineBuilder() { }

// initialize builder
public Builder start() {
return new Builder();
}

// interfaces
public interface iType {
iBuild withType(String type);
}
public interface iMobileType {
iBuild withMobileType(String mobileType);
}
public interface iBuild {
iMobileType withMobile();
iBuild withMobileType(String mobileType);
Machine build();
}

// subclass to return
public static class Builder extends MachineBuilder implements iType, iMobileType, iBuild {

public iBuild withType(String type) {
super.type = type; return this;
}
public iMobileType withMobile() {
super.mobile = true; return this;
}
public iBuild withMobileType(String mobileType) {
super.mobileType = mobileType; return this;
}
public Machine build() {
return new Machine(this);
}

}

}

目的是将 type 作为必需步骤,然后将 mobile 作为可选步骤,但如果使用 mobile,则将 mobileType 也必须使用。

虽然只成功了一半

    // fine
Machine car = new MachineBuilder()
.start().withType("car").withMobile().withMobileType("driving").build();
System.out.println(car.getType() + ":" + car.getMobile() + ":" + car.getMobileType());

// fine
Machine washingMachine = new MachineBuilder()
.start().withType("washingMachine").build();
System.out.println(washingMachine.getType() + ":" + washingMachine.getMobile() + ":" + washingMachine.getMobileType());

// corrupt (no type)
Machine boat = new MachineBuilder()
.start().withMobile().withMobileType("sailing").build();
System.out.println(boat.getType() + ":" + boat.getMobile() + ":" + boat.getMobileType());

// corrupt (no anything)
Machine bicycle = new MachineBuilder()
.start().build();
System.out.println(bicycle.getType() + ":" + bicycle.getMobile() + ":" + bicycle.getMobileType());

我必须使用方法 start 初始化构建器对象,但这没有实现任何接口(interface),因此只需调用 start 然后调用 build会损坏对象。同样,调用 mobile 的可选方法可以使其绕过 type

是否可以在不使用 start 方法的情况下从一开始就强制流向?我觉得我错过了一些非常愚蠢的东西。

PS。抱歉,我在问题中加入了这么多代码,我只是想尽可能地说明问题

最佳答案

很高兴回答这个问题。我尝试重写你的代码。只需按照 Step Builder Pattern 的策略重新组织它即可。

此处不添加任何说明,希望您能够轻松理解代码。

class Machine {
private String type;
private boolean isMobile;
private String mobileType;

public static TypeStep builder(){
return new MachineBuilder();
}

public interface TypeStep{
IsMobileStep withType(String type);
}

public interface IsMobileStep{
MobileTypeStep withMobile(boolean isMobile);

}

public interface MobileTypeStep{
Build withMobileType(String mobileType);
}

public interface Build{
Machine build();
}


public static class MachineBuilder implements TypeStep, IsMobileStep, MobileTypeStep, Build {
private String type;
private boolean isMobile;
private String mobileType;

@Override
public IsMobileStep withType(String type) {
this.type = type;
return this;
}

@Override
public MobileTypeStep withMobile(boolean isMobile) {
this.isMobile = isMobile;
return this;
}

@Override
public Build withMobileType(String mobileType) {
this.mobileType = mobileType;
return this;
}

@Override
public Machine build() {
return new Machine(this);
}

}

private Machine(MachineBuilder machineBuilder) {
this.type = machineBuilder.type;
this.isMobile = machineBuilder.isMobile;
this.mobileType = machineBuilder.mobileType;
}

public String getType() {
return type;
}

public boolean isMobile() {
return isMobile;
}

public String getMobileType() {
return mobileType;
}
}

测试运行:

public class Main{
public static void main(String[] args) {
Machine car = Machine.builder().withType("car").withMobile(true).withMobileType("driving").build();

System.out.println("Model 1:"+ car.getType() +":"+ car.isMobile()+":"+car.getMobileType());

Machine boat = Machine.builder().withType("boat").withMobile(true).withMobileType("driving").build();

System.out.println("Model 2:"+ boat.getType() +":"+ boat.isMobile()+":"+boat.getMobileType());
}
}

输出:

Model 1:car:true:driving
Model 2:boat:true:driving

为了更好的可读性:Github Repo Step Builder

关于具有条件步骤的 Java 步骤生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66199013/

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