场景

我们要建造一个复杂的产品。比如:飞机。这个复杂的产品的创建,有这样一个问题需要处理:

  • 装配这些子组件是不是有个步骤问题?


建造者模式的本质

  • 分离了对象子组件的单独构造(由Builder来负责)和装配(由Director负责)。从而可以构造出复杂的对象。这个模式适用于:某个对象的构建过程复杂的情况下使用
  • 由于实现了构建和装配的解耦。不同的构建器,相同的装配,也可以做出不同的对象;相同的构建器,不同的装配顺序也可以做出不同的对象
  • 实现了构建算法、装配算法的解耦,实现了更好的复用






组件

引擎:Engine.java

package shejimoshi.建造者模式.组件;

/**
* 引擎
*
* @author 陈霓清
*/
public class Engine {
private String name;

public Engine(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

轨道舱:OrbitalModule.java

package shejimoshi.建造者模式.组件;

/**
* 轨道舱
*
* @author 陈霓清
*/
public class OrbitalModule {
private String name;

public OrbitalModule(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

}

逃逸塔:EscapeTower.java

package shejimoshi.建造者模式.组件;

/**
* 逃逸塔
*
* @author 陈霓清
*/
public class EscapeTower {
private String name;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public EscapeTower(String name) {
super();
this.name = name;
}

}


产品

宇宙飞船:AirShip.java

package shejimoshi.建造者模式.产品;

import shejimoshi.建造者模式.组件.Engine;
import shejimoshi.建造者模式.组件.EscapeTower;
import shejimoshi.建造者模式.组件.OrbitalModule;

/**
* 宇宙飞船
*
* @author 陈霓清
*/
public class AirShip {

private OrbitalModule orbitalModule; // 轨道舱
private Engine engine; // 引擎
private EscapeTower escapeTower; // 逃逸塔

public void launch() {
System.out.println("发射!");
}

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;
}

}


构建

构建者接口:AirShipBuilderInterface.java

package shejimoshi.建造者模式.构建;

import shejimoshi.建造者模式.组件.Engine;
import shejimoshi.建造者模式.组件.EscapeTower;
import shejimoshi.建造者模式.组件.OrbitalModule;

public interface AirShipBuilderInterface {
// 引擎
Engine builderEngine();
// 轨道舱
OrbitalModule builderOrbitalModule();
// 逃逸塔
EscapeTower builderEscapeTower();
}

构建者实现类:AirShipBuilder.java

package shejimoshi.建造者模式.构建;

import shejimoshi.建造者模式.组件.Engine;
import shejimoshi.建造者模式.组件.EscapeTower;
import shejimoshi.建造者模式.组件.OrbitalModule;

public class AirShipBuilder implements AirShipBuilderInterface {

@Override
public Engine builderEngine() {
System.out.println("构建发动机!");
return new Engine("发动机!");
}

@Override
public EscapeTower builderEscapeTower() {
System.out.println("构建逃逸塔");
return new EscapeTower("逃逸塔");
}

@Override
public OrbitalModule builderOrbitalModule() {
System.out.println("构建轨道舱");
return new OrbitalModule("轨道舱");
}

}


装配

装配接口:AirShipDirectorInterface.java

package shejimoshi.建造者模式.装配;

import shejimoshi.建造者模式.产品.AirShip;

public interface AirShipDirectorInterface {

// 组装飞船对象
AirShip directAirShip();

}

装配实现类:AirshipDirector.java

package shejimoshi.建造者模式.装配;

import shejimoshi.建造者模式.产品.AirShip;
import shejimoshi.建造者模式.构建.AirShipBuilderInterface;
import shejimoshi.建造者模式.组件.Engine;
import shejimoshi.建造者模式.组件.EscapeTower;
import shejimoshi.建造者模式.组件.OrbitalModule;

public class AirshipDirector implements AirShipDirectorInterface {

private AirShipBuilderInterface builder;

public AirshipDirector(AirShipBuilderInterface builder) {
this.builder = builder;
}

/**
* 组装飞船对象
*/
@Override
public AirShip directAirShip() {
Engine e = builder.builderEngine();
OrbitalModule om = builder.builderOrbitalModule();
EscapeTower et = builder.builderEscapeTower();

// 装配成飞船对象
AirShip ship = new AirShip();
ship.setEngine(e);
ship.setEscapeTower(et);
ship.setOrbitalModule(om);

return ship;
}

}

调用者

Client.java

package shejimoshi.建造者模式;

import shejimoshi.建造者模式.产品.AirShip;
import shejimoshi.建造者模式.构建.AirShipBuilder;
import shejimoshi.建造者模式.装配.AirShipDirectorInterface;
import shejimoshi.建造者模式.装配.AirshipDirector;

public class Client {

public static void main(String[] args) {
AirShipDirectorInterface director = new AirshipDirector(new AirShipBuilder());

AirShip ship = director.directAirShip();

System.out.println(ship.getEngine().getName());

ship.launch();
}
}


回到顶部