建造者模式(Builder)定义:将一个复杂对象的构造与其表示分离,使同样的构造过程可以创建不同的表示。它将一个复杂的对象分解为多个简单对象,然后一步一步构建这个对象。所构造的对象的组成部分(数量、顺序)不变,而每一个组成部分的具体内容是可变的。
建造者模式的优点:
1、各个具体的建造者相互独立。
2、调用者不需要知道所创建的对象的内部细节。
但是使用建造这模式,必须满足所创建的对象的“各组成部分相同”,这个前提条件。
抽象建造者及其实现类:
1 public abstract class Builder { 2 protected Product product = new Product(); 3 4 public abstract void buildPartA(); 5 6 public abstract void buildPartB(); 7 8 public abstract void buildPartC(); 9 10 public Product getResult() { 11 return product; 12 } 13 } 14 15 public class ConcreteBuilderA extends Builder { 16 @Override 17 public void buildPartA() { 18 // TODO Auto-generated method stub 19 product.setPartA("A->A"); 20 } 21 22 @Override 23 public void buildPartB() { 24 // TODO Auto-generated method stub 25 product.setPartB("A->B"); 26 } 27 28 @Override 29 public void buildPartC() { 30 // TODO Auto-generated method stub 31 product.setPartC("A->C"); 32 } 33 } 34 35 public class ConcreteBuilderB extends Builder { 36 @Override 37 public void buildPartA() { 38 // TODO Auto-generated method stub 39 product.setPartA("B->A"); 40 } 41 42 @Override 43 public void buildPartB() { 44 // TODO Auto-generated method stub 45 product.setPartB("B->B"); 46 } 47 48 @Override 49 public void buildPartC() { 50 // TODO Auto-generated method stub 51 product.setPartC("B->C"); 52 } 53 }
指挥者(对建造者发出命令的):
1 public class Director { 2 private Builder builder; 3 4 public Director(Builder builder) { 5 this.builder = builder; 6 } 7 //控制建造顺序 8 public Product construct() {
9 this.builder.buildPartA(); 10 this.builder.buildPartB(); 11 this.builder.buildPartC(); 12 return this.builder.getResult(); 13 } 14 }
产品:
1 public class Product { 2 private String partA; 3 private String partB; 4 private String partC; 5 6 public void setPartA(String partA) { 7 this.partA = partA; 8 } 9 10 public void setPartB(String partB) { 11 this.partB = partB; 12 } 13 14 public void setPartC(String partC) { 15 this.partC = partC; 16 } 17 18 public void show() { 19 System.out.println(this.partA + "|" + this.partB + "|" + this.partC); 20 } 21 }
调用者:
1 public class Client { 2 /* 3 * 产品都是由多个部件构成的,各个部件可以灵活选择,但其创建步骤都大同小异 4 */ 5 public static void main(String[] args) { 6 Builder builder; 7 //扩展点 8 builder = new ConcreteBuilderA(); 9 //切换到B 10 //builder = new ConcreteBuilderB(); 11 12 //稳定代码 13 Director director = new Director(builder); 14 15 //创建模式,最终得到的对象 16 Product product = director.construct(); 17 product.show(); 18 } 19 }
执行结果:
下面模拟一个真是场景中使用建造者模式的代码:
1 package com.example.uml; 2 3 //建造者模式练习 4 public class BuilderDemo { 5 6 public BuilderDemo() { 7 // TODO Auto-generated constructor stub 8 } 9 10 public static void main(String[] args) { 11 // TODO Auto-generated method stub 12 13 Output output = new EnglishOutput(); 14 Director director = new Director(output); 15 MyRecords myRecords = director.getRecord(); 16 System.out.println(myRecords); 17 } 18 19 } 20 21 //指挥者 22 class Director{ 23 private Output output; 24 25 public Director(Output output) { 26 this.output = output; 27 } 28 29 public MyRecords getRecord() { 30 return this.output.getRecord(); 31 } 32 33 } 34 35 //抽象建造者 36 abstract class Output{ 37 38 protected MyRecords myRecords = new MyRecords(); 39 40 public abstract void writeCourse(); 41 42 public abstract void writeScore(); 43 44 public abstract void writePasst(); 45 46 public MyRecords getRecord() { 47 //模拟场景:先根据用户输入的课程名称,找到编号 48 //根据编号查询出分数,根据分数判断是否及格 49 //因此这三个方法是要按照顺序依次调用的。 50 writeCourse(); 51 writeScore(); 52 writePasst(); 53 54 return myRecords; 55 } 56 } 57 58 class ChineseOutput extends Output{ 59 @Override 60 public void writeCourse() { 61 // TODO Auto-generated method stub 62 myRecords.setCourse("语文"); 63 } 64 65 @Override 66 public void writeScore() { 67 // TODO Auto-generated method stub 68 myRecords.setScore(80); 69 } 70 71 @Override 72 public void writePasst() { 73 // TODO Auto-generated method stub 74 myRecords.setPass(true); 75 } 76 } 77 78 79 class EnglishOutput extends Output{ 80 @Override 81 public void writeCourse() { 82 // TODO Auto-generated method stub 83 myRecords.setCourse("英语"); 84 } 85 86 @Override 87 public void writeScore() { 88 // TODO Auto-generated method stub 89 myRecords.setScore(55); 90 } 91 92 @Override 93 public void writePasst() { 94 // TODO Auto-generated method stub 95 myRecords.setPass(false); 96 } 97 } 98 99 //模型类,成绩表 100 class MyRecords{ 101 //课程名称 102 private String Course; 103 104 //分数 105 private Integer Score; 106 107 //是否及格 108 private Boolean Pass; 109 110 public Boolean getPass() { 111 return Pass; 112 } 113 114 public void setPass(Boolean pass) { 115 Pass = pass; 116 } 117 118 public String getCourse() { 119 return Course; 120 } 121 122 public void setCourse(String course) { 123 Course = course; 124 } 125 126 public Integer getScore() { 127 return Score; 128 } 129 130 public void setScore(Integer score) { 131 Score = score; 132 } 133 134 @Override 135 public String toString() { 136 return "MyRecords [Course=" + Course + ", Score=" + Score + ", Pass=" + Pass + "]"; 137 } 138 }