Consider a builder when faced with many constructor parameters--java bean注入属性值记录
1、telescoping constructor
在创建构造函数的时候,采取以下策略:第一个构造函数使用必选的参数,第二个增加一个可选参数,第三个增加两个可选参数,以此类推。
缺点:类拥有过多构造函数。构造函数的参数繁多,使用的时候如果相邻的两个参数的类型相同,很容易出错。
比如:缺点:类拥有过多构造函数。构造函数的参数繁多,使用的时候如果相邻的两个参数的类型相同,很容易出错。
public class User {
private String name;
private Integer age;
private String addr;
private String phone;
private String height;
private String weight;
public User(String name,Integer age){
this.name=name;
this.age=age;
}
public User(String name,Integer age,String addr,String phone,String height){
this.name=name;
this.age=age;
this.addr=addr;
this.phone=phone;
this.height=height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
private String name;
private Integer age;
private String addr;
private String phone;
private String height;
private String weight;
public User(String name,Integer age){
this.name=name;
this.age=age;
}
public User(String name,Integer age,String addr,String phone,String height){
this.name=name;
this.age=age;
this.addr=addr;
this.phone=phone;
this.height=height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
User user = new User("张飒",22,"重庆","13452678181","160");
2、JavaBeans Pattern
构造参数不使用参数或者只使用必选参数。其他的参数通过set方法设置。
缺点:对象可能还没初始化完就被其他线程使用了。有不一致的风险。
public class User {
private String name;
private Integer age;
private String addr;
private String phone;
private String height;
private String weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
private String name;
private Integer age;
private String addr;
private String phone;
private String height;
private String weight;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
}
User user = new User();
user.setName("张瑟");
user.setPhone("13453892119");
user.setHeight("160");
user.setName("张瑟");
user.setPhone("13453892119");
user.setHeight("160");
3、The Builder pattern
public class User {
private String name;
private Integer age;
private String addr;
private String phone;
private String height;
private String weight;
static class Builder{
private String name;
private Integer age;
private String addr;
private String phone;
private String height;
private String weight;
public Builder(String name,Integer age){
this.name=name;
this.age=age;
}
public Builder addr(String addr){
this.addr=addr;
return this;
}
public Builder phone(String phone){
this.phone=phone;
return this;
}
public Builder height(String height){
this.height=height;
return this;
}
public Builder weight(String weight){
this.weight=weight;
return this;
}
public User build(){
return new User(this);
}
}
private User(Builder builder){
this.name=builder.name;
this.age=builder.age;
this.addr=builder.addr;
this.phone=builder.phone;
this.height=builder.height;
this.weight=builder.weight;
}
}
每个对象附带一个Builder类。先将参数设置到Builder类对象中,最后调用build()方法构建目标对象。
缺点:需要新增一个Builder类。如果构造函数的参数不多,用Builder Pattern略显繁琐。
比如:缺点:需要新增一个Builder类。如果构造函数的参数不多,用Builder Pattern略显繁琐。
public class User {
private String name;
private Integer age;
private String addr;
private String phone;
private String height;
private String weight;
static class Builder{
private String name;
private Integer age;
private String addr;
private String phone;
private String height;
private String weight;
public Builder(String name,Integer age){
this.name=name;
this.age=age;
}
public Builder addr(String addr){
this.addr=addr;
return this;
}
public Builder phone(String phone){
this.phone=phone;
return this;
}
public Builder height(String height){
this.height=height;
return this;
}
public Builder weight(String weight){
this.weight=weight;
return this;
}
public User build(){
return new User(this);
}
}
private User(Builder builder){
this.name=builder.name;
this.age=builder.age;
this.addr=builder.addr;
this.phone=builder.phone;
this.height=builder.height;
this.weight=builder.weight;
}
}
public static void main(String[] args){
User user =new User.Builder("张瑟", 19).addr("重庆").height("160").build();
}
User user =new User.Builder("张瑟", 19).addr("重庆").height("160").build();
}