Three Reasons Why I Like the Builder PatternThere are three ways to create new objects in Java programming language: I prefer the builder pattern over the other two methods. Why? Joshua Bloch described the builder pattern and the benefits of using it in Effective Java. He did an excellent job and I will not repeat these benefits in this blog post. Instead, I will describe three other reasons why I prefer the builder pattern over the telescoping constructor pattern and the Javabeans pattern. 1. It Helps You to Enforce Better DesignUsing either the telescoping constructor pattern or the Javabeans pattern means that the properties of the created object must be created before the actual object is created. This might or might not be a problem. If the properties of the created object are fetched by using an external service, it is not a problem. However, if the properties are created in the method which creates the “target” object, it is a problem. In the latter scenario, the lifecycle of the created objects is often bound to the lifecycle of the “target” object. If this is the case, the builder pattern can help you use the Domain-driven design (DDD) pattern called the aggregate. Martin Fowler specifies the aggregate pattern as follows:
The builder pattern can help you to design your code in a such way that your domain model is divided into aggregates. This means that the all objects belonging to the aggregate are created by the aggregate root object (Order) and can be accessed only via the aggregate root object. This moves the construction relation to the logic to the place where it belongs. 2. It Makes You ThinkWriting code in the traditional way is easy because you don’t have to think. All you have to do is write the code which copies information from one place to another. It might take some time to do it, but it isn’t a problem to you because you are on your comfort zone. Maybe you want to just crunch code without thinking what you are doing. I am not like that (and you shouldn’t be either). The builder pattern forces you to think about your objects. To be more specific, it forces you to think about the following things:
Finding answers to these questions help you to write better code. I can guarantee that if you take the time to find the answers to these questions, your code will look a lot better than the code written on autopilot. 3. It Helps You to Create Domain-specific LanguagesIf you use create new objects by using the telescoping constructor pattern or the Javabeans pattern, it is hard to add business meaning to your code. You can try to improve the situation by following these principles:
99 times out of a 100 the created objects are just objects without meaning. They hold data. That is all. When you use the builder pattern, you can create a domain-specific language (DSL) for creating new objects by naming the methods of your builder class. This helps you to add business meaning to the code which creates new objects. This raises one question: how can I update the properties of my objects? You can of course be boring and use setter methods for updating the individual properties of your objects. But you can do something totally different as well. Instead of updating individual properties, you can group these properties into meaningful groups and update the values of these properties in a single method. If you name this method correctly, you can create a DSL for updating the information of existing objects. It Is Not a Silver BulletBlake Caldwell says that the builder pattern is basically a less error prone alternative for constructors. I agree with him. The first thing which you notice when you start to use the builder pattern is that the code which creates new objects is easier to write and read. However, after some time you might notice other benefits as well. I know that I did. However, it is important that you understand that the builder pattern is not a silver bullet. I think that no one should follow a best practice just because it is a best practice. This applies to the builder pattern as well. If your object has only a few constructor arguments, it makes no sense to use the builder pattern. However, because of the benefits described in this blog post (and in Effective Java), you should consider using the builder pattern every time when you have to create new objects.
|