建造者模式,将一个复杂对象的构建与它的表示分离,使得构建过程可以以分部进行的链式调用来完成
若不用Build模式 定义一个Person类 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 public class Person { private String name; private int age; private double height; private double weight; public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public double getHeight () { return height; } public void setHeight (double height) { this .height = height; } public double getWeight () { return weight; } public void setWeight (double weight) { this .weight = weight; } }
可能会定义一个构造方法。 1 2 3 4 5 6 public Person (String name, int age, double height, double weight) { this .name = name; this .age = age; this .height = height; this .weight = weight; }
对应不同的需求,可能会定义好几种构造方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public Person (String name) { this .name = name; } public Person (String name, int age) { this .name = name; this .age = age; } public Person (String name, int age, double height) { this .name = name; this .age = age; this .height = height; }
使用的时候就变成这样了 1 2 3 4 5 Person p1=new Person(); Person p2=new Person("张三" ); Person p3=new Person("李四" ,18 ); Person p4=new Person("王五" ,21 ,180 ); Person p5=new Person("赵六" ,17 ,170 ,65.4 );
这样的坏处:
构造函数有多个参数,不好确定每个参数具体意思,可读性不强
若参数较多的时候,麻烦
若使用Builder模式 具体代码实现 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 public class Person { private String name; private int age; private double height; private double weight; privatePerson(Builder builder) { this .name=builder.name; this .age=builder.age; this .height=builder.height; this .weight=builder.weight; } public String getName () { return name; } public void setName (String name) { this .name = name; } public int getAge () { return age; } public void setAge (int age) { this .age = age; } public double getHeight () { return height; } public void setHeight (double height) { this .height = height; } public double getWeight () { return weight; } public void setWeight (double weight) { this .weight = weight; } static class Builder { private String name; private int age; private double height; private double weight; public Builder name (String name) { this .name=name; return this ; } public Builder age (int age) { this .age=age; return this ; } public Builder height (double height) { this .height=height; return this ; } public Builder weight (double weight) { this .weight=weight; return this ; } public Person build () { return new Person(this ); } } }
大概流程
定义静态Builder类
Builder类中声明和外部类一样的变量
定义多个set方法,返回结果为Builder本身(this)
Builder类中增加build()方法,返回外部类Person对象
外部类Person中声明一个参数为Builder的构造函数
使用 1 2 3 4 5 6 7 Person.Builder builder=new Person.Builder(); Person person=builder .name("张三" ) .age(18 ) .height(178.5 ) .weight(67.4 ) .build();
Android中哪些地方使用到Builder模式 对话框的创建,AlertDialog 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 AlertDialog.Builder builder=new AlertDialog.Builder(this ); AlertDialog dialog=builder.setTitle("标题" ) .setIcon(android.R.drawable.ic_dialog_alert) .setView(R.layout.myview) .setPositiveButton(R.string.positive, new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialog, int which) { } }) .setNegativeButton(R.string.negative, new DialogInterface.OnClickListener() { @Override public void onClick (DialogInterface dialog, int which) { } }) .create(); dialog.show();