一.小结
定义为一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。经典的建造者模式一般有Product、Builder、ConcreteBuilder、Director几个角色,由Director按照不同的规则建造具体的Product;现在常见的建造者模式一般只有Product和Builder两个角色,通过链式调用来产生不同的Product。
二.经典的建造者模式
如上图所示,建造者模式有4个角色。代码略
-
Product: 最终要生成的对象
-
Builder: 构建者的抽象基类,其定义了构建Product的抽象步骤,其实体类需要实现这些步骤。其会包含一个用来返回最终产品的方法
Product getProduct()
。 -
ConcreteBuilder: Builder的实现类。
-
Director: 决定如何构建最终产品的算法 其会包含一个负责组装的方法
void Construct(Builder builder)
, 在这个方法中通过调用builder的方法,就可以设置builder,等设置完成后,就可以通过builder的getProduct()
方法获得最终的产品。
三.常见的建造者模式
下面以歌曲属性来说明java bean形式的建造者模式,在java中一般构造函数与方法中参数不要超过5个,如果一个实体类字段太多的话可以采用Builder模式进行链式调用。
// 1.java bean形式的建造者
public class Song {
// 歌名
private String title;
// 歌手
private String artist;
// 专辑名称
private String albumName;
// 文件地址
private String data;
// 时长
private long duration;
// 码率
private int bitRate;
// 采样率
private String sampleRate;
// 位深
private String bitLength;
// 专辑封面地址
private String albumPath;
// 年份
private String year;
// 音乐类型
private String type;
public Song() {
}
public String getTitle() {
return title;
}
public String getArtist() {
return artist;
}
public String getAlbumName() {
return albumName;
}
public String getData() {
return data;
}
public long getDuration() {
return duration;
}
public int getBitRate() {
return bitRate;
}
public String getSampleRate() {
return sampleRate;
}
public String getBitLength() {
return bitLength;
}
public String getAlbumPath() {
return albumPath;
}
public String getYear() {
return year;
}
public String getType() {
return type;
}
@Override
public String toString() {
return "Song{"
+ "title='" + title + '\''
+ ", artist='" + artist + '\''
+ ", albumName='" + albumName + '\''
+ ", data='" + data + '\''
+ ", duration=" + duration
+ ", bitRate=" + bitRate
+ ", sampleRate='" + sampleRate + '\''
+ ", bitLength='" + bitLength + '\''
+ ", albumPath='" + albumPath + '\''
+ ", year='" + year + '\''
+ ", type='" + type + '\''
+ '}';
}
public static class Builder {
private Song song;
public Builder() {
this.song = new Song();
}
public Builder setTitle(String title) {
this.song.title = title;
return this;
}
public Builder setArtist(String artist) {
this.song.artist = artist;
return this;
}
public Builder setAlbumName(String albumName) {
this.song.albumName = albumName;
return this;
}
public Builder setData(String data) {
this.song.data = data;
return this;
}
public Builder setDuration(long duration) {
this.song.duration = duration;
return this;
}
public Builder setBitRate(int bitRate) {
this.song.bitRate = bitRate;
return this;
}
public Builder setSampleRate(String sampleRate) {
this.song.sampleRate = sampleRate;
return this;
}
public Builder setBitLength(String bitLength) {
this.song.bitLength = bitLength;
return this;
}
public Builder setAlbumPath(String albumPath) {
this.song.albumPath = albumPath;
return this;
}
public Builder setYear(String year) {
this.song.year = year;
return this;
}
public Builder setType(String type) {
this.song.type = type;
return this;
}
public Song build() {
return this.song;
}
}
}
// 2.情景类
public class Client {
public static void main(String[] args) {
String title = "Forever Young";
String artist = "艾怡良";
String albumName = "垂直活著,水平留戀著";
String year = "2018-12-21T08:00:00Z";
long duration = 300013000;// ms
String type ="Mandopop ";
Song song = new Song.Builder()
.setTitle(title)
.setArtist(artist)
.setAlbumName(albumName)
.setYear(year)
.setDuration(duration)
.setType(type)
.build();
System.out.println(song.toString());
}
}