建造者模式
2017-07-09 本文已影响0人
会思考的鸭子
场景:
当做一个复杂的产品时,我们需要将他们按照一定的步骤组件起来
建造者模式的本质
- 分离了对象子组件的单独构造和装配。从而可以构造出复杂的对象。这个模式适用于某个对象的构建过程复杂的情况下使用。
- 由于实现了构建和装配的解耦,不同的构建器(Builder),相同的装配顺序(Director),可以做出不同的对象;相同的构建器,不同的装配顺序也可以做出不同的对象。也就是实现了构建算法、装配算法的解耦,实现了更好的复用。
代码角度的理解
具体的产品可以理解成一个Java Bean,它里面的属性也是由各个java对象组成。我们要在客户端直接生成这个Java Bean,首先有一个Builder类拿到这个Java Bean的各个属性,再有一个Director类将拿到的各个属性对Java Bean对象进行组装(调用Java Bean属性的各个set方法)
UML(Java Bean 是一个宇宙飞船)
image.png具体实现代码,参照UML图
java bean:
package com.amberweather.builder;
/**
*
* 一个java bean,每个属性都是一个类
* @author HT
*
*/
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;
}
}
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 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 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;
}
}
Buidler的接口:
package com.amberweather.builder;
/**
*
* java bean的各个属性获取
* @author HT
*
*/
public interface AirShipBuilder {
Engine buildeEngine();
OrbitalModule buildeOrbitalModule();
EscapeTower buildeEscapeTower();
}
组装接口:
package com.amberweather.builder;
/**
*
* 组装接口
* @author HT
*
*/
public interface AirShipDirector {
AirShip direcAirShip();
}
Builder 的具体实现:
package com.amberweather.builder;
/**
*
* java bean的各个属性获取的具体实现类
* @author Administrator
*
*/
public class AmberAirShipBuilder implements AirShipBuilder {
@Override
public Engine buildeEngine() {
System.out.println("构建发动机");
return new Engine("AmberWeather 发动机"); //可以和其他模式结合
}
@Override
public OrbitalModule buildeOrbitalModule() {
System.out.println("构建轨道舱");
return new OrbitalModule("AmberWeather 轨道舱");
}
@Override
public EscapeTower buildeEscapeTower() {
System.out.println("构建逃逸塔");
return new EscapeTower("AmberWeather 逃逸塔");
}
}
组装类Director的具体实现:
package com.amberweather.builder;
/**
* director组装类的具体实现
*
* @author Administrator
*
*/
public class AmberAirShipDirector implements AirShipDirector {
private AirShipBuilder builder;
public AmberAirShipDirector(AirShipBuilder builder) {
super();
this.builder = builder;
}
@Override
public AirShip direcAirShip() {
Engine engine = builder.buildeEngine();
OrbitalModule orbitalModule = builder.buildeOrbitalModule();
EscapeTower escapeTower = builder.buildeEscapeTower();
AirShip ariship = new AirShip();
ariship.setEngine(engine);
ariship.setOrbitalModule(orbitalModule);
ariship.setEscapeTower(escapeTower);
return ariship;
}
}
客户端代码:
package com.amberweather.builder;
public class client {
public static void main(String[] args) {
AirShipDirector director = new AmberAirShipDirector(new AmberAirShipBuilder());
AirShip airship = director.direcAirShip();
System.out.println(airship.getEngine().getName());
}
}