组件很多,装配顺序不定
本质:
1,分离了对象子组件的单独构造(Builder负责)和装配(Director负责),从而可以构造出复杂的对象,这个模式适用于某个对象的构建过程复杂的情况下使用
2,实现了构建和装配的解耦
/**建造者用来得到各个组件
* @author 小帆敲代码
*
*/
public interface AirshipBuilder {
public Engine getngine();//可用工厂或者单例模式得到对应的组件
public EscapeTower getEscapeTower();
public ObitalMoudle getObitalMoudle();
}
public class FAirshipBuilder implements AirshipBuilder{
@Override
public Engine getngine() {
System.out.println("建造发动机");
return new Engine("F牌发动机");
}
public Engine getngine() {
System.out.println("建造发动机");
return new Engine("F牌发动机");
}
@Override
public EscapeTower getEscapeTower() {
System.out.println("建造逃生塔");
return new EscapeTower("F牌");
}
public EscapeTower getEscapeTower() {
System.out.println("建造逃生塔");
return new EscapeTower("F牌");
}
@Override
public ObitalMoudle getObitalMoudle() {
System.out.println("建造轨道舱");
return new ObitalMoudle("F牌轨道舱");
}
public ObitalMoudle getObitalMoudle() {
System.out.println("建造轨道舱");
return new ObitalMoudle("F牌轨道舱");
}
}
/**
* 装配组件
* @author 小帆敲代码
*
*/
public interface AirshipDirector {
//装配
public Airship directAirship();
}
* 装配组件
* @author 小帆敲代码
*
*/
public interface AirshipDirector {
//装配
public Airship directAirship();
}
public class FAirshipDirector implements AirshipDirector{
private AirshipBuilder builder;
public FAirshipDirector(AirshipBuilder builder) {
this.builder = builder;
}
public FAirshipDirector(AirshipBuilder builder) {
this.builder = builder;
}
@Override
public Airship directAirship() {
Engine e=builder.getngine();
EscapeTower et=builder.getEscapeTower();
ObitalMoudle o=builder.getObitalMoudle();
//装配
Airship airship=new Airship();
airship.setEngine(e);
airship.setEscapeTower(et);
airship.setObtalMoudle(o);
return airship;
}
public Airship directAirship() {
Engine e=builder.getngine();
EscapeTower et=builder.getEscapeTower();
ObitalMoudle o=builder.getObitalMoudle();
//装配
Airship airship=new Airship();
airship.setEngine(e);
airship.setEscapeTower(et);
airship.setObtalMoudle(o);
return airship;
}
}
public class Airship {
private Engine engine;//发动机
private ObitalMoudle obtalMoudle;//轨道舱
private EscapeTower escapeTower;//逃生塔
public void lauch() {
System.out.println("发射!!!");
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public ObitalMoudle getObtalMoudle() {
return obtalMoudle;
}
public void setObtalMoudle(ObitalMoudle obtalMoudle) {
this.obtalMoudle = obtalMoudle;
}
public EscapeTower getEscapeTower() {
return escapeTower;
}
public void setEscapeTower(EscapeTower escapeTower) {
this.escapeTower = escapeTower;
}
}
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;
}
}
class ObitalMoudle{
private String name;
public ObitalMoudle(String name) {
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) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
private Engine engine;//发动机
private ObitalMoudle obtalMoudle;//轨道舱
private EscapeTower escapeTower;//逃生塔
public void lauch() {
System.out.println("发射!!!");
}
public Engine getEngine() {
return engine;
}
public void setEngine(Engine engine) {
this.engine = engine;
}
public ObitalMoudle getObtalMoudle() {
return obtalMoudle;
}
public void setObtalMoudle(ObitalMoudle obtalMoudle) {
this.obtalMoudle = obtalMoudle;
}
public EscapeTower getEscapeTower() {
return escapeTower;
}
public void setEscapeTower(EscapeTower escapeTower) {
this.escapeTower = escapeTower;
}
}
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;
}
}
class ObitalMoudle{
private String name;
public ObitalMoudle(String name) {
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) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Client {
public static void main(String[] args) {
FAirshipDirector f=new FAirshipDirector(new FAirshipBuilder());
Airship airship=f.directAirship();
System.out.println(airship.getEngine().getName());
airship.lauch();
}
FAirshipDirector f=new FAirshipDirector(new FAirshipBuilder());
Airship airship=f.directAirship();
System.out.println(airship.getEngine().getName());
airship.lauch();
}
}