1建造模式-java


//产品对象类
package com.mfz.builder;

public class AirShip {
    private OrbitalModule orbitalModule; //轨道仓
    private Engine engine; //发动机
    private EscapeTower escapeTower; //逃逸塔

    public AirShip() {
    }

    public AirShip(OrbitalModule orbitalModule, Engine engine, EscapeTower escapeTower) {
        this.orbitalModule = orbitalModule;
        this.engine = engine;
        this.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;
    }

    public void lunch(){
        System.out.println("加载(1)->"+this.engine.getName());
        System.out.println("加载(2)->"+this.escapeTower.getName());
        System.out.println("加载(3)->"+this.orbitalModule.getName());
        System.out.println("加载完成!\n");
        System.out.println("------发射火箭------");
    }
}

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




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 EscapeTower{
    private String name;

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

    public String getName() {
        return name;
    }

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

//构建者对象接口
package com.mfz.builder;

public interface AirShipBuilder {

    Engine buildEngine();
    OrbitalModule buildOrbitalModule();
    EscapeTower buildEscapeTower();
}

//构建者接口实现类型
package com.mfz.builder;

public class LemonAirShipBuilder implements AirShipBuilder {

    @Override
    public Engine buildEngine() {
        return new Engine("柠檬发动机");
    }

    @Override
    public OrbitalModule buildOrbitalModule() {
        return new OrbitalModule("柠檬轨道仓");
    }

    @Override
    public EscapeTower buildEscapeTower() {
        return new EscapeTower("柠檬逃逸塔");
    }
}


//组装者接口
package com.mfz.builder;

public interface AirShipDirector {
    AirShip directoryShip(); //组装飞般对象
}



//组装者接口实现类

package com.mfz.builder;

  public class LemonAirShipDirectory implements AirShipDirector {

    private AirShipBuilder builder;

    public LemonAirShipDirectory(AirShipBuilder builder) {
        this.builder = builder;
    }

    public LemonAirShipDirectory() {
    }

    public AirShipBuilder getBuilder() {
        return builder;
    }

    public void setBuilder(AirShipBuilder builder) {
        this.builder = builder;
    }

    @Override
    public AirShip directoryShip() {

        AirShip air = new AirShip();
        air.setEngine(builder.buildEngine());
        air.setEscapeTower(builder.buildEscapeTower());
        air.setOrbitalModule(builder.buildOrbitalModule());
        return air;
    }
}


//调用者
package com.mfz.builder;

public class client {

    public static void main(String[] args){

        AirShipDirector director = new LemonAirShipDirectory(new LemonAirShipBuilder());
        director.directoryShip().lunch();

    }
}





评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值