`
zhangwei_david
  • 浏览: 470781 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

使用Builder模式代替构造器

    博客分类:
  • Java
 
阅读更多

package demo.bigDecimal;

public class Model {
 private String name;
 private String familyName;
 private String country;
 private String city;

 public static class Builder {
  private String name;
  private String familyName;
  private String country;
  private String city;

  public Builder name(String name) {
   this.name = name;
   return this;
  }

  public Builder familyName(String familyName) {
   this.familyName = familyName;
   return this;
  }

  public Builder country(String country) {
   this.country = country;
   return this;
  }

  public Builder city(String city) {

   this.city = city;
   return this;
  }

  public Model build() {
   Model model = new Model();
   model.city = this.city;
   model.country = this.country;
   model.familyName = this.familyName;
   model.name = this.name;
   return model;
  }
 }

 public String getName() {
  return name;
 }

 public String getFamilyName() {
  return familyName;
 }

 public String getCountry() {
  return country;
 }

 public String getCity() {
  return city;
 }
 

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics