依据来源于《Java300集》
场景
- 我们要建造一个复杂的产品,神州飞船,iphone。这个复杂的产品的创建。有这样一个问题需要处理:
- 装配这些子组件是不是有个步骤问题
- 实际开发中,我们所需要的对象构建时,也非常复杂,有很多步骤需要处理。
建造模式的本质
- 分离了对象子组件的单独构造(由Builder来负责)和装配(由Director负责)。从而可以构造出复杂的对象。这个模式适用于:某个对象额构建过程复杂的情况下使用。
- 由于实现了构造和装配的解耦。不同的构造器,相同的配置,也可以做出不同的对象;相同的构造器,不同的装配顺序也可以做出不同的对象,也就是实现了构建算法、装配算法的解耦,实现了更好的复用。
个人理解:
- 构造+装配之间解耦
- 构造一个对象,要想这个对象完整,就要不停的去完善它(一部车,车架子有了,还有座椅,轮胎,发动机等等),需要很多零碎的信息;
- 这些零碎信息的需求,导致了构造者的横空出世;构造者做了一个抽象,收集了一个完整零碎信息的集合。(举个例子:县长要盖一栋楼,办公室主任就往负责一个构造,他列出一张清单,需要地皮,房子支柱,盖房材料;一个乡长知道了,就准备了他们家坟地旁边的一块地,给了几根破木头,推了两车吐,这要盖个啥样的房子,就由建筑工程师来组装了;另一个乡长就准备了他们家在县里买的地皮,提供的钢筋,水泥,至于盖成啥房子,也是建筑工程师来组装);
- 零碎的信息提供好了,就需要进行装配了,装配的结果是一个完整的对象,而装配就需要将零碎信息提供过来;
栗子:
package com.principle.builder;
/**
* 新房子
*
* @author xy
*
*/
public class NewHouse {
private Place place;
private Support support;
private Material material;
public Place getPlace() {
return place;
}
public void setPlace(Place place) {
this.place = place;
}
public Support getSupport() {
return support;
}
public void setSupport(Support support) {
this.support = support;
}
public Material getMaterial() {
return material;
}
public void setMaterial(Material material) {
this.material = material;
}
public void build() {
System.out.println("新房子盖好了,xxx可以潜规则秘书了");
}
}
class Place {
private String name;
public Place(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Support {
private String name;
public Support(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class Material {
private String name;
public Material(String name) {
super();
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.principle.builder;
public interface NewHouseBuilder {
Place builderPlace();
Support builderSupport();
Material builderMaterial();
}
package com.principle.builder;
public interface NewHouseDirector {
NewHouse createNewHouse();
}
package com.principle.builder;
public class LinderWangNewHouseBuilder implements NewHouseBuilder {
@Override
public Place builderPlace() {
return new Place("坟地旁边");
}
@Override
public Support builderSupport() {
return new Support("几根破棍子");
}
@Override
public Material builderMaterial() {
return new Material("几车土");
}
}
package com.principle.builder;
public class EngineerLiNewHouseDirector implements NewHouseDirector {
private NewHouseBuilder builder;
public EngineerLiNewHouseDirector(NewHouseBuilder builder) {
this.builder = builder;
}
@Override
public NewHouse createNewHouse() {
NewHouse newHouse = new NewHouse();
newHouse.setPlace(builder.builderPlace());
newHouse.setSupport(builder.builderSupport());
newHouse.setMaterial(builder.builderMaterial());
return newHouse;
}
}
package com.principle.builder;
public class Client {
public static void main(String[] args) {
EngineerLiNewHouseDirector director=new EngineerLiNewHouseDirector(new LinderWangNewHouseBuilder());
NewHouse newHouse=director.createNewHouse();
newHouse.build();
}
}
打印结果:
新房子盖好了,xxx可以潜规则秘书了
builder.png
网友评论