一、场景
建造一个复杂的产品。
二、本质
将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示。 [构建与表示分离,同构建不同表示]
与抽象工厂的区别:在建造者模式里,有个指导者,由指导者来管理建造者,用户是与指导者联系的,指导者联系建造者最后得到产品。即建造模式可以强制实行一种分步骤进行的建造过程。
建造模式是将复杂的内部创建封装在内部,对于外部调用的人来说,只需要传入建造者和建造工具,对于内部是如何建造成成品的,调用者无需关心。
举个简单的例子,如汽车,有很多部件,车轮,方向盘,发动机还有各种小零件等等,部件很多,但远不止这些,如何将这些部件装配成一部汽车,这个装配过程也很复杂(需要很好的组装技术), builder模式就是为了将部件和组装分开。
参与者
- Builder
—— 为创建一个Product对象的各个部件指定抽象接口
- ConcreteBuilder
—— 实现Builder的接口以构造和装配该产品的各个部件
—— 定义并明确它所创建的表示
—— 提供一个检索产品的接口
- Director
—— 构造一个可以使用Builder接口的对象
- Product
—— 表示被构造的复杂对象。ConcreteBuilder创建该产品的内部表示并定义它的装配过程
—— 包含定义组成部件的类,包含将这些部件装配成最终产品的接口
三、示例
package com.lgd.builder;
/**
* 宇宙飞船
* @author Administrator
*
*/
public class AirShip {
private OrbitalModule orbitalModuleString; //轨道舱
private Engine engineString; //发动机
private EscapeTower escapeTowerString; //逃逸仓
public void launch()
{
System.out.println("发射。");
}
public OrbitalModule getOrbitalModuleString() {
return orbitalModuleString;
}
public void setOrbitalModuleString(OrbitalModule orbitalModuleString) {
this.orbitalModuleString = orbitalModuleString;
}
public Engine getEngineString() {
return engineString;
}
public void setEngineString(Engine engineString) {
this.engineString = engineString;
}
public EscapeTower getEscapeTowerString() {
return escapeTowerString;
}
public void setEscapeTowerString(EscapeTower escapeTowerString) {
this.escapeTowerString = escapeTowerString;
}
}
class OrbitalModule{
private String nameString;
public OrbitalModule(String name){
this.nameString = name;
}
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
}
class Engine{
private String nameString;
public Engine(String name){
this.nameString = name;
}
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
}
class EscapeTower{
private String nameString;
public EscapeTower(String name){
this.nameString = name;
}
public String getNameString() {
return nameString;
}
public void setNameString(String nameString) {
this.nameString = nameString;
}
}
==================
package com.lgd.builder;
public interface AirShipBuilder {
Engine buliderEngine();
OrbitalModule buliderOrbitalModule();
EscapeTower builderEscapeTower();
}
package com.lgd.builder;
public class LgdAirShipBuilder implements AirShipBuilder{//StringBuilder
@Override
public Engine buliderEngine() {
// TODO Auto-generated method stub
System.out.println("LGD构建发动机");
return new Engine("LGD发送机");
}
@Override
public OrbitalModule buliderOrbitalModule() {
System.out.println("LGD构建轨道舱");
return new OrbitalModule("LGD轨道舱");
}
@Override
public EscapeTower builderEscapeTower() {
System.out.println("LGD构建逃逸塔");
return new EscapeTower("LGD逃逸塔");
}
}
package com.lgd.builder;
public class LkxAirShipBuilder implements AirShipBuilder{
@Override
public Engine buliderEngine() {
// TODO Auto-generated method stub
System.out.println("LKX构建发动机");
return new Engine("LKX发送机");
}
@Override
public OrbitalModule buliderOrbitalModule() {
System.out.println("LKX构建轨道舱");
return new OrbitalModule("LKX轨道舱");
}
@Override
public EscapeTower builderEscapeTower() {
System.out.println("LKX构建逃逸塔");
return new EscapeTower("LKX逃逸塔");
}
}
==================
package com.lgd.builder;
public interface AirShipDirector {
//组装飞船
AirShip createAirShip();
}
package com.lgd.builder;
public class LgdAirShipDirector implements AirShipDirector{
private AirShipBuilder builder;
public LgdAirShipDirector(AirShipBuilder builder) {
super();
this.builder = builder;
}
@Override
public AirShip createAirShip() {
// TODO Auto-generated method stub
Engine engine = builder.buliderEngine();
OrbitalModule orbitalModule = builder.buliderOrbitalModule();
EscapeTower escapeTower = builder.builderEscapeTower();
//装配成飞船对象
AirShip ship = new AirShip();
ship.setEngineString(engine);
ship.setEscapeTowerString(escapeTower);
ship.setOrbitalModuleString(orbitalModule);
return ship;
}
}
==================
package com.lgd.builder;
public class client {
public static void main(String[] args) {
AirShipDirector directorLgd = new AirShipDirectorImpl(new LgdAirShipBuilder());
AirShip shipLgd = directorLgd.createAirShip();
System.out.println(shipLgd.getEngineString().getNameString());
shipLgd.launch();
AirShipDirector directorLkx = new AirShipDirectorImpl(new LkxAirShipBuilder());
AirShip shipLkx = directorLkx.createAirShip();
System.out.println(shipLkx.getEngineString().getNameString());
shipLkx.launch();
}
}
运行结果:
LGD构建发动机
LGD构建轨道舱
LGD构建逃逸塔
LGD发送机
发射。
LKX构建发动机
LKX构建轨道舱
LKX构建逃逸塔
LKX发送机
发射。