创建者模式:
分离对象子组件的单独构造(由Builder来负责)和装配(由Director负责),对象的构造比较复杂时使用 该模式。
类图:
Builder 抽象建造者接口,规范各个组成部分的构建。
ConcreteBuilder 具体建造者角色,实现组成部分的构建,并提供示例。
Product 产品角色,创建返回的对象。
Director 导演者角色,负责创建以及使用。
优点:
由于实现了构建和装配的解耦。不同的构建器,相同的装配也可以做成不同的对象,相同的构建器,不同的装配顺序也可以生产成不同的对象。也就是实现了构造算法和装配算法的解耦,实现了更好的复用。
应用场景:
StringBuilder类的append方法。
SQL中的PreparedStatement
JDOM中,DomBuilder SAXBuilder
创建者模式Demo
1 package builder.domain;
2
3 /**
4 * 宇宙飞船
5 *
6 */
7 public class AirShip {
8 private OrbitalModule orbitalModule; //轨道舱
9 private Engine engine; //发动机
10 private EscapeTower escapeTower; //逃逸塔
11
12
13 public void launch(){
14 System.out.println("发射!");
15 }
16
17
18 public OrbitalModule getOrbitalModule() {
19 return orbitalModule;
20 }
21
22
23 public void setOrbitalModule(OrbitalModule orbitalModule) {
24 this.orbitalModule = orbitalModule;
25 }
26
27
28 public Engine getEngine() {
29 return engine;
30 }
31
32
33 public void setEngine(Engine engine) {
34 this.engine = engine;
35 }
36
37
38 public EscapeTower getEscapeTower() {
39 return escapeTower;
40 }
41
42
43 public void setEscapeTower(EscapeTower escapeTower) {
44 this.escapeTower = escapeTower;
45 }
46 }
47
48
49 package builder.domain;
50
51 public class Engine {
52 private String name;
53
54 public Engine(String name) {
55 this.name = name;
56 }
57
58 public String getName() {
59 return name;
60 }
61
62 public void setName(String name) {
63 this.name = name;
64 }
65 }
66
67
68 package builder.domain;
69
70 public class EscapeTower {
71 private String name;
72
73 public String getName() {
74 return name;
75 }
76
77 public void setName(String name) {
78 this.name = name;
79 }
80
81 public EscapeTower(String name) {
82 super();
83 this.name = name;
84 }
85 }
86
87
88 package builder.domain;
89
90 public class OrbitalModule {
91 private String name;
92
93 public OrbitalModule(String name)
94 {
95 this.name = name;
96 }
97
98 public String getName() {
99 return name;
100 }
101
102 public void setName(String name) {
103 this.name = name;
104 }
105
106
107 }
108
109
110 package builder.service;
111
112 import builder.domain.Engine;
113 import builder.domain.EscapeTower;
114 import builder.domain.OrbitalModule;
115
116 public interface IAirShipBuilder {
117 Engine builderEngine();
118 OrbitalModule builderOrbitalModule();
119 EscapeTower builderEscapeTower();
120 }
121
122
123 package builder.service;
124
125 import builder.domain.Engine;
126 import builder.domain.EscapeTower;
127 import builder.domain.OrbitalModule;
128
129 public class AirShipBuilderImpl implements IAirShipBuilder {
130 //StringBuilder, 以后学习XML解析中,JDOM库中的类:DomBuilder,SaxBuilder
131 @Override
132 public Engine builderEngine() {
133 // TODO Auto-generated method stub
134 System.out.println("构建发动机!");
135 return new Engine("发动机!");
136 }
137
138 @Override
139 public OrbitalModule builderOrbitalModule() {
140 // TODO Auto-generated method stub
141 System.out.println("构建轨道舱");
142 return new OrbitalModule("轨道舱");
143 }
144
145 @Override
146 public EscapeTower builderEscapeTower() {
147 // TODO Auto-generated method stub
148 System.out.println("构建逃逸塔");
149 return new EscapeTower("逃逸塔");
150 }
151
152 }
153
154
155 package builder;
156
157 import builder.domain.AirShip;
158
159 public interface IAirShipDirector {
160 /**
161 * 组装飞船对象
162 * @return
163 */
164 AirShip directAirShip();
165 }
166
167
168 package builder;
169
170 import builder.domain.AirShip;
171 import builder.domain.Engine;
172 import builder.domain.EscapeTower;
173 import builder.domain.OrbitalModule;
174 import builder.service.IAirShipBuilder;
175
176 public class AirshipDirectorImpl implements IAirShipDirector {
177 private IAirShipBuilder builder;
178
179 public AirshipDirectorImpl(IAirShipBuilder builder) {
180 this.builder = builder;
181 }
182
183 @Override
184 public AirShip directAirShip() {
185 // TODO Auto-generated method stub
186 Engine e = builder.builderEngine();
187 OrbitalModule o = builder.builderOrbitalModule();
188 EscapeTower et = builder.builderEscapeTower();
189
190 //装配成飞船对象
191 AirShip ship = new AirShip();
192 ship.setEngine(e);
193 ship.setEscapeTower(et);
194 ship.setOrbitalModule(o);
195
196 return ship;
197 }
198
199 }
200
201 package builder;
202
203 import builder.domain.AirShip;
204 import builder.service.AirShipBuilderImpl;
205
206 public class Client {
207
208 public static void main(String[] args) {
209 // TODO Auto-generated method stub
210 IAirShipDirector director = new AirshipDirectorImpl(new AirShipBuilderImpl());
211
212 AirShip ship = director.directAirShip();
213
214 System.out.println(ship.getEngine().getName());
215
216 ship.launch();
217 }
218
219 }