建造者模式简介

  • 模式属于创建型模式,它提供了一种创建对象的最佳方式。
  • 使用多个简单的对象一步一步构建成一个复杂的对象。
  • 一个Builder类会一步一步构造最终的对象。该Builder类是独立于其他对象的。

指挥者模式

产品类Product
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//产品
public class Product {
private String one;
private String two;
private String three;

public String getOne() {
return one;
}

public void setOne(String one) {
this.one = one;
}

public String getTwo() {
return two;
}

public void setTwo(String two) {
this.two = two;
}

public String getThree() {
return three;
}

public void setThree(String three) {
this.three = three;
}

@Override
public String toString() {
return "Product{" +
"one='" + one + '\'' +
", two='" + two + '\'' +
", three='" + three + '\'' +
'}';
}
}
抽象类Builder
1
2
3
4
5
6
7
8
9
10
//抽象的建造者
public abstract class Builder {
abstract void one();//第一步工序

abstract void two();//第二步工序

abstract void three();//第三步工序

abstract Product getProduct();//完工,得到产品
}
子类Worker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//具体的建造者:工人
public class Worker extends Builder {
private Product product;

public Worker() {
product = new Product();
}

@Override
void one() {
product.setOne("第一步工序");
System.out.println("第一步工序");
}

@Override
void two() {
product.setTwo("第二步工序");
System.out.println("第二步工序");
}

@Override
void three() {
product.setThree("第三步工序");
System.out.println("第三步工序");
}

@Override
Product getProduct() {
return product;
}
}
指挥者类
1
2
3
4
5
6
7
8
9
10
11
//指挥者
public class Director {
//指挥工人生成产品
public Product build(Builder builder){
builder.one();
builder.two();
builder.three();

return builder.getProduct();
}
}
测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Test {
public static void main(String[] args) {
//指挥者
Director director = new Director();
//指挥工人生产产品
Product product = director.build(new Worker());
System.out.println(product);

/**
* 输出结果:
* 第一步工序
* 第二步工序
* 第三步工序
* Product{one='第一步工序', two='第二步工序', three='第三步工序'}
*/
}
}
测试结果
1
2
3
4
第一步工序
第二步工序
第三步工序
Product{one='第一步工序', two='第二步工序', three='第三步工序'}

内部类模式

产品类Product
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//产品
public class Product {
private String one = "第一步工序";
private String two = "第二步工序";
private String three = "第三步工序";

public String getOne() {
return one;
}

public void setOne(String one) {
this.one = one;
}

public String getTwo() {
return two;
}

public void setTwo(String two) {
this.two = two;
}

public String getThree() {
return three;
}

public void setThree(String three) {
this.three = three;
}

@Override
public String toString() {
return "Product{" +
"one='" + one + '\'' +
", two='" + two + '\'' +
", three='" + three + '\'' +
'}';
}
}
抽象类Builder
1
2
3
4
5
6
7
8
9
10
//抽象的建造者
public abstract class Builder {
abstract Builder one(String mes);//第一步工序

abstract Builder two(String mes);//第二步工序

abstract Builder three(String mes);//第三步工序

abstract Product getProduct();//完工,得到产品
}
子类Worker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//具体的建造者:工人
public class Worker extends Builder {
private Product product;

public Worker() {
product = new Product();
}

@Override
Builder one(String mes) {
product.setOne(mes);
return this;
}

@Override
Builder two(String mes) {
product.setTwo(mes);
return this;
}

@Override
Builder three(String mes) {
product.setThree(mes);
return this;
}

@Override
Product getProduct() {
return product;
}
}
测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class Test {
public static void main(String[] args) {
//工人
Worker worker = new Worker();
//生产产品
Product product = worker.getProduct();
System.out.println(product.toString());
/**
* 输出结果:
* Product{one='第一步工序', two='第二步工序', three='第三步工序'}
*/

//链式编程
product = worker.one("第一步额外工序").three("提前第三步工序").getProduct();
System.out.println(product);
/**
* 输出结果:
* Product{one='第一步额外工序', two='第二步工序', three='提前第三步工序'}
*/
}
}
测试结果
1
2
Product{one='第一步工序', two='第二步工序', three='第三步工序'}
Product{one='第一步额外工序', two='第二步工序', three='提前第三步工序'}