定义
将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
主要作用
在用户不知道 对象的建造过程和细节 的情况下就可以直接创建复杂的对象。
用户只需要给出指定复杂对象的类型和内容,建造者模式负责按顺序创建复杂对象(把内部的建造过程和细节隐藏起来)。
UML图:
//抽象的建造者:方法
public abstract class Builder{
abstract void buildA(); //地基
abstract void buildB(); //钢筋工程
abstract void buildC(); //铺电线
abstract void buildD(); //粉刷
//完工:得到产品
abstract Product getProduct();
}
----------------------------------------------------------------------------
//产品:房子
public class Product{
private String buildA;
private String buildB;
private String buildC;
private String buildD;
public String getBuildA(){
return buildA;
}
public void setBuilderA(String buildA){
this.buildA =buildA;
}
public String getBuildB(){
return buildB;
}
public void setBuilderB(String buildB){
this.buildB =buildB;
}
public String getBuildC(){
return buildC;
}
public void setBuilderC(String buildC){
this.buildC =buildC;
}
public String getBuildD(){
return buildD;
}
public void setBuilderD(String buildD){
this.buildD =buildD;
}
}
----------------------------------------------------------------------------
//具体的建造者:工人
public class Worker extends Builder{
private Product product;
public Worker(){
product = new Product();
}
@Override
void buildA(){
product.setBuildA("地基");
System.out.println("地基");
}
@Override
void buildB(){
product.setBuildB("钢筋工程");
System.out.println("钢筋工程");
}
@Override
void buildC(){
product.setBuildC("铺电线");
System.out.println("铺电线");
}
@Override
void buildD(){
product.setBuildD("粉刷");
System.out.println("粉刷");
}
@Override
Product getProduct(){
return product;
}
}
----------------------------------------------------------------------------
//指挥:核心。负责指挥构建一个工程,工程如何构建,由它决定
public class Director{
//指挥工人按照顺序建房子
public Product build(Builder builder){
builder.buildA();
builder.buildB();
builder.buildC();
builder.buildD();
return builder.getProduct();
}
}
通过静态内部类方式实现零件无序装配构造
//建造者
public abstract class Builder{
abstract Builder buildA(String msg); //汉堡
abstract Builder buildB(String msg); //饮料
abstract Builder buildC(String msg); //薯条
abstract Builder buildD(String msg); //甜点
abstract Product getProduct;
}
----------------------------------------------------------------------------
//产品:套餐
public class Product{
private String buildA = "汉堡";
private String buildB = "饮料";
private String buildC = "薯条";
private String buildD = "甜点";
public String getBuildA(){
return buildA;
}
public void setBuilderA(String buildA){
this.buildA =buildA;
}
public String getBuildB(){
return buildB;
}
public void setBuilderB(String buildB){
this.buildB =buildB;
}
public String getBuildC(){
return buildC;
}
public void setBuilderC(String buildC){
this.buildC =buildC;
}
public String getBuildD(){
return buildD;
}
public void setBuilderD(String buildD){
this.buildD =buildD;
}
}
----------------------------------------------------------------------------
//具体的建造者
public class Worker extends Builder{
private Product product;
public Worker(){
product = new Product();
}
@Override
Builder buildA(String msg){
product.setBuildA(msg);
return this;
}
@Override
Builder buildB(String msg){
product.setBuildB(msg);
return this;
}
@Override
Builder buildC(String msg){
product.setBuildC(msg);
return this;
}
@Override
Builder buildD(String msg){
product.setBuildD(msg);
return this;
}
@Override
Product getProduct(){
return product;
}
}
优点
- 产品的建造和表示分离,实现了解耦。使用建造者模式可以使客户端不必知道产品内部的细节。
- 将复杂产品的创建步骤分解在不同的方法中,使得创建过程更加清晰。
- 具体的建造者类之间式相互独立的,这有利于系统的扩展。增加新的具体建造者无需修改原有类库的代码,符合“开闭原则”。
缺点
- 建造者模式所创建的产品一般具有较多的共同点,其组成部分相似;如果产品之间的差异性很大,则不适合使用建造者模式,因此其适用范围受到一定的限制。
- 如果产品的内部变化复杂,可能会导致需要定义很多具体建造者来实现这种变化,导致系统变得很庞大。