1.创建对象的模式1
public class LunchModelOne {
private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
public LunchModelOne(){
this(null);
}
public LunchModelOne(String s1){
this(s1,null);
}
public LunchModelOne(String s1,String s2){
this(s1,s2,null);
}
public LunchModelOne(String s1,String s2,String s3){
this(s1,s2,s3,null);
}
public LunchModelOne(String s1,String s2,String s3,String s4){
this(s1,s2,s3,s4,null);
}
public LunchModelOne(String s1,String s2,String s3,String s4,String s5){
this(s1,s2,s3,s4,s5,null);
}
public LunchModelOne(String s1,String s2,String s3,String s4,String s5,String s6){
this.s1=s1;
this.s2=s2;
this.s3=s3;
this.s4=s4;
this.s5=s5;
this.s6=s6;
}
}
2.创建对象的模式2
public class LunchModelTwo {
private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
public LunchModelTwo(){}
public LunchModelTwo setS1(String s1){
this.s1=s1;
return this;
}
public LunchModelTwo setS2(String s2){
this.s2=s2;
return this;
}
public LunchModelTwo setS3(String s3){
this.s3=s3;
return this;
}
public LunchModelTwo setS4(String s4){
this.s4=s4;
return this;
}
public LunchModelTwo setS5(String s5){
this.s5=s5;
return this;
}
public LunchModelTwo setS6(String s6){
this.s6=s6;
return this;
}
}
3.创建对象的模式3
public class LunchModelThree {
private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
public static class Builder{
private String s1;
private String s2;
private String s3;
private String s4;
private String s5;
private String s6;
public Builder(){}
public Builder setS1(String s1){
this.s1=s1;
return this;
}
public Builder setS2(String s2){
this.s2=s2;
return this;
}
public Builder setS3(String s3){
this.s3=s3;
return this;
}
public Builder setS4(String s4){
this.s4=s4;
return this;
}
public Builder setS5(String s5){
this.s5=s5;
return this;
}
public Builder setS6(String s6){
this.s6=s6;
return this;
}
public LunchModelThree create(){
return new LunchModelThree(this);
}
}
private LunchModelThree(Builder builder){
this.s1=builder.s1;
this.s2=builder.s2;
this.s3=builder.s3;
this.s4=builder.s4;
this.s5=builder.s5;
this.s6=builder.s6;
}
public String toString(){
return new StringBuffer().append("s1="+s1).append(";s2="+s2)
.append(";s3="+s3).append(";s4="+s4).append(";s5="+s5).append(";s6="+s6).toString();
}
}
测试:
public class LunchModelThreeTest {
public static void main(String[]args){
LunchModelThree.Builder builder = new LunchModelThree.Builder();
LunchModelThree lunch = builder.setS1("s1").setS2("s2").setS3("s3").create();
System.out.println(lunch.toString());
}
}
谁能给个意见说一下以上三种模式的好坏?