Java 算法珠玑

多个构造函数时,考虑使用构造器

一个对象,具有多个成员变量可能需要初始化,常规方法,需要提供大量构造函数。例如:

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
public class AlertDialog {
private int width;
private int height;
private String title;
private String confirmText;
private String denyText;

private AlertDialog(){}
public AlertDialog(int width, int height){ // 空白的警告框
AlertDialog(width,height,null);
}

// 带标题的警告框
public AlertDialog(int width, int height, String title){ // 带标题的警告框
AlertDialog(width, height, title, "确定");
}

// 带标题的警告框,有确定按钮
public AlertDialog(int width, int height, String title, String confirm){
AlertDialog(width, height, title, confirm, null);
}

// 带标题的警告框,有确定按钮,取消按钮
public AlertDialog(int width, int height, String title, String confirm, String denyText){
// set every thing.
}
}

有多种样式的警告框,为了调用方便,必须提供多个构造函数,不灵活。如果采用另外一种方式,则可以解决,但会花费很多经历处理并发的情况:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class AlertDialog {
private int width;
private int height;
private String title;
private String confirmText;
private String denyText;

public AlertDialog(){}// 空白的构造函数

public void setWidth(int width){
this.width = width;
}
// 其他set方法
}

调用时,通过调用各个参数的set方法进行设置,但是并发解决不了,且无法进行参数校验。此时,可以使用构造器来解决:

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
public class AlertDialog {
private int width;
private int height;
private String title;
private String confirmText;
private String denyText;

// private
private AlertDialog(){}

// Builder中使用
protected AlertDialog(Builder b){
width = b.width;
height = b.height;
// .....
if(width==0||height==0) throws new Exception("size must be set");
}

// 构造器
public static class Builder {
private int width;
private int height;
private String title;
private String confirmText;
private String denyText;

// 注意:返回的Builder。
public Builder setTitle(String title) {
this.title = title;
return this;
}
// 其他set...

public AlertDialog build(){
return new AlertDialog(this);
}
}
}

于是,可以根据相应需求,进行相应设置,并在AlertDialog真正构造时,进行参数校验。如下:

1
new AlertDialog.Builder().setTitle("提示").build();

通用的 Builder 模式构建器

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
public class Builder<T> {
private final Supplier<T> instantiator;
private List<Consumer<T>> modifiers = new ArrayList<>();
public Builder(Supplier<T> instantiator) {
this.instantiator = instantiator;
}
public static <T> Builder<T> of(Supplier<T> instantiator) {
return new Builder<>(instantiator);
}
public <P1> Builder<T> with(Consumer1<T, P1> consumer, P1 p1) {
Consumer<T> c = instance -> consumer.accept(instance, p1);
modifiers.add(c);
return this;
}
public <P1, P2> Builder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2);
modifiers.add(c);
return this;
}
public <P1, P2, P3> Builder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3);
modifiers.add(c);
return this;
}
public T build() {
T value = instantiator.get();
modifiers.forEach(modifier -> modifier.accept(value));
modifiers.clear();
return value;
}
/**
* 1 参数 Consumer
*/
@FunctionalInterface
public interface Consumer1<T, P1> {
void accept(T t, P1 p1);
}
/**
* 2 参数 Consumer
*/
@FunctionalInterface
public interface Consumer2<T, P1, P2> {
void accept(T t, P1 p1, P2 p2);
}
/**
* 3 参数 Consumer
*/
@FunctionalInterface
public interface Consumer3<T, P1, P2, P3> {
void accept(T t, P1 p1, P2 p2, P3 p3);
}
}

使用方法

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
public class GirlFriend {
// 省略属性 ...
// 省略 getter & setter ...

// 为了演示方便,加几个聚合方法
public void addHobby(String hobby) {
this.hobby = Optional.ofNullable(this.hobby).orElse(new ArrayList<>());
this.hobby.add(hobby);
}
public void addGift(String day, String gift) {
this.gift = Optional.ofNullable(this.gift).orElse(new HashMap<>());
this.gift.put(day, gift);
}
public void setVitalStatistics(int bust, int waist, int hips) {
this.bust = bust;
this.waist = waist;
this.hips = hips;
}
public static void main(String[] args) {
GirlFriend myGirlFriend = Builder.of(GirlFriend::new)
.with(GirlFriend::setName, "小美")
.with(GirlFriend::setAge, 18)
.with(GirlFriend::setVitalStatistics, 33, 23, 33)
.with(GirlFriend::setBirthday, "2001-10-26")
.with(GirlFriend::setAddress, "上海浦东")
.with(GirlFriend::setMobile, "18688888888")
.with(GirlFriend::setEmail, "pretty-xiaomei@qq.com")
.with(GirlFriend::setHairColor, "浅棕色带点微卷")
.with(GirlFriend::addHobby, "逛街")
.with(GirlFriend::addHobby, "购物")
.with(GirlFriend::addHobby, "买东西")
.with(GirlFriend::addGift, "情人节礼物", "LBR 1912女王时代")
.with(GirlFriend::addGift, "生日礼物", "迪奥烈焰蓝金")
.with(GirlFriend::addGift, "纪念日礼物", "阿玛尼红管唇釉")
// 等等等等 ...
.build();
}
}