前面幾篇關於設計模式的文章:設計模式
《單例模式——8種實現方式》markdown
這些設計模式均可歸爲對象建立型模式。今天咱們聊聊最後一種對象建立型模式:建造者模式(Builder Pattern)post
建造者(Builder pattern)模式的定義:將一個複雜對象的構造與它的表示分離,使一樣的構建過程能夠建立不一樣的表示ui
建造者模式的核心是:將產品和產品建造過程解耦this
建造者模式通常適用於建立複雜且由多個部分組成的對象的場景。經過建造者模式,能夠把複雜對象的建造過程抽象出來(抽象類別),客戶端使用時沒必要知道產品構建的細節。lua
建造者模式通常由產品、抽象建造者、具體建造者、指揮者等4個角色組成。url
下面用具體的場景來展現建造者模式:spa
如今咱們要建立一輛自行車。
自行車有多個部件組成,一般有:輪子、腳踏板、車身、座椅、車手把
咱們也能夠建造不一樣的類型的自行車,好比山地自行車、共享單車(青桔單車)等
產品角色(自行車):
public class Bicycle {
//輪子
private String wheel;
// 腳踏板
private String pedal;
// 車身
private String crossbar;
// 座椅
private String saddle;
// 車手把
private String handlebars;
// getter和setter省略
@Override
public String toString() {
return "Bicycle{" +
"wheel='" + wheel + ''' +
", pedal='" + pedal + ''' +
", crossbar='" + crossbar + ''' +
", saddle='" + saddle + ''' +
", handlebars='" + handlebars + ''' +
'}';
}
}
複製代碼
抽象建造者(自行車建造者)
public interface BicycleBuilder {
void builtWheel(String wheel);
void builtPedal(String pedal);
void builtCrossbar(String crossbar);
void buildSaddle(String saddle);
void buildHandlebars(String handlebars);
Bicycle build();
}
複製代碼
具體建造者(青桔自行車建造者)(非廣告,只由於今晚騎青桔了)
public class DiDiBicycleBuilder implements BicycleBuilder {
private Bicycle bicycle;
public DiDiBicycleBuilder() {
bicycle = new Bicycle();
}
@Override
public void builtWheel(String wheel) {
this.bicycle.setWheel(wheel);
}
@Override
public void builtPedal(String pedal) {
this.bicycle.setPedal(pedal);
}
@Override
public void builtCrossbar(String crossbar) {
this.bicycle.setCrossbar(crossbar);
}
@Override
public void buildSaddle(String saddle) {
this.bicycle.setSaddle(saddle);
}
@Override
public void buildHandlebars(String handlebars) {
this.bicycle.setHandlebars(handlebars);
}
@Override
public Bicycle build() {
return bicycle;
}
}
複製代碼
指揮者(自行車建立指揮者)
public class BicycleDirector {
private BicycleBuilder builder;
public BicycleDirector(BicycleBuilder builder) {
this.builder = builder;
}
public Bicycle builtBicycle(String wheel, String pedal, String crossbar, String saddle, String handlebars) {
builder.builtWheel(wheel);
builder.builtPedal(pedal);
builder.builtCrossbar(crossbar);
builder.buildSaddle(saddle);
builder.buildHandlebars(handlebars);
return builder.build();
}
}
複製代碼
客戶端,使用建造者模式建立自行車對象
public class Main {
public static void main(String[] args) {
BicycleBuilder builder = new DiDiBicycleBuilder();
BicycleDirector director = new BicycleDirector(builder);
Bicycle bicycle = director.builtBicycle("青桔輪子", "青桔腳踏板", "青桔車身", "舒服的座椅", "青桔車手把");
System.out.println(bicycle);
}
}
複製代碼
咱們常見的鏈式調用建立對象,其實也是利用了建造者模式。這種建造者模式,沒有具體的角色,而是把建立的過程封裝在Builder構建器裏了。
具體的代碼:
public class Bicycle {
//輪子
private String wheel;
// 腳踏板
private String pedal;
// 車身
private String crossbar;
// 座椅
private String saddle;
// 車手把
private String handlebars;
private Bicycle(BicycleBuilder bicycleBuilder) {
this.wheel = bicycleBuilder.wheel;
this.pedal = bicycleBuilder.pedal;
this.crossbar = bicycleBuilder.crossbar;
this.saddle = bicycleBuilder.saddle;
this.handlebars = bicycleBuilder.handlebars;
}
// 建造者,靜態內部類。將建立的過程封裝在建造者中
public static class BicycleBuilder{
//輪子
private String wheel;
// 腳踏板
private String pedal;
// 車身
private String crossbar;
// 座椅
private String saddle;
// 車手把
private String handlebars;
public BicycleBuilder wheel(String wheel){
this.wheel = wheel;
return this;
}
public BicycleBuilder pedal(String pedal) {
this.pedal = pedal;
return this;
}
public BicycleBuilder crossbar(String crossbar) {
this.crossbar = crossbar;
return this;
}
public BicycleBuilder saddle(String saddle) {
this.saddle = saddle;
return this;
}
public BicycleBuilder handlebars(String handlebars) {
this.handlebars = handlebars;
return this;
}
public Bicycle built() {
return new Bicycle(this);
}
}
@Override
public String toString() {
return "Bicycle{" +
"wheel='" + wheel + ''' +
", pedal='" + pedal + ''' +
", crossbar='" + crossbar + ''' +
", saddle='" + saddle + ''' +
", handlebars='" + handlebars + ''' +
'}';
}
}
複製代碼
客戶端使用:
public class Main {
public static void main(String[] args) {
Bicycle bicycle = new Bicycle.BicycleBuilder().wheel("山地車輪子").crossbar("不鏽鋼車身").handlebars("減壓車把").built();
System.out.println(bicycle);
}
}
// 輸出結果:
Bicycle{wheel='山地車輪子', pedal='null', crossbar='不鏽鋼車身', saddle='null', handlebars='減壓車把'}
複製代碼