如何设计具有双构造函数和数组参数的Builder模式?

时间:2017-08-06 22:18:35

标签: java arrays netbeans builder

我想设计一个带有2个构造函数的构建器模式。其中一个参数必须是强制的,可以是字符串或数组字符串。

以下所有代码均用于测试。它没有做任何有用的事情。

第一块代码调用两个类。对第一个类的调用表明构建器模式可以有2个不同的构造函数。在此测试示例中,第一个调用具有两个必需参数。

第二个电话只有可选参数。此代码有效,并证明构建器将与2个构造函数一起使用。

package flag.panel.bx6;

public class TestBuilderPattern {
        private static String[] anArray = new String[20];

    public static void main(String[] args) {
    TestBuilder comp = new TestBuilder.SampleBuilder(
            "500 GB", "2 GB").setBluetoothEnabled(true)
            .setGraphicsCardEnabled(true).build();
            System.out.print("Gfx card : " + comp.isGraphicsCardEnabled() + "\n");                
    TestBuilder comp2 = new TestBuilder.SampleBuilder().setBluetoothEnabled(true)
            .setGraphicsCardEnabled(true).build();
             System.out.print("HDD : " + comp2.getHDD() + "\n");                
             System.out.print("RAM : " + comp2.getRAM() + "\n");                


            anArray[0] = "text";

    TestBuilderArray comp3 = new TestBuilderArray.SampleBuilder(
            "500 GB", "2 GB").setBluetoothEnabled(true)
            .setGraphicsCardEnabled(true).build();
            System.out.print("Gfx card : " + comp.isGraphicsCardEnabled() + "\n");  

            System.out.print("HDD : " + comp3.getHDD() + "\n");  

            TestBuilderArray comp4 = new TestBuilderArray.SampleBuilder("3TB",anArray).setBluetoothEnabled(true)
            .setGraphicsCardEnabled(true).build();

             System.out.print("RAM : " + comp4.getRAM() + "\n");                

}

}

以下是具有2个构造函数的类的代码。这段代码有效。

package flag.panel.bx6;
public class TestBuilder {

//required parameters
private final String HDD;
private final String RAM;

//optional parameters
private final boolean isGraphicsCardEnabled;
private final boolean isBluetoothEnabled;

public String getHDD() {
        return HDD;
}
public String getRAM() {
        return RAM;
}
public boolean isGraphicsCardEnabled() {
        return isGraphicsCardEnabled;
}
public boolean isBluetoothEnabled() {
        return isBluetoothEnabled;
}
private TestBuilder(SampleBuilder builder) {
        this.HDD=builder.HDD;
        this.RAM=builder.RAM;
        this.isGraphicsCardEnabled=builder.isGraphicsCardEnabled;
        this.isBluetoothEnabled=builder.isBluetoothEnabled;
}
//Builder Class
public static class SampleBuilder{
    // required parameters
    private String HDD;
    private String RAM;

    // optional parameters
    private boolean isGraphicsCardEnabled;
    private boolean isBluetoothEnabled;

    //Contructor 1
    public SampleBuilder(String hdd, String ram){
        this.HDD=hdd;
        this.RAM=ram;
    }
    //Constructor 2
    public SampleBuilder(){
        this.HDD = "HDD1";
        this.RAM = "RAM1";
    }
    public SampleBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) {
        this.isGraphicsCardEnabled = isGraphicsCardEnabled;
        return this;
    }
    public SampleBuilder setBluetoothEnabled(boolean isBluetoothEnabled) {
        this.isBluetoothEnabled = isBluetoothEnabled;
        return this;
    }
    public TestBuilder build(){
        return new TestBuilder(this);
    }
}

}

现在代码不起作用。我希望构造函数接受String或String数组作为必需参数。如果调用包含String,则该值需要存储在String数组的构造函数中。在内部,所有处理都将在String数组上完成。

package flag.panel.bx6;
public class TestBuilderArray {

    //required parameters
    private final String HDD;
    private final String RAM;

    private final String[] testArray;

    //optional parameters
    private final boolean isGraphicsCardEnabled;
    private final boolean isBluetoothEnabled;

    public String getHDD() {
            return HDD;
    }
    public String getRAM() {
            return RAM;
    }
    public boolean isGraphicsCardEnabled() {
            return isGraphicsCardEnabled;
    }
    public boolean isBluetoothEnabled() {
            return isBluetoothEnabled;
    }
    private TestBuilderArray(SampleBuilder builder) {
            this.HDD=builder.HDD;
            this.RAM=builder.RAM;
            this.testArray = builder.testArray;
            this.isGraphicsCardEnabled=builder.isGraphicsCardEnabled;
            this.isBluetoothEnabled=builder.isBluetoothEnabled;
    }
    //Builder Class
    public static class SampleBuilder{
        // required parameters
        private String HDD;
        private String RAM;

        private String[] testArray;
        // optional parameters
        private boolean isGraphicsCardEnabled;
        private boolean isBluetoothEnabled;

        //Contructor 1
        public SampleBuilder(String hdd, String ram){
            testArray = new String[10]  //##### This was the missing statement.
            this.HDD=hdd;
            this.RAM=ram;
            this.testArray[0]="ram";
        }
        //Constructor 2
        public SampleBuilder(String hdd, String[] tstArray){
            testArray = new String[10]  //##### This was the missing statement.
            this.HDD = hdd;
            this.RAM = "RAM1";
            //System.arraycopy( src, 0, dest, 0, src.length );
            //System.arraycopy( tstArray, 0, this.testArray, 0, tstArray.length );
        }
        public SampleBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) {
            this.isGraphicsCardEnabled = isGraphicsCardEnabled;
            return this;
        }
        public SampleBuilder setBluetoothEnabled(boolean isBluetoothEnabled) {
            this.isBluetoothEnabled = isBluetoothEnabled;
            return this;
        }
        public TestBuilderArray build(){
            return new TestBuilderArray(this);
        }
    }
}

当我尝试运行此代码时,Netbeans提供以下输出:

    run:
Gfx card : true
HDD : HDD1
RAM : RAM1
Exception in thread "main" java.lang.NullPointerException
    at flag.panel.bx6.TestBuilderArray$SampleBuilder.<init>(TestBuilderArray.java:61)
    at flag.panel.bx6.TestBuilderPattern.main(TestBuilderPattern.java:27)
C:\Users\darren\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 1 second)

这表示TestBuilderArray方法的第61行出错。我尝试了一系列不同的东西,但我无法弄清楚我做错了什么。我怀疑这与双构造函数无关。

我还评论了另一个导致问题的陈述:

                //System.arraycopy( tstArray, 0, this.testArray, 0, tstArray.length );

所以基本上我不能让String数组在这个类中工作。欢迎大家提出意见。

0 个答案:

没有答案