一、建造者模式概述
建造者模式的本质:
1.分离了对象子组件的单独构造(由Builder负责)和装配(由Director负责)。从而可以构造出复杂的对象。这个模式适用于:某个对象的过程复杂的情况下使用。
2.由于实现了构建和装配的解耦。不同的构建器,相同的装配,也可以做出不同的对象;相同的构建器,不同的装配顺序也可以做出不同的对象。也就是实现了构建算法,装配算法的解耦,实现了更好的复用。
二、建造者模式
1.创建飞船部件及飞船
1 /** 2 * 飞船 3 * @author CL 4 * 5 */ 6 public class Airship { 7 private Engine engine; //发动机 8 private OrbitalMouble orbitalMouble; //轨道舱 9 private EscapeTower escapeTower; //逃逸塔 10 11 public void launch() { 12 System.out.println("发射!"); 13 } 14 15 public Engine getEngine() { 16 return engine; 17 } 18 public void setEngine(Engine engine) { 19 this.engine = engine; 20 } 21 public OrbitalMouble getOrbitalMouble() { 22 return orbitalMouble; 23 } 24 public void setOrbitalMouble(OrbitalMouble orbitalMouble) { 25 this.orbitalMouble = orbitalMouble; 26 } 27 public EscapeTower getEscapeTower() { 28 return escapeTower; 29 } 30 public void setEscapeTower(EscapeTower escapeTower) { 31 this.escapeTower = escapeTower; 32 } 33 34 } 35 36 /** 37 * 发动机类 38 */ 39 class Engine { 40 private String name; 41 42 public Engine(String name) { 43 this.name = name; 44 } 45 46 public String getName() { 47 return name; 48 } 49 50 public void setName(String name) { 51 this.name = name; 52 } 53 54 } 55 56 /** 57 * 轨道舱类 58 */ 59 class OrbitalMouble { 60 private String name; 61 62 public OrbitalMouble(String name) { 63 this.name = name; 64 } 65 66 public String getName() { 67 return name; 68 } 69 70 public void setName(String name) { 71 this.name = name; 72 } 73 } 74 75 /** 76 * 逃逸塔类 77 */ 78 class EscapeTower { 79 private String name; 80 81 public EscapeTower(String name) { 82 this.name = name; 83 } 84 85 public String getName() { 86 return name; 87 } 88 89 public void setName(String name) { 90 this.name = name; 91 } 92 }
2.创建飞船的构建接口和装配接口
1 /** 2 * 飞船的构建者 3 * @author CL 4 * 5 */ 6 public interface AirshipBuilder { 7 /** 8 * 构建发动机 9 * @return 10 */ 11 Engine builderEngine(); 12 /** 13 * 构建轨道舱 14 * @return 15 */ 16 OrbitalMouble builderOrbitalMouble(); 17 /** 18 * 构建逃逸塔 19 * @return 20 */ 21 EscapeTower builderEscapeTower(); 22 }
1 /** 2 * 飞船的装配者 3 * @author CL 4 * 5 */ 6 public interface AirshipDirector { 7 /** 8 * 装配飞船 9 * @return 10 */ 11 Airship directorAirship(); 12 }
3.创建构建接口和装配接口的实现类
1 /** 2 * XAAU牌飞船构建者的实现类 3 * @author CL 4 * 5 */ 6 public class XAAUAirshipBuilder implements AirshipBuilder { 7 8 public Engine builderEngine() { 9 System.out.println("构建发动机"); 10 return new Engine("XAAU牌发动机"); 11 } 12 13 public OrbitalMouble builderOrbitalMouble() { 14 System.out.println("构建轨道舱"); 15 return new OrbitalMouble("XAAU牌轨道舱"); 16 } 17 18 public EscapeTower builderEscapeTower() { 19 System.out.println("构建逃逸塔"); 20 return new EscapeTower("XAAU牌逃逸塔"); 21 } 22 23 }
1 /** 2 * XAAU牌飞船装配者的实现类 3 * @author CL 4 * 5 */ 6 public class XAAUAirshipDirector implements AirshipDirector { 7 8 private AirshipBuilder builder; 9 10 public XAAUAirshipDirector(AirshipBuilder builder) { 11 this.builder = builder; 12 } 13 14 public Airship directorAirship() { 15 //构建飞船部件 16 Engine e = builder.builderEngine(); 17 OrbitalMouble o = builder.builderOrbitalMouble(); 18 EscapeTower et = builder.builderEscapeTower(); 19 20 //装配成飞船对象 21 Airship ship = new Airship(); 22 ship.setEngine(e); 23 ship.setOrbitalMouble(o); 24 ship.setEscapeTower(et); 25 26 return ship; 27 } 28 29 }
4.测试
1 /** 2 * 利用建造者模式创建XAAU牌飞船对象的实例 3 * @author CL 4 * 5 */ 6 public class Client { 7 8 public static void main(String[] args) { 9 AirshipDirector director = new XAAUAirshipDirector(new XAAUAirshipBuilder()); 10 11 Airship ship = director.directorAirship(); 12 13 System.out.println(ship.getEngine().getName()); 14 System.out.println(ship.getOrbitalMouble().getName()); 15 System.out.println(ship.getEscapeTower().getName()); 16 17 ship.launch(); 18 } 19 }
控制台输出:
构建发动机
构建轨道舱
构建逃逸塔
XAAU牌发动机
XAAU牌轨道舱
XAAU牌逃逸塔
发射!
三、建造者模式常见应用场景
(1)StringBuilder类中的append方法;
(2)SQL中的PreparedStatement;
(3)JDOM中,DomBuilder、SAXBuilder;
(4)………………