Builder
2019-11-11 14:10:40 0 举报
一张Builder设计模式图
作者其他创作
大纲/内容
ConcreateBuilder2
+ buildA(): void+ buildB(): void
Builder模式的UML图:
package com.designpatterns.builder.computer;/** * builder的执行者 */public class Director { private ComputerConfigBuilder computerConfigBuilder; public void setBuilder(ComputerConfigBuilder builder){ this.computerConfigBuilder = builder; } public void createComputer(){ computerConfigBuilder.setComputerId(); computerConfigBuilder.setCPU(); computerConfigBuilder.setMemery(); } public Computer getComputer(){ return computerConfigBuilder.getComputer(); }}
builder模式主要有4个角色:① 产品product;② 抽象的builder;③ builder的实现,这种实现可以有多种;④ 使用者director。
依赖
package com.designpatterns.builder.computer;/** * builder中的抽象方法实现:实现一台高配电脑 */public class HeightComputer implements ComputerConfigBuilder { private Computer computer; public HeightComputer(){ this.computer = new Computer(); } @Override public void setComputerId() { computer.setComputerId(02); } @Override public void setCPU() { computer.setCPU(\"i5\"); } @Override public void setMemery() { computer.setMemory(\"300M\"); } @Override public Computer getComputer() { return computer; }}
实现
Builder
可以用实现高配电脑的方式实现一台低配电脑
package com.designpatterns.builder.computer;/** * 抽象方法 */public interface ComputerConfigBuilder { void setComputerId(); void setCPU(); void setMemery(); Computer getComputer();}
Director
+ construct
Product
ConcreateBuilder1
图例说明
package com.designpatterns.builder.computer;/** * builder模式中的product。 */public class Computer { private int computerId; private String CPU; private String memory; public int getComputerId() { return computerId; } public void setComputerId(int computerId) { this.computerId = computerId; } public String getCPU() { return CPU; } public void setCPU(String CPU) { this.CPU = CPU; } public String getMemory() { return memory; } public void setMemory(String memory) { this.memory = memory; } @Override public String toString() { return \"Computer{\" + \"computerId=\" + computerId + \
聚合
对应代码的实现:
0 条评论
下一页