4

Recently, I came across Lombok's builder and frankly loved it. Then I came to know that Builder pattern is described at many resources for instance Effective Java. Somewhat like this

Now, in this pattern, the constructor is kept private so that the instance of the class could only be created by only using the builder.

But, in Lombok's documentation for the builder Builder's Documentation the constructor visibility is kept package private. What could be the reasons behind keeping it as package private?

Little Helper
  • 1,870
  • 3
  • 12
  • 20
Crosk Cool
  • 664
  • 2
  • 12
  • 27
  • 1
    It think it might be to enable inheritance for builders, as a single private constructor would prevent inheritance. I'm not sure if @Builder actually supports inheritance though. You should try asking this in the [lombok group](https://groups.google.com/group/project-lombok) if you don't get any answer here. – kapex Mar 07 '18 at 10:49

1 Answers1

1

The answer to your question described in detail at https://softwareengineering.stackexchange.com/questions/362265/builder-with-constructor-or-factory-method. I see that your question is more specific to Project Lombok's design so you might want to join the community. But in general I agree with the accepted answer and the way Lombok implemented the builder pattern using a factory method.

Istvan
  • 613
  • 5
  • 5