静态内部类的泛型与建造者模式

静态内部类的泛型和外部类的泛型没有任何关系,即使使用同一个字母。

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
package com.li.springboot.bean;

import java.util.List;

public class LayuiResponse<T> {

private final int code;
private final String msg;
private final List<T> data;
private final Integer count;

public int getCode() {
return code;
}

public String getMsg() {
return msg;
}

public List<T> getData() {
return data;
}

public int getCount() {
return count;
}

public static <T> Builder<T> builder() {
return new Builder<T>();
}

private LayuiResponse(Builder<T> builder) {
this.code = builder.code;
this.msg = builder.msg;
this.data = builder.data;
this.count = builder.count;
}

public static class Builder<R> {
private int code;
private String msg;
private List<R> data;
private Integer count;

public Builder<R> code(int code) {
this.code = code;
return this;
}

public Builder<R> msg(String msg) {
this.msg = msg;
return this;
}

public Builder<R> data(List<R> data) {
this.data = data;
return this;
}

public Builder<R> count(int count) {
this.count = count;
return this;
}

public LayuiResponse<R> build() {
return new LayuiResponse<>(this);
}
}
}

使用时,需指定内部类的泛型

1
2
3
 List<String> list = new ArrayList<>();
list.add("123");
LayuiResponse<String> ok = LayuiResponse.<String>builder().code(0).msg("ok").data(list).build();

使用构造器模式,主要是为了使属性在构建时一次性初始化好,不允许中间过程去设置属性,保持bean的状态一致性。同时也是为了清晰化构建过程。推荐使用final来修饰被构建对象的属性值, 确保成员属性不会被赋值。