Java 带条件步骤的步骤生成器

Java Step Builder with Conditional Steps

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

例如,我将使用可构建的 class 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 初始化生成器对象,但这并没有实现任何接口,因此只要调用 start 然后 build 就会破坏对象。同样调用 mobile 的可选方法允许它绕过 type.

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

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

我认为您的构建器实现非常困难且不太正确。 典型的构建器应该有私有构造函数、初始方法、字段设置器和构建方法。我会这样做:

public class MachineBuilder  {

    public String type;

    public boolean mobile;

    public String mobileType;

    private MachineBuilder (final String type, final boolean mobile, final String mobileType) {
        this.type = type;
        this.mobile = mobile;
        this.mobileType = mobileType;
    }
    
    private MachineBuilder(){
        
    }

    public MachineBuilder setType(final String source) {
        this.type = source;
        return this;
    }

    public MachineBuilder setMobile(final boolean source) {
        this.mobile = source;
        return this;
    }

    public MachineBuilder setMobileType(final String source) {
        this.mobileType = source;
        return this;
    }

    public static MachineBuilder init() {
        return new MachineBuilder();
    }

    public static MachineBuilder init(final String type, final boolean mobile, final String mobileType) {
        return new MachineBuilder(type, mobile, mobileType);
    }

    public MachineBuilder build() {  
        return Machine(this.type, this.mobile, this.mobileType);
    }
}

很高兴回答这个问题。我试着重写你的代码。只需按照 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