1.场景:建造一个复杂的产品,或者构建一个复杂对象需要很多步骤,而且步骤之间有一定的顺序
2.本质:
<1>分离了对象子组件的单独构造(由Builder来负责)和装配(由Director负责)。从而可以构造出复杂的对象。这个模式适用于:某个对象的构造过程复杂的情况下使用。
<2>由于实现了构建和装配的解蕅。不同的构建 器,相同的装配,也可以装配出不同的对象;相同的构建器,不同的装配顺序也可以做出不同的对象。也就是实现了构建算法,装配算法的解藕,实现了更好的复用。
3.示例代码:
<1>构建的实体及实体包含的组件
/**
* @author Administrator
* 飞船
*/
public class Airship {
private Engine engine;
private OrbitalModule orbitalModule;
private EscapeTower escapeTower;
public Airship() {
super();
}
public Airship(Engine engine, OrbitalModule orbitalModule, EscapeTower escapeTower) {
super();
this.engine = engine;
this.orbitalModule = orbitalModule;
this.escapeTower = escapeTower;
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public OrbitalModule getOrbitalModule() {
return orbitalModule;
}
public void setOrbitalModule(OrbitalModule orbitalModule) {
this.orbitalModule = orbitalModule;
}
public EscapeTower getEscapeTower() {
return escapeTower;
}
public void setEscapeTower(EscapeTower escapeTower) {
this.escapeTower = escapeTower;
}
}
//引擎
class Engine{
private String name;
public Engine(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//轨道窗
class OrbitalModule{
private String name;
public OrbitalModule(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//逃逸塔
class EscapeTower{
private String name;
public EscapeTower(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
<2>抽象的建造者
//构建者接口
public interface AirshipBuilder {
Engine buildEngine();
OrbitalModule buildOrbialModule();
EscapeTower buildEscapeTower();
}
<3>具体的构建者
//具体构建者
public class ChinaAirshipBuilder implements AirshipBuilder {
@Override
public Engine buildEngine() {
System.out.println("构造中国飞天引擎");
return new Engine("飞天引擎");
}
@Override
public OrbitalModule buildOrbialModule() {
System.out.println("构造中国飞天轨道窗");
return new OrbitalModule("飞天轨道窗");
}
@Override
public EscapeTower buildEscapeTower() {
System.out.println("构造中国飞天逃逸塔");
return new EscapeTower("飞天逃逸塔");
}
}
<4>抽象装配者
//装配者接口
public interface AirshipDirector {
Airship directorAirship();
}
<5>具体装配者
//具体的装配器
public class ChinaAirshipDirector implements AirshipDirector {
//构造器
private AirshipBuilder airshipBuilder;
public ChinaAirshipDirector(AirshipBuilder airshipBuilder) {
super();
this.airshipBuilder = airshipBuilder;
}
@Override
public Airship directorAirship() {
Engine engine=airshipBuilder.buildEngine();
OrbitalModule orbitalModule=airshipBuilder.buildOrbialModule();
EscapeTower escapeTower=airshipBuilder.buildEscapeTower();
Airship chinaAirship=new Airship(engine, orbitalModule, escapeTower);
System.out.println("装配中国飞天飞船成功");
return chinaAirship;
}
}
<6>客户端
//客户端
public class Client {
public static void main(String[] args) {
AirshipBuilder airshipBuilder=new ChinaAirshipBuilder();
AirshipDirector airshipDirector=new ChinaAirshipDirector(airshipBuilder);
Airship airship=airshipDirector.directorAirship();
}
}
4.实际举例
<1>StringBuilder类的append方法
<2>SQL中的PrepareStatement
<3>JDOM中的DomBuilder,SAXBuilder