一、定义
将一个复杂的对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示。
二、适用情况
1、当创建复杂对象的算法应该独立于该对象的组成部分以及它们的装配方式时。
2、当构造过程必须允许被构造的对象有不同的表示时
三、角色
1、Builder:为创建一个Product对象的各个部件指定抽象接口。
2、ConcreteBuilder:实现Builder的接口以构造和装配该产品的各个部件,定义并明确它所创建的表示,并提供一个检索产品的接口。
2、Director:构造一个使用Builder接口的对象,指导构建过程。
3、Product:表示被构造的复杂对象。ConcreteBuilder创建该产品的内部表示并定义它的装配过程,包含定义组成部件的类,包括将这些部件装配成最终产品的接口。
四、范例
Product:
/**
* @Title: AirShip.java
* @Package cn.wong.pattern.builder
* @Description: TODO
* @author Lar
* @Modifier Lar
* @version
*/
public class AirShip {
private OrbitalModule orbitalModule;
private Engine engine;
private EscapeTower escapeTower;
public OrbitalModule getOrbitalModule() {
return orbitalModule;
}
public void setOrbitalModule(OrbitalModule orbitalModule) {
this.orbitalModule = orbitalModule;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public EscapeTower getEscapeTower() {
return escapeTower;
}
public void setEscapeTower(EscapeTower escapeTower) {
this.escapeTower = escapeTower;
}
}
/**
* 轨道舱
*
* @Title: AirShip.java
* @Package cn.wong.pattern.builder
* @Description: TODO
* @author Lar
* @Modifier Lar
* @version
*/
class OrbitalModule {
// 名称
private String name;
public OrbitalModule() {
}
public OrbitalModule(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/**
* 发动机
*
* @Title: AirShip.java
* @Package cn.wong.pattern.builder
* @Description: TODO
* @author Lar
* @Modifier Lar
* @version
*/
class Engine {
// 名称
private String name;
// 品牌
private String brand;
public Engine() {
}
public Engine(String name, String brand) {
this.name = name;
this.brand = brand;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
/**
* 逃逸塔
*
* @Title: AirShip.java
* @Package cn.wong.pattern.builder
* @Description: TODO
* @author Lar
* @Modifier Lar
* @version
*/
class EscapeTower {
private String name;
public EscapeTower(String name) {
this.name = name;
}
public EscapeTower() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class LarAirShip extends AirShip {
public LarAirShip() {
System.out.println("开始建造Lar牌飞船。。。");
}
}
Builder:
public interface AirShipBuilder {
public void builderEngine();
public void builderOrbitalModule();
public void builderEscapeTower();
public AirShip builderAirShip();
}
ConcreteBuilder:
/**
* 构建Lar牌AirShip
* @Title: LarAirShipBuilder.java
* @Package cn.wong.pattern.builder
* @Description: TODO
* @author Lar
* @Modifier Lar
* @version
*/
public class LarAirShipBuilder implements AirShipBuilder {
AirShip airShip;
public LarAirShipBuilder(){
airShip = new LarAirShip();
}
public void builderEngine() {
System.out.println("构造发动机");
airShip.setEngine(new Engine("超大马力发动机","Lar牌"));
}
public void builderOrbitalModule() {
System.out.println("构造轨道舱");
airShip.setOrbitalModule(new OrbitalModule("Lar牌轨道舱"));
}
public void builderEscapeTower() {
System.out.println("构造逃逸塔");
airShip.setEscapeTower(new EscapeTower("Lar牌逃逸塔"));
}
public AirShip builderAirShip() {
return airShip;
}
}
Director:
这里使用了指导过程的抽象
/**
* @Title: AirShipDirector.java
* @Package cn.wong.pattern.builder
* @Description: TODO
* @author Lar
* @Modifier Lar
* @version
*/
public interface AirShipDirector {
public AirShip directorAirShip(AirShipBuilder asb);
}
public class LarAirShipDirector implements AirShipDirector {
@Override
public AirShip directorAirShip(AirShipBuilder asb) {
asb.builderEngine();
asb.builderEscapeTower();
asb.builderOrbitalModule();
return asb.builderAirShip();
}
}
Client:
public class Client {
public static void main(String[] args) {
AirShipDirector asd = new LarAirShipDirector();
AirShip larAirShip = asd.directorAirShip(new LarAirShipBuilder());
}
}
结果:
开始建造Lar牌飞船。。。
构造发动机
构造逃逸塔
构造轨道舱