11

Could someone explain me the advantages of using lombok @Builder to create an object instead of constructor call?

MyObject o1 = MyObject.builder()
              .id(1)
              .name("name")
              .build();

MyObject o2 = new MyObject(1, "name")

Is it just a question of better visibility?

Vitalii
  • 10,091
  • 18
  • 83
  • 151
  • 4
    not "visibility" but "readability". And you can *in theory* do more things with a builder, since you can pass the half initialized builder around. – luk2302 Nov 17 '17 at 15:47
  • 3
    The builder however has a drawback: it doesn't _force_ you to supply all mandatory parameters. So for instance if you refactor some code and add a mandatory parameter to the constructor, you won't have compile error on builder callers... but you'll have runtime failures. – Joel Nov 17 '17 at 15:53
  • 1
    Careful passing half-initialized builders around. They're mutable. A big reason to use Lombok in the first place is immutable objects. – slim Nov 17 '17 at 16:10
  • Possible duplicate of [When would you use the Builder Pattern?](https://stackoverflow.com/questions/328496/when-would-you-use-the-builder-pattern) – VLAZ Sep 27 '19 at 09:33

2 Answers2

17

Consider:

Order order = new Order("Alan", "Smith", 2, 6, "Susan", "Smith");

What do the parameters mean? We have to look at the constructor spec to find out.

Now with a builder:

Order order = Order.builder()
    .originatorFirstName("Alan")
    .originatorLastName("Smith")
    .lineItemNumber(2)
    .quantity(6)
    .recipientFirstName("Susan")
    .recipientLastName("Smith")
    .build();

It's more wordy, but it's very clear to read, and with IDE assistance it's easy to write too. The builders themselves are a bit of a chore to write, but code-generation tools like Lombok help with that.

Some people argue that if your code needs builders to be readable, that's exposing other smells. You're using too many basic types; you're putting too many fields in one class. For example, consider:

Order order = new Order(
     new Originator("Alan", "Smith"),
     new ItemSet(new Item(2), 6),
     new Recipient("Susan", "Smith"));

... which is self-explanatory without using a builder, because we are using more classes with single-responsibilities and fewer fields.

slim
  • 40,215
  • 13
  • 94
  • 127
12

This is not a lombok specific feature, this is called a builder pattern.

Imagine you have a class with 20 parameters, and 10 of them are optional. You could somehow make tons of constructors taking care of this logic, or make a constructor with all those arguments and pass nulls in some places. Isn't builder pattern simply easier?

Shadov
  • 5,421
  • 2
  • 19
  • 38
  • Where: a class should have one single responsibility. Most often, that does not go together with having 20 parameters. If something has 20 fields, you might rather want to step back and check if you are really talking about one thing, or something that should rather be broken into several smaller things. – GhostCat Nov 17 '17 at 16:09
  • 1
    `@Builder` is not the "Gang of Four" build pattern linked in your answer. The builder pattern consists of a Director and concrete builders, not the same as what `@Builder` provides. – Marc Jun 03 '21 at 07:21