1. 建造者(Builder)模式

06. 建造者(Builder)模式 - 图1

创建的东西细节复杂,还必须暴露给使用者。屏蔽过程而不屏蔽细节

产品角色(Product):Phone

  1. /**
  2. * 只暴露get方法 不暴露set方法
  3. */
  4. public class Phone {
  5. protected String cpu;
  6. protected String mem;
  7. protected String disk;
  8. protected String cam;
  9. public String getCpu() {
  10. return cpu;
  11. }
  12. public String getMem() {
  13. return mem;
  14. }
  15. public String getDisk() {
  16. return disk;
  17. }
  18. public String getCam() {
  19. return cam;
  20. }
  21. @Override
  22. public String toString() {
  23. return "Phone{" +
  24. "cpu='" + cpu + '\'' +
  25. ", mem='" + mem + '\'' +
  26. ", disk='" + disk + '\'' +
  27. ", cam='" + cam + '\'' +
  28. '}';
  29. }
  30. }

抽象建造者(Builder):AbstracPhoneBuilder

  1. public abstract class AbstractBuilder {
  2. Phone phone;
  3. abstract void customCpu(String cpu);
  4. abstract void customMem(String mem);
  5. abstract void customDisk(String disk);
  6. abstract void customCam(String cam);
  7. Phone getProduct() {
  8. return phone;
  9. }
  10. }

具体建造者(Concrete Builder):PhoneBuilder

  1. public class XiaomiBuilder extends AbstractBuilder {
  2. public XiaomiBuilder() {
  3. phone = new Phone();
  4. }
  5. @Override
  6. void customCpu(String cpu) {
  7. phone.cpu = cpu;
  8. }
  9. @Override
  10. void customMem(String mem) {
  11. phone.mem = mem;
  12. }
  13. @Override
  14. void customDisk(String disk) {
  15. phone.disk = disk;
  16. }
  17. @Override
  18. void customCam(String cam) {
  19. phone.cam = cam;
  20. }
  21. }

测试方法:

  1. public static void main(String[] args) {
  2. AbstractBuilder builder = new XiaomiBuilder();
  3. builder.customCpu("骁龙888");
  4. builder.customCam("一亿");
  5. builder.customDisk("512G");
  6. builder.customMem("12G");
  7. }

在建造者模式中常用的链式我们也可以实现,只需将设置属性方法返回自身即可。

抽象建造者:

  1. public abstract class AbstractBuilder2 {
  2. Phone phone;
  3. abstract AbstractBuilder2 customCpu(String cpu);
  4. abstract AbstractBuilder2 customMem(String mem);
  5. abstract AbstractBuilder2 customDisk(String disk);
  6. abstract AbstractBuilder2 customCam(String cam);
  7. Phone getProduct() {
  8. return phone;
  9. }
  10. }

具体建造者:

  1. public class XiaomiBuilder2 extends AbstractBuilder2 {
  2. public XiaomiBuilder2() {
  3. phone = new Phone();
  4. }
  5. @Override
  6. AbstractBuilder2 customCpu(String cpu) {
  7. phone.cpu = cpu;
  8. return this;
  9. }
  10. @Override
  11. AbstractBuilder2 customMem(String mem) {
  12. phone.mem = mem;
  13. return this;
  14. }
  15. @Override
  16. AbstractBuilder2 customDisk(String disk) {
  17. phone.disk = disk;
  18. return this;
  19. }
  20. @Override
  21. AbstractBuilder2 customCam(String cam) {
  22. phone.cam = cam;
  23. return this;
  24. }
  25. }

测试方法:

  1. public static void main(String[] args) {
  2. //链式调用
  3. AbstractBuilder2 builder2 = new XiaomiBuilder2();
  4. Phone phone2 = builder2.customCpu("天玑9000").customCam("6400万").customMem("12G")
  5. .customDisk("512G").getProduct();
  6. System.out.println(phone2);
  7. }

2. 应用场景

  • StringBuilder:append(); 给谁append呢?
  • Swagger-ApiBuilder:
  • 快速实现。Lombok-Builder模式

  • 编写代码的时候使用到了设计模式。类+模式

    • xxxFactory
    • xxxTemplate
    • xxxBuilder
    • xxxFacede