标签:
假如我们的一个实体类有很多的属性值,但是这些属性值又是可选的。如果我们遇到这样的是类,如何设计出方便的实体类呢?
通常解决办法一: 重叠构造器
public class User { private String id; // id(必填) private String name; // 用户名(必填) private String email; // 邮箱(可选) private int age; // 年龄(可选) private String phoneNumber; // 电话(可选) private String address; // 地址(可选) public User(String id, String name) { this(id, name, null, 0, null, null); } public User(String id, String name, String email) { this(id, name, email, 0, null, null); } public User(String id, String name, String email, int age) { this(id, name, email, age, null, null); } public User(String id, String name, String email, int age, String phoneNumber) { this(id, name, email, age, phoneNumber, null); } public User(String id, String name, String email, int age, String phoneNumber, String address) { super(); this.id = id; this.name = name; this.email = email; this.age = age; this.phoneNumber = phoneNumber; this.address = address; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", email=" + email + ", age=" + age + ", phoneNumber=" + phoneNumber + ", address=" + address + "]"; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
注:重叠构造器模式可行,但是当有很多参数的时候,客户端代码会很难编写,并且难以阅读
通常解决办法二:JavaBean模式
public class User { private String id; // id(必填) private String name; // 用户名(必填) private String email; // 邮箱(可选) private int age; // 年龄(可选) private String phoneNumber; // 电话(可选) private String address; // 地址(可选) public User(String id, String name, String email, int age, String phoneNumber, String address) { super(); this.id = id; this.name = name; this.email = email; this.age = age; this.phoneNumber = phoneNumber; this.address = address; } public User() { super(); } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", email=" + email + ", age=" + age + ", phoneNumber=" + phoneNumber + ", address=" + address + "]"; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
注:JavaBean存在很多缺点,构造过程被分到几个中调用,在多线程很难保证线程安全。
构建器解决
public class NutritionFacts { private int servingSize; private int servings; private int calorizs; private int fat; private int soduim; private int carbophydrate; public static class Builder { private int servingSize; private int servings; private int calorizs = 0; private int fat = 0; private int soduim = 0; private int carbophydrate = 0; public Builder(int servingSize, int servings) { super(); this.servingSize = servingSize; this.servings = servings; } public Builder colorizes(int val) { calorizs = val; return this; } public Builder fat(int val) { fat = val; return this; } public Builder soduim(int val) { soduim = val; return this; } public Builder carbophydrate(int val) { carbophydrate = val; return this; } public NutritionFacts build() { return new NutritionFacts(this); } } private NutritionFacts(Builder builder) { servingSize = builder.servingSize; servings = builder.servings; calorizs = builder.calorizs; fat = builder.fat; soduim = builder.soduim; carbophydrate = builder.carbophydrate; } public int getServingSize() { return servingSize; } public int getServings() { return servings; } public int getCalorizs() { return calorizs; } public int getFat() { return fat; } public int getSoduim() { return soduim; } public int getCarbophydrate() { return carbophydrate; } @Override public String toString() { return "NutritionFacts [servingSize=" + servingSize + ", servings=" + servings + ", calorizs=" + calorizs + ", fat=" + fat + ", soduim=" + soduim + ", carbophydrate=" + carbophydrate + "]"; } }
测试代码
public static void main( String[] args ) { User user = new User.Builder(UUID.randomUUID().toString(), "parry").address("广州").builder(); System.out.println(user.toString()); }
标签:
原文地址:http://www.cnblogs.com/parryyang/p/5696916.html