首先我们举一个例子简单一点的例子,该类中有2个属性,2个必要属性,2个可选择的属性
class A214{ //必要属性 private String import1; private String import2; private String optional1; private String optional2; }
- 采用这一种方案:重叠构造器
class A214{ //必要属性 private String import1; private String import2;
//可选属性 private String optional1; private int optional2; //两个必要属性的构造器 public A214(String import1, String import2) { super(); this.import1 = import1; this.import2 = import2; } //必要属性+1个可选属性 public A214(String import1, String import2, String optional1) { super(); this.import1 = import1; this.import2 = import2; this.optional1 = optional1; } //必要属性+1个可选属性 public A214(String import1, String import2, int optional2) { super(); this.import1 = import1; this.import2 = import2; this.optional2 = optional2; } //必要属性+2个可选属性 public A214(String import1, String import2, String optional1, int optional2) { super(); this.import1 = import1; this.import2 = import2; this.optional1 = optional1; this.optional2 = optional2; } }这种方法看似很周全。但目前的参数只有4个,要是8个,10个呢??
- 采用第二种方法:javabean模式:setter and getter
@Test public void test14() { A214 a=new A214(); a.setImport1("wang"); a.setImport2("yang"); a.setOptional1(""); a.setOptional2(0); } class A214{ //必要属性 private String import1; private String import2;
//可选属性 private String optional1; private int optional2; public String getImport1() { return import1; } public void setImport1(String import1) { this.import1 = import1; } public String getImport2() { return import2; } public void setImport2(String import2) { this.import2 = import2; } public String getOptional1() { return optional1; } public void setOptional1(String optional1) { this.optional1 = optional1; } public int getOptional2() { return optional2; } public void setOptional2(int optional2) { this.optional2 = optional2; } }这种方案创建对象确实很容易,代码也容易阅读
但是:javabean自身是有缺陷的。由于在不同处构造过程中,可能会导致javabean处于不一致的状态。类无法通过构造器的验证性来保持一致性,而且这种模式阻止了把类变成不可变!(当然也是可以的,只不过花费更多的代码去实现) - 构建器
不直接生成对象,先获取构建器builder,在builder上设置可选的参数
@Test public void test14() { A214 a=new A214.A214_Builder("wang", "yang").builder(); A214 b=new A214.A214_Builder("wang", "yang").optional1("optional1").builder(); A214 c=new A214.A214_Builder("wang", "yang").optional1("optional1").optional2(122).builder(); } class A214{ //必要属性 private String import1; private String import2; //可选属性 private String optional1; private int optional2; public static class A214_Builder{ //必要属性 private String import1; private String import2; //可选属性 private String optional1=""; private int optional2=0; public A214_Builder(String import1, String import2) { super(); this.import1 = import1; this.import2 = import2; } public A214_Builder optional1(String optional1){ this.optional1=optional1; return this; } public A214_Builder optional2(int optional2){ this.optional2=optional2; return this; } public A214 builder(){ return new A214(this); } } private A214(A214_Builder builder){ this.import1=builder.import1; this.import2=builder.import2; this.optional1=builder.optional1; this.optional2=builder.optional2; } }
使用这种方法,前面所提出的问题已经解决了。但是这样做也有问题,一个是构建器的构建,代码量增加;二个,构建器的构建对性能有一些影响。