定義
將一個復(fù)雜對象的構(gòu)建與它的表示分離,使得同樣的構(gòu)建過程可以創(chuàng)建不同的表示
你看懂了嗎? 反正我是一臉懵逼
UML類圖
解讀:
- Product產(chǎn)品類: 產(chǎn)品抽象類
- Builder: 抽象Builder類,規(guī)劃產(chǎn)品組建,一般是由子類實(shí)現(xiàn)具體的組建過程
- ConcreteBuilder: 具體的Builder類
- Director: 統(tǒng)一組建過程
好吧, 還是說的有點(diǎn)虛呀, 來一個簡單例子
簡單實(shí)現(xiàn)
Computer抽象類類:Product角色芳杏,即產(chǎn)品抽象類
/**
* Created by allever on 17-5-18.
* 計(jì)算機(jī)抽象類: Product角色*/
public abstract class Computer {
private String strBoard;
private String strDisplay;
private String strOS;
protected Computer(){}
/**CPU核心數(shù)*/
public void setBoard(String board){
this.strBoard = board;
}
public void setDisplay(String display){
this.strDisplay = display;
}
public void setStrOS(String os){
this.strOS = os;
}
@Override
public String toString() {
String result = "Computer:\nboard=" + strBoard + "\ndisplay = " + strDisplay + "\nOS = " + strOS;
return result;
}
}
MacBook類熬拒,具體Computer類
/**
* Created by allever on 17-5-18.
*/
public class MacBook extends Computer {
protected MacBook(){}
}
Builder抽象類
負(fù)責(zé)規(guī)范產(chǎn)品組建睡蟋,一般是由子類實(shí)現(xiàn)具體的組建過程
/**
* Created by allever on 17-5-18.
* 抽象Builder類*/
public abstract class Builder {
public abstract void buildBoard(String board);
public abstract void buildDisplay(String display);
public abstract void buildOS(String os);
public abstract Computer build();
}
MacBookBuilder:具體Builder類,ConcreteBuilder角色
/**
* Created by allever on 17-5-18.
* Builder實(shí)現(xiàn)類*/
public class MacBookBuilder extends Builder {
private Computer computerMacBook = new MacBook();
@Override
public void buildBoard(String board) {
computerMacBook.setBoard(board);
}
@Override
public void buildDisplay(String display) {
computerMacBook.setDisplay(display);
}
@Override
public void buildOS(String os) {
computerMacBook.setStrOS(os);
}
@Override
public Computer build() {
return computerMacBook;
}
}
Director類:同一組裝過程
/**
* Created by allever on 17-5-18.
* Director類: 負(fù)責(zé)構(gòu)建Computer*/
public class Director {
private Builder builder = null;
public Director(Builder builder){
this.builder = builder;
}
//構(gòu)建對象
public void construct(String board, String display, String os){
builder.buildBoard(board);
builder.buildDisplay(display);
builder.buildOS(os);
}
}
客戶端調(diào)用
Builder builder = new MacBookBuilder();
Director director = new Director(builder);
director.construct("英特爾酷睿i7","15.6寸","Ubuntu 16.04");
Log.d(TAG, builder.build().toString());
Toast.makeText(BuildPatternsActivity.this,builder.build().toString(),Toast.LENGTH_LONG).show();
小提示
通常,Director角色會被省略,直接使用Builder構(gòu)建對象,這個Builder通常為鏈?zhǔn)秸{(diào)用,即buildXXX()方法返回自身,return this;
修改后的Builder類
/**
* Created by allever on 17-5-18.
* 抽象Builder類*/
public abstract class Builder {
public abstract Builder buildBoard(String board);
public abstract Builder buildDisplay(String display);
public abstract Builder buildOS(String os);
public abstract Computer build();
}
MacBookBuilder:
/**
* Created by allever on 17-5-18.
* Builder實(shí)現(xiàn)類*/
public class MacBookBuilder extends Builder {
private Computer computerMacBook = new MacBook();
@Override
public MacBookBuilder buildBoard(String board) {
computerMacBook.setBoard(board);
return this;
}
@Override
public MacBookBuilder buildDisplay(String display) {
computerMacBook.setDisplay(display);
return this;
}
@Override
public MacBookBuilder buildOS(String os) {
computerMacBook.setStrOS(os);
return this;
}
@Override
public Computer build() {
return computerMacBook;
}
}
在客戶端中的調(diào)用
Builder builder = new MacBookBuilder();
//Director director = new Director(builder);
//director.construct("英特爾酷睿i7","15.6寸","Ubuntu 16.04");
Log.d(TAG, builder.buildBoard("英特爾酷睿i7")
.buildDisplay("15.6寸")
.buildOS("Max OS")
build().toString());
現(xiàn)在你是不是感覺到很熟悉的呢? 就像Android中構(gòu)建一個Notification對象
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle("This is content title")
.setContentText("This is content text")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
.setWhen(System.currentTimeMillis())
//設(shè)置點(diǎn)擊行為
.setContentIntent(pendingIntent)
//點(diǎn)擊后消失
.setAutoCancel(true)
//設(shè)置聲音
.setSound(Uri.fromFile(new File("/system/media/audio/ringtones/Luna.ogg")))
//設(shè)置震動 震動1s 靜止1s 震動1s 要申明權(quán)限
.setVibrate(new long[]{0, 1000,1000,1000})
//設(shè)置呼吸燈閃爍
.setLights(Color.GREEN, 1000, 1000)
//設(shè)置默認(rèn)
.setDefaults(NotificationCompat.DEFAULT_ALL)
//設(shè)置長文本
.setStyle(new NotificationCompat.BigTextStyle().bigText("An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map."))
//設(shè)置展開大圖,,設(shè)置了長文本會看不到
.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.mipmap.expensive)))
//設(shè)置通知重要程度
.setPriority(NotificationCompat.PRIORITY_MAX)
.build();