建造者模式

1. 什么是建造者模式

Builder 模式也叫建造者模式或者生成器模式,是由 GoF 提出的 23 种设计模式中的一种。

Builder 模式是一种对象创建型模式之一,用来隐藏复合对象的创建过程,它把复合对象的创建过程加以抽象,通过子类继承和重载的方式,动态地创建具有复合属性的对象。

2. 建造者模式的结构

07_建造者模式 - 图1

3. 建造者模式应用场景

  • 对象的创建:Builder 模式是为对象的。
  • 创建而设计的模式创建的是一个复合对象:被创建的对象为一个具有复合属性的复合对象。
  • 关注对象创建的各部分的创建过程:不同的工厂(这里指 builder 生成器)对产品属性有不同的创建方法。

4. 代码演示

4.1. 直接创建

  1. public class House {
  2. private String floor;
  3. private String wall;
  4. private String ceiling;
  5. public String getFloor() {
  6. return floor;
  7. }
  8. public void setFloor(String floor) {
  9. this.floor = floor;
  10. }
  11. public String getWall() {
  12. return wall;
  13. }
  14. public void setWall(String wall) {
  15. this.wall = wall;
  16. }
  17. public String getCeiling() {
  18. return ceiling;
  19. }
  20. public void setCeiling(String ceiling) {
  21. this.ceiling = ceiling;
  22. }
  23. @Override
  24. public String toString() {
  25. return "House{" +
  26. "floor='" + floor + '\'' +
  27. ", wall='" + wall + '\'' +
  28. ", ceiling='" + ceiling + '\'' +
  29. '}';
  30. }
  31. }
  1. House house = new House();
  2. house.setFloor("地板");
  3. house.setWall("墙");
  4. house.setCeiling("屋顶");
  5. System.out.println(house);

4.2. 使用建造者

  1. public interface HouseBuilder {
  2. void makeFloor();
  3. void makeWall();
  4. void makeCeiling();
  5. House getHouse();
  6. }
  1. public class PingFangBuilder implements HouseBuilder {
  2. House house = new House();
  3. @Override
  4. public void makeFloor() {
  5. house.setFloor("平房-->地板");
  6. }
  7. @Override
  8. public void makeWall() {
  9. house.setWall("平房-->墙");
  10. }
  11. @Override
  12. public void makeCeiling() {
  13. house.setCeiling("平房-->屋顶");
  14. }
  15. @Override
  16. public House getHouse() {
  17. return house;
  18. }
  19. }
  1. HouseBuilder builder = new PingFangBuilder();
  2. builder.makeFloor();
  3. builder.makeWall();
  4. builder.makeCeiling();
  5. House house = builder.getHouse();
  6. System.out.println(house);

4.3. 引入包工头

  1. public class HouseDirector {
  2. private final HouseBuilder builder;
  3. public HouseDirector(HouseBuilder builder) {
  4. this.builder = builder;
  5. }
  6. public void makeHouse() {
  7. builder.makeFloor();
  8. builder.makeWall();
  9. builder.makeCeiling();
  10. }
  11. }
  1. HouseBuilder builder = new PingFangBuilder();
  2. HouseDirector director = new HouseDirector(builder);
  3. director.makeHouse();
  4. House house = builder.getHouse();
  5. System.out.println(house);

4.4. 新增建造者

  1. public class GongYuBuilder implements HouseBuilder {
  2. House house = new House();
  3. @Override
  4. public void makeFloor() {
  5. house.setFloor("公寓-->地板");
  6. }
  7. @Override
  8. public void makeWall() {
  9. house.setWall("公寓-->墙");
  10. }
  11. @Override
  12. public void makeCeiling() {
  13. house.setCeiling("公寓-->屋顶");
  14. }
  15. @Override
  16. public House getHouse() {
  17. return house;
  18. }
  19. }
  1. HouseBuilder builder1 = new PingFangBuilder();
  2. HouseDirector1 director1 = new HouseDirector1(builder1);
  3. director1.makeHouse();
  4. House house = builder1.getHouse();
  5. System.out.println(house);
  6. HouseBuilder builder2 = new GongYuBuilder();
  7. HouseDirector1 director2 = new HouseDirector1(builder2);
  8. director2.makeHouse();
  9. House house2 = builder2.getHouse();
  10. System.out.println(house2);

4.5. 另一种包工头

  1. public class HouseDirector3 {
  2. public void makeHouse(HouseBuilder builder) {
  3. builder.makeFloor();
  4. builder.makeWall();
  5. builder.makeCeiling();
  6. }
  7. }
  1. HouseBuilder builder1 = new PingFangBuilder();
  2. HouseDirector2 director1 = new HouseDirector2();
  3. director1.makeHouse(builder1);
  4. House house = builder1.getHouse();
  5. System.out.println(house);
  6. HouseBuilder builder2 = new GongYuBuilder();
  7. HouseDirector2 director2 = new HouseDirector2();
  8. director2.makeHouse(builder2);
  9. House house2 = builder2.getHouse();
  10. System.out.println(house2);