来源:http://www.bjsxt.com/
一、【GOF23设计模式】建造者模式详解类图关系
建造飞船
1 package com.test.Builder; 2 3 public class AirShip { 4 private OrbitalModule orbitalModule;//轨道舱 5 private Engine engine;//发动机 6 private EscapeTower escapeTower;//逃逸塔 7 8 public void launch(){ 9 System.out.println("发射!"); 10 } 11 12 public OrbitalModule getOrbitalModule() { 13 return orbitalModule; 14 } 15 public void setOrbitalModule(OrbitalModule orbitalModule) { 16 this.orbitalModule = orbitalModule; 17 } 18 public Engine getEngine() { 19 return engine; 20 } 21 public void setEngine(Engine engine) { 22 this.engine = engine; 23 } 24 public EscapeTower getEscapeTower() { 25 return escapeTower; 26 } 27 public void setEscapeTower(EscapeTower escapeTower) { 28 this.escapeTower = escapeTower; 29 } 30 } 31 32 class OrbitalModule{ 33 private String name; 34 35 public OrbitalModule(String name) { 36 super(); 37 this.name = name; 38 } 39 40 public String getName() { 41 return name; 42 } 43 44 public void setName(String name) { 45 this.name = name; 46 } 47 } 48 class Engine{ 49 private String name; 50 51 public Engine(String name) { 52 super(); 53 this.name = name; 54 } 55 56 public String getName() { 57 return name; 58 } 59 60 public void setName(String name) { 61 this.name = name; 62 } 63 } 64 class EscapeTower{ 65 private String name; 66 67 public EscapeTower(String name) { 68 super(); 69 this.name = name; 70 } 71 72 public String getName() { 73 return name; 74 } 75 76 public void setName(String name) { 77 this.name = name; 78 } 79 }
构造接口
1 package com.test.Builder; 2 3 public interface AirShipBuilder { 4 OrbitalModule buildOrbitalModule(); 5 Engine builderEngine(); 6 EscapeTower builderEscapeTower(); 7 }
组装接口
1 package com.test.Builder; 2 3 public interface AirShipDirector { 4 /** 5 * 组装飞船对象 6 */ 7 AirShip directAirShip(); 8 }
构造实现类
1 package com.test.Builder; 2 /** 3 * StringBuilder,以后学习XML解析中,JDOM库中的类:DomBuilder,SaxBuilder 4 */ 5 public class SxtAirShipBuilder implements AirShipBuilder{ 6 7 @Override 8 public OrbitalModule buildOrbitalModule() { 9 System.out.println("构建sxt轨道舱"); 10 return new OrbitalModule("sxt轨道舱"); 11 } 12 13 @Override 14 public Engine builderEngine() { 15 System.out.println("构建sxt发动机"); 16 return new Engine("sxt发动机"); 17 } 18 19 @Override 20 public EscapeTower builderEscapeTower() { 21 System.out.println("构建sxt逃逸塔"); 22 return new EscapeTower("sxt逃逸塔"); 23 } 24 }
组装实现类
1 package com.test.Builder; 2 3 public class SxtAirShipDirector implements AirShipDirector{ 4 5 private AirShipBuilder builder; 6 7 public SxtAirShipDirector(AirShipBuilder builder) { 8 super(); 9 this.builder = builder; 10 } 11 12 @Override 13 public AirShip directAirShip() { 14 OrbitalModule o = builder.buildOrbitalModule(); 15 Engine e = builder.builderEngine(); 16 EscapeTower et = builder.builderEscapeTower(); 17 18 AirShip ship = new AirShip(); 19 ship.setOrbitalModule(o); 20 ship.setEngine(e); 21 ship.setEscapeTower(et); 22 return ship; 23 } 24 }
1 package com.test.Builder; 2 3 public class Client { 4 public static void main(String[] args) { 5 AirShipDirector director = new SxtAirShipDirector(new SxtAirShipBuilder()); 6 AirShip ship = director.directAirShip(); 7 System.out.println(ship.getEngine().getName()); 8 ship.launch(); 9 } 10 }