1. 定义

将一个复杂对象的创建与它的表示分离,使得同样的构建过程可以创建不同的表示。

2. 实现

2.1 Product

  1. class Product {
  2. private String productName;
  3. private String companyName;
  4. private String part1;
  5. private String part2;
  6. private String part3;
  7. private String part4;
  8. public Product() {
  9. }
  10. public Product(String productName, String companyName, String part1, String part2, String part3, String part4) {
  11. this.productName = productName;
  12. this.companyName = companyName;
  13. this.part1 = part1;
  14. this.part2 = part2;
  15. this.part3 = part3;
  16. this.part4 = part4;
  17. }
  18. public String getProductName() {
  19. return productName;
  20. }
  21. public void setProductName(String productName) {
  22. this.productName = productName;
  23. }
  24. public String getCompanyName() {
  25. return companyName;
  26. }
  27. public void setCompanyName(String companyName) {
  28. this.companyName = companyName;
  29. }
  30. public String getPart1() {
  31. return part1;
  32. }
  33. public void setPart1(String part1) {
  34. this.part1 = part1;
  35. }
  36. public String getPart2() {
  37. return part2;
  38. }
  39. public void setPart2(String part2) {
  40. this.part2 = part2;
  41. }
  42. public String getPart3() {
  43. return part3;
  44. }
  45. public void setPart3(String part3) {
  46. this.part3 = part3;
  47. }
  48. public String getPart4() {
  49. return part4;
  50. }
  51. public void setPart4(String part4) {
  52. this.part4 = part4;
  53. }
  54. }

2.2 ProductBuilder

  1. interface ProductBuilder {
  2. void builderProductName(String productName);
  3. void builderCompanyName(String companyName);
  4. void builderPart1(String part1);
  5. void builderPart2(String part2);
  6. void builderPart3(String part3);
  7. void builderPart4(String part4);
  8. Product build();
  9. }

2.3 DefaultConcreteProductBuilder

  1. class DefaultConcreteProductBuilder implements ProductBuilder {
  2. private String productName;
  3. private String companyName;
  4. private String part1;
  5. private String part2;
  6. private String part3;
  7. private String part4;
  8. @Override
  9. public void builderProductName(String productName) {
  10. this.productName = productName;
  11. }
  12. @Override
  13. public void builderCompanyName(String companyName) {
  14. this.companyName = companyName;
  15. }
  16. @Override
  17. public void builderPart1(String part1) {
  18. this.part1 = part1;
  19. }
  20. @Override
  21. public void builderPart2(String part2) {
  22. this.part2 = part2;
  23. }
  24. @Override
  25. public void builderPart3(String part3) {
  26. this.part3 = part3;
  27. }
  28. @Override
  29. public void builderPart4(String part4) {
  30. this.part4 = part4;
  31. }
  32. @Override
  33. public Product build() {
  34. return new Product(this.productName, this.companyName, this.part1, this.part2, this.part3, this.part4);
  35. }
  36. }

2.4 SpecialConcreteProductBuilder

  1. class SpecialConcreteProductBuilder implements ProductBuilder {
  2. private String productName;
  3. private String companyName;
  4. private String part1;
  5. private String part2;
  6. private String part3;
  7. private String part4;
  8. @Override
  9. public void builderProductName(String productName) {
  10. this.productName = productName;
  11. }
  12. @Override
  13. public void builderCompanyName(String companyName) {
  14. this.companyName = companyName;
  15. }
  16. @Override
  17. public void builderPart1(String part1) {
  18. this.part1 = part1;
  19. }
  20. @Override
  21. public void builderPart2(String part2) {
  22. this.part2 = part2;
  23. }
  24. @Override
  25. public void builderPart3(String part3) {
  26. this.part3 = part3;
  27. }
  28. @Override
  29. public void builderPart4(String part4) {
  30. this.part4 = part4;
  31. }
  32. @Override
  33. public Product build() {
  34. return new Product(this.productName, this.companyName, this.part1, this.part2, this.part3, this.part4);
  35. }
  36. }

2.5 Director

  1. class Director {
  2. private ProductBuilder builder;
  3. public Director(ProductBuilder builder) {
  4. this.builder = builder;
  5. }
  6. public Product makeProduct(String productName, String companyName, String part1, String part2, String part3, String part4) {
  7. builder.builderProductName(productName);
  8. builder.builderCompanyName(companyName);
  9. builder.builderPart1(part1);
  10. builder.builderPart2(part2);
  11. builder.builderPart3(part3);
  12. builder.builderPart4(part4);
  13. Product product = builder.build();
  14. return product;
  15. }
  16. }

2.6 使用

  1. public static void main(String[] args) {
  2. SpecialConcreteProductBuilder specialConcreteProductBuilder = new SpecialConcreteProductBuilder();
  3. Director director = new Director(specialConcreteProductBuilder);
  4. Product product = director.makeProduct("productName1", "com...", "part1", "part2", "part3", "part4");
  5. System.out.println(product);
  6. }

3. 建造者模式与不可变对象配合使用

3.1 Product

  1. /不可变对象,属性定义为final
  2. class Product {
  3. private final String productName;
  4. private final String companyName;
  5. private final String part1;
  6. private final String part2;
  7. private final String part3;
  8. private final String part4;
  9. public Product(String productName, String companyName, String part1, String part2, String part3, String part4) {
  10. this.productName = productName;
  11. this.companyName = companyName;
  12. this.part1 = part1;
  13. this.part2 = part2;
  14. this.part3 = part3;
  15. this.part4 = part4;
  16. }
  17. // 内部类实现构造器
  18. static class Builder {
  19. private String productName;
  20. private String companyName;
  21. private String part1;
  22. private String part2;
  23. private String part3;
  24. private String part4;
  25. public Builder productName(String productName) {
  26. this.productName = productName;
  27. return this;//返回自己使用时可以用链式
  28. }
  29. public Builder companyName(String companyName) {
  30. this.companyName = companyName;
  31. return this;
  32. }
  33. public Builder part1(String part1) {
  34. this.part1 = part1;
  35. return this;
  36. }
  37. public Builder part2(String part2) {
  38. this.part2 = part2;
  39. return this;
  40. }
  41. public Builder part3(String part3) {
  42. this.part3 = part3;
  43. return this;
  44. }
  45. public Builder part4(String part4) {
  46. this.part4 = part4;
  47. return this;
  48. }
  49. public Product build() {
  50. return new Product(this.productName, this.companyName, this.part1, this.part2, this.part3, this.part4);
  51. }
  52. }
  53. }

3.2 使用

  1. public static void main(String[] args) {
  2. Product.Builder builder = new Product.Builder().productName("productName1").companyName("com.").part1("part1").part2("part2");
  3. builder.part3("part3").part4("part4");
  4. Product product = builder.build();
  5. System.out.println(product);
  6. }

4. 应用场景

  1. 需要生成的对象具有复杂的内部结构
  2. 需要生成的对象内部属性本身相互依赖
  3. 与不可变对象配合使用

    5. 优点

  4. 建造者独立,易扩展。

  5. 便于控制细节风险。

    6. Spring源码中应用

    1. org.springframework.web.servlet.mvc.method.RequestMappingInfo
    2. org.springframework.beans.factory.support.BeanDefinitionBuilder