Using 'builder()' factory method instead of constructor
See original GitHub issueCurrently, 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:
- Created 4 years ago
- Reactions:1
- Comments:13 (5 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@4whomtbts It’s good to be back! I think you need to know some changes in #2323.
HTTP
keyword have been deprecated.HttpAuthServiceBuilder
andHttpFileServiceBuilder
have been done, you may want to skip them.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
+1.001 for
builder
vs +1 fornewBuilder
😃