定義:
將一個復(fù)雜對象的構(gòu)建與它的表示分離欧瘪,使得同樣的構(gòu)建過程可以創(chuàng)建不同的表示。
使用場景:
(1)相同的使用方法,不同的執(zhí)行順序鳄抒,產(chǎn)生不同的事件結(jié)果時。
(2)多個部件或零件椰弊,都可以裝配到一個對象中许溅,但是產(chǎn)生的運行結(jié)果又不相同時。
(3)產(chǎn)品類非常復(fù)雜秉版,或者產(chǎn)品類中的調(diào)用順序不同產(chǎn)生了不同的作用贤重。
(4)當(dāng)初始化一個對象特別復(fù)雜,如參數(shù)多沐飘,且很多參數(shù)都具有默認值時游桩。
/**
* 抽象產(chǎn)品類
*/
abstract class Product {
protected String name;
protected String type;
public void setName(String name) {
this.name = name;
}
public void setType(String type) {
this.type = type;
}
public abstract void showProduct();
}
/**
* 小米手機
*/
public class XiaoMiPhone extends Product {
@Override
public void showProduct() {
System.out.println("名稱:" + name + "型號:" + type);
}
}
/**
* 抽象建造者類
*/
abstract class Builder {
public abstract Builder setName(String name);
public abstract Builder setType(String type);
public abstract Product create();
}
/**
* 具體建造者
*/
class ConcreteBuilder extends Builder {
private Product product = new XiaoMiPhone();
@Override
public Builder setName(String name) {
product.setName(name);
return this;
}
@Override
public Builder setType(String type) {
product.setType(type);
return this;
}
@Override
public Product create() {
return product;
}
/**
* 動態(tài)注入不同產(chǎn)品
*
* @param product
*/
public void setProduct(Product product) {
this.product = product;
}
}
/**
* 指揮者--用于具體產(chǎn)品的構(gòu)造
*/
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public Product constructA(String name, String type) {
return builder.setName(name).setType(type).create();
}
public Product constructB(String name, String type) {
return builder.setType(type).setName(name).create();
}
}
/**
* 客戶端調(diào)用
*/
public class Client {
public static void main(String[] args) {
Builder builder = new ConcreteBuilder();
Director director = new Director(builder);
Product productA = director.constructA("小米", "MI-6X");
productA.showProduct();
Product productB = director.constructB("為發(fā)燒而生:小米", "MI-6X");
productB.showProduct();
}
}
安卓源碼中的常見用法:
AlertDialog.Builer builder=new AlertDialog.Builder(context);
builder.setIcon(R.drawable.icon)
.setTitle("title")
.setMessage("message")
.setPositiveButton("Button1",
new DialogInterface.OnclickListener(){
public void onClick(DialogInterface dialog,int whichButton){
setTitle("click");
}
})
.create()
.show();
最簡單常用的寫法:
/**
* 蘋果手機
*/
public class ApplePhone {
private String name;
private String type;
public static class Builder {
private String name;
private String type;
public Builder setName(String name){
this.name = name;
return this;
}
public Builder setType(String type){
this.type = type;
return this;
}
public ApplePhone create() {
ApplePhone phone = new ApplePhone();
phone.name = name;
phone.type = type;
return phone;
}
}
}
/**
* 客戶端調(diào)用
*/
public class Client {
public static void main(String[] args) {
ApplePhone.Builder builder = new ApplePhone.Builder();
ApplePhone phone = builder.setType("Iphone8X").setName("我是一個小蘋果").create();
}
}
建造者模式的優(yōu)點是封裝性好,且易于擴展耐朴。建造者類一般只提供產(chǎn)品類中各個組件的建造借卧,而將具體建造過程交付給指揮者類。由指揮者類負責(zé)將各個組件按照特定的規(guī)則組建為產(chǎn)品筛峭,然后將組建好的產(chǎn)品交付給客戶端铐刘。