//产品对象类
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();
}
}
1建造模式-java
最新推荐文章于 2025-01-19 07:00:00 发布
1520

被折叠的 条评论
为什么被折叠?



