微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

带有条件步骤的 Java 步骤构建器

如何解决带有条件步骤的 Java 步骤构建器

我正忙于将 Step Builders 实现到 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 方法初始化构建器对象,但这并没有实现任何接口,所以只调用 start 然后 build 会破坏对象。同样,调用 mobile 的可选方法允许它绕过 type

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

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

解决方法

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

,

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

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 String mobileType) {
        return new MachineBuilder(type,mobile,mobileType);
    }

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。