生成器模式是创造型模式的一种, 和工厂模式,抽象工厂莫斯类似。该模式是为了解决该类问题:当工厂模式或抽象工厂模式中的对象包含很对属性时

之所以出现该种模式,主要因为工厂模式和抽象工厂模式存在以下问题

  1. 当创建的对象需要的参数很多时,客户端程序很难保证参数顺序的正确性
  2. 参数中有些对于个别对象是并不是必须的,但是工厂模式中必须传入工厂中所有对象的属性,如果不是必须的,也需要传入null
  3. 针对上述情况,获取我们可以针对不对的对象生成不同属性的setter 函数,当此时工厂中的对象的状态是不一致的

生成器模式就很好的解决了上述问题

针对每种对象的属性不一致的问题,生成器模式是这样解决的:在生成对象时,使用step-by-step的方式去设置需要对象的属性,最后再返回创建好的对象

还是以在工厂模式中的Computer类为例子

首先我们在 Computer 内部实现一个 ComputerBuilder 公有的静态内部类,将客户端参数全部到该内部类的属性中,类名最好是类名+Builder来命名

该内部类对外提供构造函数,一系列setter 函数,用来设置optional parameters,并且这些setter函数都返回 Builder类本身

在全部设置完参数后,再调用build 函数来返回创造好的对象,该函数一般可以调用生成对象的私有构造函数

下面是实现代码

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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.walterlife.dp.BuilderDP;

import org.apache.log4j.Logger;

public class Computer {
// required parameters
public String HDD;
public String RAM;

// optional parameters
public boolean isGraphicsCardEnabled;
public boolean isBuletoothEnabled;

public static Logger logger = Logger.getLogger(Computer.class);

private Computer(ComputerBuilder computerBuilder) {
if(computerBuilder != null) {
this.HDD = computerBuilder.HDD;
this.RAM = computerBuilder.RAM;
this.isGraphicsCardEnabled = computerBuilder.isGraphicsCardEnabled;
this.isBuletoothEnabled = computerBuilder.isBuletoothEnabled;
} else {
logger.error("Computer constructer param computerBuilder is null!!!");
}
}

public String toString() {
return ("HDD: " + this.HDD
+ " RAM: " + this.RAM
+ " isGraphicsCardEnabled: " + this.isGraphicsCardEnabled
+ " isBuletoothEnabled: " + this.isBuletoothEnabled);
}

/**
* @return the hDD
*/

public String getHDD() {
return HDD;
}

/**
* @return the rAM
*/

public String getRAM() {
return RAM;
}

/**
* @return the isGraphicsCardEnabled
*/

public boolean isGraphicsCardEnabled() {
return isGraphicsCardEnabled;
}

/**
* @return the isBuletoothEnabled
*/

public boolean isBuletoothEnabled() {
return isBuletoothEnabled;
}

public static class ComputerBuilder {

// required parameters
public String HDD;
public String RAM;

// optional parameters
public boolean isGraphicsCardEnabled;
public boolean isBuletoothEnabled;

public ComputerBuilder(String HDD, String RAM) {
this.HDD = HDD;
this.RAM = RAM;
}

/**
* @param isGraphicsCardEnabled the isGraphicsCardEnabled to set
*/

public ComputerBuilder setGraphicsCardEnabled(boolean isGraphicsCardEnabled) {
this.isGraphicsCardEnabled = isGraphicsCardEnabled;
return this;
}

/**
* @param isBuletoothEnabled the isBuletoothEnabled to set
*/

public ComputerBuilder setBuletoothEnabled(boolean isBuletoothEnabled) {
this.isBuletoothEnabled = isBuletoothEnabled;
return this;
}

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

在上述代码中,Computer类只提供一个 getter methods,且没有共有构造函数,所以获取Compter对象的唯一方法是通过ComputerBuilder类获得(build方法)

下面是测试代码

1
2
3
4
5
6
7
8
public static void testBuilder() {
Computer computer = new Computer.ComputerBuilder("500 GB", "2 GB")
.setGraphicsCardEnabled(true)
.setBuletoothEnabled(true)
.build();

System.out.printf("build computer result: %s\n", computer.toString());
}

测试结果

1
build computer result: HDD: 500 GB RAM: 2 GB isGraphicsCardEnabled: true isBuletoothEnabled: true

生成器模式在JDK中的运用

  1. java.lang.StringBuilder#append() (unsynchronized)
  2. java.lang.StringBuffer#append() (synchronized)

留言