Builder模式
- 建造者模式是较为复杂的创建型模式,他将客服端与包含多个组成部分的复杂对象过程分离
- 使用场景:当构造一个对象需要很多的参数的时候,并且参数的个数或者类型不固定的场景.
-
UML结构
bulder.png
代码:
Builder对象:
abstract class Builder {
//创建产品对象
protected Product product = new Product();
public abstract void buildPartA();
public abstract void buildPartB();
public abstract void buildPartC();
//返回产品对象
public Product getResult() {
return product;
}
}
createBuilder对象:
public class CreateBuilder extends Builder{
@Override
public void buildPartA() {
}
@Override
public void buildPartB() {
}
@Override
public void buildPartC() {
}
}
Director对象:
public class Director {
private Builder builder;
public Director(Builder builder) {
this.builder = builder;
}
public void setBuilder(Builder builder) {
this.builder = builder;
}
//产品构建与组装方法
public Product construct() {
builder.buildPartA();
builder.buildPartB();
builder.buildPartC();
return builder.getResult();
}
}
product对象:
public class Product {
private String partA;//定义部件,部件可以任意类型
private String partB;
private String partC;
public String getPartA() {
return partA;
}
public void setPartA(String partA) {
this.partA = partA;
}
public String getPartB() {
return partB;
}
public void setPartB(String partB) {
this.partB = partB;
}
public String getPartC() {
return partC;
}
public void setPartC(String partC) {
this.partC = partC;
}
}
使用:
public static void main(String[] args) {
Builder builder = new CreateBuilder();
Director director = new Director(builder);
Product product = director.construct();
}
总结
- Builder:为创建一个产品Product对象的各个部件指定抽象接口
- CreateBuilder: 它实现了Builder接口,实现各个部件的具体构造和装配方法.
- Product:它是被构建的复杂对象,包含多个组成部件.
- Director: 指挥者又称导演类,他负责安排负责对象的建造次序,指挥者和抽象者之间存在的关联关系.
优点
- 松散耦合,生成器模式可以用在同一个构建算法构建出表现上完全不同的产品,实现产品构建和产品表现上的分离.
- 很容易改变产品的内部表示。
- 更好的复用性:生成器模式很好的实现构建算法和具体产品实现的分离。
缺点
- 会产生多余的Builder对象以及Director对象,消耗内存。
- 对象的构建过程会暴露。
Android Builder模式实际运用
- AlertDialog
- OkHttp/Glide
常用的builder模式
参考链接: https://www.jianshu.com/p/e2a2fe3555b9
public class User {
private final String firstName; // 必传参数
private final String lastName; // 必传参数
private final int age; // 可选参数
private final String phone; // 可选参数
private final String address; // 可选参数
private User(UserBuilder builder) {
this.firstName = builder.firstName;
this.lastName = builder.lastName;
this.age = builder.age;
this.phone = builder.phone;
this.address = builder.address;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getAge() {
return age;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
public static class UserBuilder {
private final String firstName;
private final String lastName;
private int age;
private String phone;
private String address;
public UserBuilder(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public UserBuilder age(int age) {
this.age = age;
return this;
}
public UserBuilder phone(String phone) {
this.phone = phone;
return this;
}
public UserBuilder address(String address) {
this.address = address;
return this;
}
public User build() {
return new User(this);
}
}
}
new User.UserBuilder("王", "小二") .age(20).phone("123456789").address("亚特兰蒂斯大陆").build();
网友评论