是什么——什么是建造者模式
建造者模式(Builder Pattern),是一種對象創(chuàng)建型設(shè)計模式拯腮。它將一個復(fù)雜對象的構(gòu)建與它的表示分離荸型,使得同樣的構(gòu)建過程可以創(chuàng)建不同的表示。
《Effective Java》中提到馏予,在設(shè)計類的時候,類的構(gòu)造方法或靜態(tài)工廠方法有很多參數(shù)的時候,使用建造者模式是一個很好的選擇召川。
UML類圖:
- Builder類:抽象建造器類
- ConcreteBuilder類:具體建造器類
- Product類:產(chǎn)品類
-
Director類:組裝類
為什么——為什么要用建造者模式
為了將構(gòu)建復(fù)雜對象的過程和它的部件解耦。
怎么做——怎么實現(xiàn)建造者模式
現(xiàn)在有兩種建造者模式胸遇,一種是有Director類組裝產(chǎn)品類的荧呐,另一種則是直接有Builder類組裝。
產(chǎn)品類:
/**
* Builder模式產(chǎn)品類
*/
public class Product {
private String partA;
private String partB;
private String partC;
//getter setter方法
//.....
@Override
public String toString() {
return "Product{" +super.toString()+
"\npartA='" + partA + '\'' +
", partB='" + partB + '\'' +
", partC='" + partC + '\'' +
'}';
}
}
有Director類
抽象建造器類:
/**
* Builder模式抽象構(gòu)造器類
*/
public abstract class Builder {
protected abstract void buildPartA(String partA);
protected abstract void buildPartB(String partB);
protected abstract void buildPartC(String partC);
protected abstract Product getResult();
}
組裝類:
public class Director {
private Builder builder;
public Director(Builder builder){
this.builder = builder;
}
public void setBuilder(Builder builder) {
this.builder = builder;
}
/**
* Director類組裝產(chǎn)品
*/
public Product construct(String partA,String partB,String partC){
builder.buildPartA(partA);
builder.buildPartB(partB);
builder.buildPartC(partC);
return builder.getResult();
}
}
具體建造者類
/**
* 具體建造者類
*/
public class ProductBuilder extends Builder {
private Product product = new Product();
@Override
protected void buildPartA(String partA) {
product.setPartA(partA);
}
@Override
protected void buildPartB(String partB) {
product.setPartB(partB);
}
@Override
protected void buildPartC(String partC) {
product.setPartC(partC);
}
@Override
protected Product getResult() {
return product;
}
}
直接用Builder類組裝
抽象建造者類:
public abstract class WithoutDirectorBuilder {
protected abstract WithoutDirectorBuilder setPartA(String partA);
protected abstract WithoutDirectorBuilder setPartB(String partB);
protected abstract WithoutDirectorBuilder setPartC(String partC);
protected abstract Product create();
}
具體建造者類:
/**
* Builder類直接組裝
*/
public class WithoutDirectorProductBuilder extends WithoutDirectorBuilder {
private Product product = new Product();
@Override
protected WithoutDirectorBuilder setPartA(String partA) {
product.setPartA(partA);
return this;
}
@Override
protected WithoutDirectorBuilder setPartB(String partB) {
product.setPartB(partB);
return this;
}
@Override
protected WithoutDirectorBuilder setPartC(String partC) {
product.setPartC(partC);
return this;
}
@Override
protected Product create() {
return product;
}
}
實際使用:
public class Client {
public static void main(String[] args) {
//使用Director類的建造者模式
Builder builder = new ProductBuilder();
Director director = new Director(builder);
Product product = director.construct("part A!","part B!","part C!");
System.out.println(product.toString());
//直接使用Builder類組裝的建造者模式
WithoutDirectorProductBuilder withoutDirectorProductBuilder
= new WithoutDirectorProductBuilder();
Product product1 = withoutDirectorProductBuilder
.setPartA("PartA")
.setPartB("PartB")
.setPartC("PartC")
.create();
System.out.println(product1.toString());
}
}