首页 > 代码库 > 设计模式(三)建造者模式

设计模式(三)建造者模式

建造模式的本质:

——分离了对象子组件的单独构造(由Builder来负责)和装配(由Director负责)。从而可以构造出复杂的对象。这个模式适用于:某个对象的构建过程复杂的情况下使用。

——由于实现了构建和装配的解耦。不同的构建器,相同的装配,也可以做出不同的对象;相同的构建器,不同的装配顺序也可以做出不同的对象。也就是实现了构建算法、装配算法的解耦,实现了更好的复用。

 

例:

1. 定义飞船类以及发动机、轨道舱、逃逸塔类:

 1 package com.ztq.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     public OrbitalModule getOrbitalModule() {
12         return orbitalModule;
13     }
14     public void setOrbitalModule(OrbitalModule orbitalModule) {
15         this.orbitalModule = orbitalModule;
16     }
17     public Engine getEngine() {
18         return engine;
19     }
20     public void setEngine(Engine engine) {
21         this.engine = engine;
22     }
23     public EscapeTower getEscapeTower() {
24         return escapeTower;
25     }
26     public void setEscapeTower(EscapeTower escapeTower) {
27         this.escapeTower = escapeTower;
28     }
29     
30 }
31 
32 class OrbitalModule{
33     private String name;
34     public OrbitalModule(String name){
35         this.name = name;
36     }
37     public String getName() {
38         return name;
39     }
40 
41     public void setName(String name) {
42         this.name = name;
43     }
44 }
45 
46 class Engine{
47     private String name;
48     
49     public Engine(String name){
50         this.name = name;
51     }
52     
53     public String getName() {
54         return name;
55     }
56 
57     public void setName(String name) {
58         this.name = name;
59     }
60 }
61 
62 class EscapeTower{
63     private String name;
64     
65     public EscapeTower(String name){
66         this.name = name;
67     }
68     public String getName() {
69         return name;
70     }
71 
72     public void setName(String name) {
73         this.name = name;
74     }
75 }

 

2. 定义AirShipBuilder接口:

1 public interface AirShipBuilder {
2     Engine builderEngine();
3     OrbitalModule builderOrbitalModule();
4     EscapeTower builderEscapeTower();
5 }

 

3. 定义AirShipDirector接口:

1 public interface AirShipDirector {
2     /***
3      * 组装飞船对象
4      * 
5      */
6     AirShip directAirShip();
7 }

 

4. 定义实现AirShipBuilder接口的类:

 1 public class ZtqAirShipBuilder implements AirShipBuilder{
 2 
 3     @Override
 4     public Engine builderEngine() {
 5         System.out.println("构建ztq牌发动机!");
 6         return new Engine("ztq牌发动机");
 7     }
 8 
 9     @Override
10     public OrbitalModule builderOrbitalModule() {
11         System.out.println("构建ztq牌轨道舱!");
12         return new OrbitalModule("ztq牌轨道舱");
13     }
14 
15     @Override
16     public EscapeTower builderEscapeTower() {
17         System.out.println("构建ztq牌逃逸塔!");
18         return new EscapeTower("ztq牌逃逸塔");
19     }
20 }

 

5. 定义实现AirShipDirector接口的类:

 1 public class ZtqAirShipDirector implements AirShipDirector{
 2     
 3     private AirShipBuilder builder;
 4     
 5     public ZtqAirShipDirector(AirShipBuilder builder) {
 6         this.builder = builder;
 7     }
 8     
 9     @Override
10     public AirShip directAirShip(){
11         Engine e = builder.builderEngine();
12         OrbitalModule o = builder.builderOrbitalModule();
13         EscapeTower es = builder.builderEscapeTower();
14         
15         AirShip ship = new AirShip();
16         ship.setEngine(e);
17         ship.setOrbitalModule(o);
18         ship.setEscapeTower(es);
19         
20         return ship;
21     }
22 }

 

6. 定义测试类:

1 public class Client {
2     public static void main(String[] args) {
3         AirShipDirector director = new ZtqAirShipDirector(new ZtqAirShipBuilder());
4         AirShip ship = director.directAirShip();
5         System.out.println(ship.getEngine().getName());
6         ship.launch();
7     }
8 }

打印结果:

构建ztq牌发动机!
构建ztq牌轨道舱!
构建ztq牌逃逸塔!
ztq牌发动机
发射!

 

UML图:

技术分享

 

设计模式(三)建造者模式