`

The builder pattern in practice

 
阅读更多

So, this is my first post (and my first blog for that matter). I can’t remember exactly where I read this (although I’m almost sure it was on Practices of an Agile Developer), but writing in a blog is supposed to help you get your thoughts together. Concretely, by taking the time to explain what you know, you get a better understanding of it yourself.

And that’s exactly what I’m going to try to do here, explain things to get a better understanding of them. And, as a bonus, it will also serve me as centralized place to go to when I want to revisit something I’ve done in the past. Hopefully, this will help some of you in the process.

With the introduction out of the way, lets jump straight into this first post which, as the title so eloquently says:), is about the builder pattern. I’m not going to dive into much details about the pattern because there’s already tons of posts and books that explain it in fine detail. Instead, I’m going to tell you why and when you should consider using it. However, it is worth mentioning that this pattern is a bit different to the one presented in the Gang of Four book. While the original pattern focuses on abstracting the steps of construction so that by varying the builder implementation used we can get a different result, the pattern explained in this post deals with removing the unnecessary complexity that stems from multiple constructors, multiple optional parameters and overuse of setters.

Imagine you have a class with a substantial amount of attributes like the User class below. Let’s assume that you want to make the class immutable (which, by the way, unless there’s a really good reason not to you should always strive to do. But we’ll get to that in a different post).

1
2
3
4
5
6
7
8
public class User {
    private final String firstName;    //required
    private final String lastName;    //required
    private final int age;    //optional
    private final String phone;    //optional
    private final String address;    //optional
...
}

Now, imagine that some of the attributes in your class are required while others are optional. How would you go about building an object of this class? All attributes are declared final so you have to set them all in the constructor, but you also want to give the clients of this class the chance of ignoring the optional attributes.

A first and valid option would be to have a constructor that only takes the required attributes as parameters, one that takes all the required attributes plus the first optional one, another one that takes two optional attributes and so on. What does that look like? Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public User(String firstName, String lastName) {
    this(firstName, lastName, 0);
}
 
public User(String firstName, String lastName, int age) {
    this(firstName, lastName, age, "");
}
 
public User(String firstName, String lastName, int age, String phone) {
    this(firstName, lastName, age, phone, "");
}
 
public User(String firstName, String lastName, int age, String phone, String address) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.age = age;
    this.phone = phone;
    this.address = address;
}

The good thing about this way of building objects of the class is that it works. However, the problem with this approach should be pretty obvious. When you only have a couple of attributes is not such a big deal, but as that number increases the code becomes harder to read and maintain. More importantly, the code becomes increasingly harder for clients. Which constructor should I invoke as a client? The one with 2 parameters? The one with 3? What is the default value for those parameters where I don’t pass an explicit value? What if I want to set a value for address but not for age and phone? In that case I would have to call the constructor that takes all the parameters and pass default values for those that I don’t care about. Additionally, several parameters with the same type can be confusing. Was the first String the phone number or the address?

So what other choice do we have for these cases? We can always follow the JavaBeans convention, where we have a default no-arg constructor and have setters and getters for every attribute. Something like:

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 User {
    private String firstName; // required
    private String lastName; // required
    private int age; // optional
    private String phone; // optional
    private String address;  //optional
 
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
}

This approach seems easier to read and maintain. As a client I can just create an empty object and then set only the attributes that I’m interested in. So what’s wrong with it? There are two main problems with this solution. The first issue has to do with having an instance of this class in an inconsistent state. If you want to create an User object with values for all its 5 attributes then the object will not have a complete state until all the setXmethods have been invoked. This means that some part of the client application might see this object and assume that is already constructed while that’s actually not the case.
The second disadvantage of this approach is that now the User class is mutable. You’re loosing all the benefits of immutable objects.

Fortunately there is a third choice for these cases, the builder pattern. The solution will look something like the following.

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
public class User {
    private final String firstName; // required
    private final String lastName; // required
    private final int age; // optional
    private final String phone; // optional
    private final String address; // optional
 
    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);
        }
 
    }
}

A couple of important points worth noting:

  • The User constructor is private, which means that this class can not be directly instantiated from the client code.
  • The class is once again immutable. All attributes are final and they’re set on the constructor. Additionally, we only provide getters for them.
  • The builder uses the Fluent Interface idiom to make the client code more readable (we’ll see an example of this in a moment).
  • The builder constructor only receives the required attributes and this attributes are the only ones that are defined “final” on the builder to ensure that their values are set on the constructor.

The use of the builder pattern has all the advantages of the first two approaches I mentioned at the beginning and none of their shortcomings. The client code is easier to write and, more importantly, to read. The only critique that I’ve heard about the pattern is the fact that you have to duplicate the class’ attributes on the builder. However, given the fact that the builder class is usually a static member class of the class it builds, they can evolve together fairly easy.

Now, how does the client code trying to create a new User object looks like? Let’s see:

1
2
3
4
5
6
7
8
public User getUser() {
    return new
            User.UserBuilder("Jhon", "Doe")
            .age(30)
            .phone("1234567")
            .address("Fake address 1234")
            .build();
}

Pretty neat, isn’t it? You can build a User object in 1 line of code and, most importantly, is very easy to read. Moreover, you’re making sure that whenever you get an object of this class is not going to be on an incomplete state.

This pattern is really flexible. A single builder can be used to create multiple objects by varying the builder attributes between calls to the “build” method. The builder could even auto-complete some generated field between each invocation, such as an id or serial number.

An important point is that, like a constructor, a builder can impose invariants on its parameters. The build method can check these invariants and throw anIllegalStateException if they are not valid.
It is critical that they be checked after copying the parameters from the builder to the object, and that they be checked on the object fields rather than the builder fields. The reason for this is that, since the builder is not thread-safe, if we check the parameters before actually creating the object their values can be changed by another thread between the time the parameters are checked and the time they are copied. This period of time is known as the “window of vulnerability”. In our User example this could look like the following:

1
2
3
4
5
6
7
public User build() {
    User user = new user(this);
    if (user.getAge() 120) {
        throw new IllegalStateException(“Age out of range”); // thread-safe
    }
    return user;
}

The previous version is thread-safe because we first create the user and then we check the invariants on the immutable object. The following code looks functionally identical but it’s not thread-safe and you should avoid doing things like this:

1
2
3
4
5
6
7
public User build() {
    if (age 120) {
        throw new IllegalStateException(“Age out of range”); // bad, not thread-safe
    }
    // This is the window of opportunity for a second thread to modify the value of age
    return new User(this);
}

A final advantage of this pattern is that a builder could be passed to a method to enable this method to create one or more objects for the client, without the method needing to know any kind of details about how the objects are created. In order to do this you would usually have a simple interface like:

1
2
3
public interface Builder {
    T build();
}

In the previous User example, the UserBuilder class could implement Builder<User>. Then, we could have something like:

1
UserCollection buildUserCollection(Builder<? extends User> userBuilder){...}

Well, that was a pretty long first post. To sum it up, the Builder pattern is an excellent choice for classes with more than a few parameters (is not an exact science but I usually take 4 attributes to be a good indicator for using the pattern), especially if most of those parameters are optional. You get client code that is easier to read, write and maintain. Additionally, your classes can remain immutable which makes your code safer.

分享到:
评论

相关推荐

    BuilderPattern.unitypackage

    BuilderPattern.unitypackage是一个建造者模式的例子。

    Terabey The Room Builder

    "Terabey The Room Builder" 是一款专为用户创造个性空间的软件,它允许用户根据自己的创想设计和建造像素风格的虚拟房间。这款软件集创新、设计与娱乐于一体,鼓励用户发挥无限想象力,创造出独特且富有个人色彩的...

    建造者模式【Builder Pattern】(一)问题提出

    建造者模式【Builder Pattern】是一种设计模式,它在软件工程中用于将复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。这种模式在处理复杂对象的构造时特别有用,因为它允许我们通过分离构造...

    创建型模式之抽象工厂模式(Abstract Factory Pattern)

    抽象工厂模式是设计模式中的一种创建型模式,其主要目的是为了解耦客户端代码与具体产品的实现。在软件工程中,当系统需要根据不同的平台、环境或者配置产生一系列相关对象,而这些对象之间的关系又非常复杂时,抽象...

    Builder pattern for support library Snackbars, that makes them easier to customise and use.zip

    Builder pattern for support library Snackbars, that makes them easier to customise and use.zip,[ARCHIVED] Builder pattern for support library Snackbars, that makes them easier to customise and use

    Flex Builder Plug-in and Adobe

    In the dynamic world of web development, especially in the realms of Flash and Flex, the integration between Eclipse and Flex Builder has been a significant step forward. This article delves into the ...

    Java Builder Pattern建造者模式详解及实例

    Java Builder Pattern,也称为建造者模式,是一种设计模式,它将复杂的对象构造过程与对象的表示分离,使得构造过程可以在不暴露其内部细节的情况下完成。这种模式常用于创建那些具有多个部分或组件的对象,其中每个...

    建造者模式【Builder Pattern】(三)问题引申

    建造者模式(Builder Pattern)是一种创建型设计模式,它允许我们分步骤构建复杂对象,而无需暴露构造过程的细节。这种模式将一个复杂的构建过程分解为一系列简单的步骤,使得构造过程和表示细节可以独立变化。在...

    建造者模式【Builder Pattern】(二)问题改进

    建造者模式(Builder Pattern)是一种创建型设计模式,它允许我们分步骤构建复杂对象,而无需暴露构造过程的细节。这种模式将一个复杂的构建过程分解为一系列简单的步骤,使得构造过程可以有不同的解释,从而实现...

    java设计模式-建造者模式(Builder Pattern)

    在Java中,建造者模式(Builder Pattern)是一种创建型设计模式,它允许你分步骤地构建一个复杂对象。这个模式通过将构建过程和表示过程分离,使得同样的构建过程可以创建不同的表示。建造者模式特别适合用于创建...

    [创建型模式]设计模式之建造者模式(Builder Pattern)

    【创建型模式】设计模式之建造者模式(Builder Pattern) 建造者模式(Builder Pattern)是设计模式中的一个创建型模式,它提供了一种方法来分步骤构造复杂的对象,使得构造过程和表示分离,使得同样的构建过程可以...

    建造者模式【Builder Pattern】(一)问题改进

    建造者模式(Builder Pattern)是设计模式中的一种创建型模式,它允许我们分步骤构建复杂的对象,而无需暴露其构造过程。在实际的软件开发中,我们常常遇到需要创建具有多种构建方式或配置的对象,这时建造者模式就...

    Teach Yourself Borland C++ Builder in 21 Days.pdf

    《Teach Yourself Borland C++ Builder in 21 Days》是C++ Builder初学者的一本经典教程,旨在帮助读者在21天内掌握这一强大的Windows应用程序开发工具。Borland C++ Builder是一款由Borland公司(现为Embarcadero ...

    Think in Pattern

    《Think in Pattern》不仅涵盖了GoF(Gang of Four)提出的23种经典设计模式,还包括了简化惯用法(Simplifying Idioms)和构建器模式(Builder Pattern)等额外章节。本书的特点在于它将每种模式都置于实际问题的...

    Design Pattern In Java.pdf

    《Design Pattern In Java》这本书是关于Java设计模式的指南,由James W. Cooper撰写。设计模式是软件开发中的一种最佳实践,它总结了在特定情境下解决常见问题的经验和方法,为面向对象编程提供了可复用的设计解决...

    Bar_code_application_code_in_PowerBuilder.rar_PowerBuilder_in

    总结来说,"Bar_code_application_code_in_PowerBuilder"是一个关于在PowerBuilder中实现条形码生成和应用的代码示例。通过创建用户对象,开发者可以方便地在各种界面元素中集成条形码功能,提高应用的交互性和实用...

    ReportBuilder v15.04 for Berlin with DevExpress theme support

    With ReportBuilder Enterprise, you get everything included in ReportBuilder Professional, plus the RAPlanguage, which allows developers and end users to code calculations and complex event handlers at...

    java实现建造者模式(Builder Pattern)

    java实现建造者模式(Builder Pattern) java实现建造者模式(Builder Pattern)是一种软件设计模式,旨在解决软件系统中创建复杂对象的问题。建造者模式可以将复杂对象的创建过程分解为多个简单的对象一步一步构建...

    design pattern

    单例模式(Singleton)、策略模式(StrategyPattern)、适配器模式(AdapterPattern)、装饰者模式(DecoratorPattern)、抽象工厂模式(...)、建造者模式(BuilderPattern)以及桥接模式(BridgePattern)...

Global site tag (gtag.js) - Google Analytics