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.

Put static builder/factory methods on the interface types that they return

See original GitHub issue

_This issue is taken from https://github.com/SpongePowered/SpongeAPI/pull/2275#issuecomment-739912355_

Currently, in order to build a TextComponent, you must call Component.text(*), there is no way to know, based on the TextComponent interface itself, how to construct such an object.

Pre 4.0 gold, TextComponent actually had builder and of methods, which were removed in favour of static methods on Component named text(*). From a strictly Sponge point of view, this works against the design of our API and so this works against us. I also accept that this library is not Sponge and so I’m not asking for of to return, as TextComponent.text(String) is a totally reasonable construct - I understand that you made this deicions to use text() et. al. as methods to support static imports. However, taking the factory method off TextComponent and putting it on to Component has caught a few of us out - personally, I feel Component.text(...) is the wrong place for this to go as the type you’re advertising this to be is a TextComponent.

Futher, from the Sponge point of view, where Text.of() in API 7 was a (admittedly bad) thing, that is the first place that people are going to start looking. Putting the factory and (renamed) builder methods back on TextComponent will aid IDE autocompletion and we’re onto a winner - people will look for that.

Thus, I propose that the text()/textBuilder()/whatever factory/builder methods are put onto TextComponent as these methods construct TextComponents, not merely Components, and I suggest a similar setup for other component types.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kashikecommented, Dec 9, 2020

I do not think we’ll be moving these methods, and I am hesitant to duplicate them in their types for a number of reasons:

  • duplicate code, and lots of it
  • need to remember to add methods to both places, make any changes in both places
  • it promotes looking at a specific type for creation methods, and may harm discoverability of other types - we have seen more use of other Component types since moving the factory methods over to the root Component interface
0reactions
kashikecommented, Dec 9, 2020

I am going to close this issue as wontfix for now, due to the above discussion.

Read more comments on GitHub >

github_iconTop Results From Across the Web

static factory method in interface class java - Stack Overflow
You can define the factory as returning the Interface but internally it creates a concrete class. For example:
Read more >
Static and Default Methods in Interfaces in Java - Baeldung
Default interface methods are an efficient way to deal with this issue. They allow us to add new methods to an interface that...
Read more >
Java 8 Interface Changes - static method, default method
Java 8 interface changes include static methods and default methods in interfaces. Prior to Java 8, we could have only method declarations ...
Read more >
Static method in Interface in Java - GeeksforGeeks
In this program, a simple static method is defined and declared in an interface which is being called in the main() method of...
Read more >
Fluent Builder Pattern — C#. It's very common to ... - Medium
The generic interface IBuilder just has a Build method to return the type that is defined by each class that implements it.
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