package com.bjsxt.builder; /** * 尚学堂牌宇宙飞船 * @author 尚学堂高淇 www.sxt.cn * */ public class AirShip { private OrbitalModule orbitalModule; //轨道舱 private Engine engine; //发动机 private EscapeTower escapeTower; //逃逸塔 public void launch(){ System.out.println("发射!"); } 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) { 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 String getName() { return name; } public void setName(String name) { this.name = name; } public EscapeTower(String name) { super(); this.name = name; } }AirShip
package com.bjsxt.builder; public interface AirShipBuilder { Engine builderEngine(); OrbitalModule builderOrbitalModule(); EscapeTower builderEscapeTower(); }AirShipBuilder
package com.bjsxt.builder; public class SxtAirShipBuilder implements AirShipBuilder { //StringBuilder, 以后学习XML解析中,JDOM库中的类:DomBuilder,SaxBuilder @Override public Engine builderEngine() { System.out.println("构建尚学堂牌发动机!"); return new Engine("尚学堂牌发动机!"); } @Override public EscapeTower builderEscapeTower() { System.out.println("构建逃逸塔"); return new EscapeTower("尚学堂牌逃逸塔"); } @Override public OrbitalModule builderOrbitalModule() { System.out.println("构建轨道舱"); return new OrbitalModule("尚学堂牌轨道舱"); } }SxtAirShipBuilder
package com.bjsxt.builder; public interface AirShipDirector { /** * 组装飞船对象 * @return */ AirShip directAirShip(); }AirShipDirector
package com.bjsxt.builder; public class SxtAirshipDirector implements AirShipDirector { private AirShipBuilder builder; public SxtAirshipDirector(AirShipBuilder builder) { this.builder = builder; } @Override public AirShip directAirShip() { Engine e = builder.builderEngine(); OrbitalModule o = builder.builderOrbitalModule(); EscapeTower et = builder.builderEscapeTower(); //装配成飞船对象 AirShip ship = new AirShip(); ship.setEngine(e); ship.setEscapeTower(et); ship.setOrbitalModule(o); return ship; } }SxtAirshipDirecto
package com.bjsxt.builder; public class Client { public static void main(String[] args) { AirShipDirector director = new SxtAirshipDirector(new SxtAirShipBuilder()); AirShip ship = director.directAirShip(); System.out.println(ship.getEngine().getName()); ship.launch(); } }Client
标签:Engine,java23,return,String,创建者,builder,设计模式,public,name From: https://www.cnblogs.com/ou-pc/p/17288970.html