question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using 'builder()' factory method instead of constructor

See original GitHub issue

Currently, we instantiate a builder by calling a constructor or a static method in a builder class:

Server server = new ServerBuilder()
        .foo()
        .bar()
        .build();

I wonder if it was a good idea or we need to reconsider this decision in favor of having builder() method:

Server server = Server.builder()
                      .foo()
                      .bar()
                      .build();

The pros of builder() are:

  • We can easily align at '.', which is better in terms of aesthetics.
  • We do not need to import ServerBuilder if we chain all.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ikhooncommented, Dec 26, 2019

@4whomtbts It’s good to be back! I think you need to know some changes in #2323.

  1. Please make sure your current branch is up-to-date from the master branch. Lots of class which have HTTP keyword have been deprecated.
  2. HttpAuthServiceBuilder and HttpFileServiceBuilder have been done, you may want to skip them.
  3. This issue is scheduled to apply before release 1.0.0. Maybe in the middle of January. 😉

I’m looking forward to your next PR. Cheer up!


  • ClientBuilder
  • DefaultClientBuilderParams
  • ServerListenerBuilder
  • ~HttpAuthServiceBuilder~
  • SimpleCompositeServiceBuilder
  • SimpleCompositeRpcServiceBuilder
  • CorsPolicyBuilder
  • CorsServiceBuilder
  • DocServiceBuilder
  • HttpFileBuilder
  • ~HttpFileServiceBuilder~
  • LoggingServiceBuilder
  • JettyServiceBuilder
  • ArmeriaRetrofitBuilder
  • KeyStoreCredentialResolverBuilder
  • SamlServiceProviderBuilder
  • TomcatServiceBuilder
  • ZooKeeperUpdatingListenerBuilder
2reactions
anuraagacommented, May 27, 2019

+1.001 for builder vs +1 for newBuilder 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Constructors vs Factory Methods [closed] - Stack Overflow
Consider using a static factory method instead of a constructor if the semantics of the desired operation do not map directly to the...
Read more >
Java Constructors vs Static Factory Methods - Baeldung
Learn about static factory methods in Java and why they're sometimes preferred over constructors for instantiating and initializing objects.
Read more >
Builder with constructor or factory method?
My best argument for using a factory method would be: Say that the builder uses a final field String id that has to...
Read more >
Difference Between Constructor and Static Factory Method in ...
The static factory methods are methods that return an instance of the native class. The static factory method has names that clarify the...
Read more >
Replace Constructor with Factory Method - Refactoring.Guru
A factory method can return an already created object, unlike a constructor, which always creates a new instance. How to Refactor. Create a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found