学习
Java
的小伙伴们都应该听说过
Java
四大名著。今天博主将与大家分享其中一本经典著作
《Effective Java》
中的一个实用知识点 —— 如何使用设计模式中的构建者模式(
Builder Pattern
)来优化具有大量参数的类的构造方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
@Data @EqualsAndHashCode(callSuper = false) public class UserDTO { private static final long serialVersionUID = 1L; private String id; private String loginName; private String password; private String name; private int age; private String email; private String phone;
public UserDTO(String id) { this.id = id; }
public UserDTO(String loginName, String password) { this.loginName = loginName; this.password = password; }
public UserDTO(String id, String loginName, String password) { this.id = id; this.loginName = loginName; this.password = password; }
public UserDTO(String id, String loginName, String password, String email) { this.id = id; this.loginName = loginName; this.password = password; this.email = email; }
public UserDTO(String id, String loginName, String password, String email, int age) { this.id = id; this.loginName = loginName; this.password = password; this.email = email; this.age = age; }
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
public static void main(String[] args) {
UserDTO userDTO = new UserDTO("", "张三", "", "", 0, "", "1008611");
UserDTO userDTO1 = new UserDTO(); userDTO1.setLoginName("张三"); userDTO1.setPassword("1008611");
UserDTO userDTO2 = new UserDTO("1", "张三", "loginName", "zhangsan", 18, "[email protected]", "1008611");
UserDTO userDTO3 = new UserDTO(); userDTO3.setId("2"); userDTO3.setName("zhangsan"); userDTO3.setEmail("[email protected]"); userDTO3.setLoginName("password"); userDTO3.setAge(18); userDTO3.setPhone("1008611"); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
@Data @EqualsAndHashCode(callSuper = false) public class UserDTO implements Serializable {
private final static final long serialVersionUID = 1L; private final String id; private final String loginName; private final String password; private final String name; private final int age; private final String email; private final String phone;
public static class Builder {
private final String id; private String loginName; private String password; private String name; private int age; private String email; private String phone; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
|
@Data @EqualsAndHashCode(callSuper = false) public class UserDTO implements Serializable {
private final static final long serialVersionUID = 1L; private final String id; private final String loginName; private final String password; private final String name; private final int age; private final String email; private final String phone;
public static class Builder {
private final String id; private String loginName; private String password; private String name; private int age; private String email; private String phone;
public Builder(String id) { this.id = id; } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
@Data @EqualsAndHashCode(callSuper = false) public class UserDTO implements Serializable {
private static final long serialVersionUID = 1L; private final String id; private final String loginName; private final String password; private final String name; private final int age; private final String email; private final String phone;
public static class Builder {
private final String id; private String loginName; private String password; private String name; private int age; private String email; private String phone;
public Builder(String id) { this.id = id; }
public Builder loginName(String val) { loginName = val; return this; }
public Builder password(String val) { password = val; return this; }
public Builder name(String val) { name = val; return this; }
public Builder age(int val) { age = val; return this; }
public Builder email(String val) { email = val; return this; }
public Builder phone(String val) { phone = val; return this; } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
@Data @EqualsAndHashCode(callSuper = false) public class UserDTO implements Serializable {
private static final long serialVersionUID = 1L; private final String id; private final String loginName; private final String password; private final String name; private final int age; private final String email; private final String phone;
public static class Builder {
private final String id; private String loginName; private String password; private String name; private int age; private String email; private String phone;
public Builder(String id) { this.id = id; }
public Builder loginName(String val) { loginName = val; return this; }
public Builder password(String val) { password = val; return this; }
public Builder name(String val) { name = val; return this; }
public Builder age(int val) { age = val; return this; }
public Builder email(String val) { email = val; return this; }
public Builder phone(String val) { phone = val; return this; }
public UserDTO build() { return new UserDTO(this); } } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
@Data @EqualsAndHashCode(callSuper = false) public class UserDTO implements Serializable {
private static final long serialVersionUID = 1L; private final String id; private final String loginName; private final String password; private final String name; private final int age; private final String email; private final String phone;
private UserDTO(Builder builder) { this.id = builder.id; this.loginName = builder.loginName; this.password = builder.password; this.name = builder.name; this.age = builder.age; this.email = builder.email; this.phone = builder.phone; }
public static class Builder {
private final String id; private String loginName; private String password; private String name; private int age; private String email; private String phone;
public Builder(String id) { this.id = id; }
public Builder loginName(String val) { loginName = val; return this; }
public Builder password(String val) { password = val; return this; }
public Builder name(String val) { name = val; return this; }
public Builder age(int val) { age = val; return this; }
public Builder email(String val) { email = val; return this; }
public Builder phone(String val) { phone = val; return this; }
public UserDTO build() { return new UserDTO(this); }
} }
|
1 2 3 4 5 6 7 8 9 10 11 12
|
public static void main(String[] args) {
UserDTO userDTO = new UserDTO.Builder("123") .loginName("zhangsan") .name("张三") .password("password") .build(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@Data @Builder @EqualsAndHashCode(callSuper = false) public class UserDTO implements Serializable {
private static final long serialVersionUID = 1L; private final String id; private final String loginName; private final String password; private final String name; private final int age; private final String email; private final String phone; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
|
public static void main(String[] args) {
UserDTO userDTO = UserDTO.builder() .id("123") .loginName("zhangsan") .name("张三") .password("password") .build(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@Data @Builder(toBuilder = true) @EqualsAndHashCode(callSuper = false) public class UserDTO implements Serializable {
private static final long serialVersionUID = 1L; private final String id; private final String loginName; private final String password; private final String name; private final int age; private final String email; private final String phone; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
public static void main(String[] args) {
UserDTO userDTO = UserDTO.builder() .id("123") .loginName("zhangsan") .name("张三") .password("password") .build(); UserDTO newUserDTO = userDTO.toBuilder().name("李四盗号了").build(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
@Data @Builder(toBuilder = true, builderMethodName = "myBuilder") @EqualsAndHashCode(callSuper = false) public class UserDTO implements Serializable {
private static final long serialVersionUID = 1L; private final String id; private final String loginName; private final String password; private final String name; private final int age; private final String email; private final String phone; }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
public static void main(String[] args) {
UserDTO userDTO = UserDTO.myBuilder() .id("123") .loginName("zhangsan") .name("张三") .password("password") .build();
UserDTO newUserDTO = userDTO.toBuilder().name("李四盗号了").build(); }
|