重叠构造器模式,javaBean模式,Builder模式的优缺点
模式 | 优点 | 缺点 |
---|
重叠构造器模式 | 形式简单 | 容易出错,参数表太长不容易控制,而且难以阅读 |
javaBean模式 | 容易阅读,创建实例比较容易 | 构造过程被分割到好多个set中容易造成线程不安全,导致对象处于不一致的状态 |
Builder模式 | 代码容易编写 | 创建对象,必须先创造构建器,造成一定的性能上的开销 |
示例代码:
class MyClass1{
private final int one;
private final int two;
private final int three;
private final int four;
private final int five;
private final int six;
public MyClass1(int one,int two){
this(one,two,0);
}
public MyClass1(int one,int two,int three){
this(one,two,three,0);
}
public MyClass1(int one,int two,int three,int four){
this(one,two,three,four,0);
}
public MyClass1(int one,int two,int three,int four,int five){
this(one,two,three,four,five,0);
}
public MyClass1(int one,int two,int three,int four,int five,int six){
this.one=one;
this.two=two;
this.three=three;
this.four=four;
this.five=five;
this.six=six;
}
@Override
public String toString(){
return " one="+this.one+" two="+this.two+" three="+this.three+" four="+this.four+" five="+this.five+" six="+this.six;
}
}
class MyClass2{
private int one=0;
private int two=0;
private int three=0;
private int four=0;
private int five=0;
private int six=0;
public MyClass2(){}
public void setOne(int one){this.one=one;}
public void setTwo(int two){this.two=two;}
public void setThree(int three){this.three=three;}
public void setFour(int four){this.four=four;}
public void setFive(int five){this.five=five;}
public void setSix(int six){this.six=six;}
@Override
public String toString(){
return " one="+this.one+" two="+this.two+" three="+this.three+" four="+this.four+" five="+this.five+" six="+this.six;
}
}
class MyClass3{
private final int one;
private final int two;
private final int three;
private final int four;
private final int five;
private final int six;
public static class Builder{
private final int one;
private final int two;
private int three=0;
private int four=0;
private int five=0;
private int six=0;
public Builder(int one,int two){
this.one=one;
this.two=two;
}
public Builder Three(int three){
this.three=three;
return this;
}
public Builder Four(int four){
this.four=four;
return this;
}
public Builder Five(int five){
this.five=five;
return this;
}
public Builder Six(int six){
this.six=six;
return this;
}
public MyClass3 build(){
return new MyClass3(this);
}
}
private MyClass3(Builder builder){
this.one=builder.one;
this.two=builder.two;
this.three=builder.three;
this.four=builder.four;
this.five=builder.five;
this.six=builder.six;
}
@Override
public String toString(){
return " one="+this.one+" two="+this.two+" three="+this.three+" four="+this.four+" five="+this.five+" six="+this.six;
}
}
public class MyBuilderMain{
public static void main(String[]args){
MyClass1 myclass1=new MyClass1(1,2,3,4,5,6);
System.out.println(myclass1.toString());
MyClass2 myclass2=new MyClass2();
myclass2.setOne(1);
myclass2.setTwo(2);
myclass2.setThree(3);
myclass2.setFour(4);
myclass2.setFive(5);
myclass2.setSix(6);
System.out.println(myclass2.toString());
MyClass3 myclass3=new MyClass3.Builder(1,2).Three(3).Four(4).Five(5).Six(6).build();
System.out.println(myclass3.toString());
}
}
输出结果: