I am currently working with Java 8 and using functional interfaces a lot. I often find myself writing code like this:
public interface MessageHandler extends Consumer<String> { }
Is this a good idea or an indicator of me abusing functional interfaces to make Java feel more like C#?
Edit: Maybe to expand a little:
public class MessageGenerator {
public void registerMessageHandler(MessageHandler handler) {
// [...]
public interface MessageHandler extends Consumer<String> { }
}
2
I wholeheartedly agree with this:
Interfaces should almost always be named after what they do, not after
some generic syntactic idea.
(Taken from this answer: https://softwareengineering.stackexchange.com/a/276866/41811)
So, adding a functional interface whose name explains precisely what the underlying Consumer<String>
is going to be used for is a very good idea. It harms nothing, and it improves readability.
Anything that makes code more readable while keeping the semantics unchanged is good. Whether or not this is due to your experience with another language is irrelevant – programming languages are tools to achieve ends, not values in themselves.
Therefore, if your way of writing things reads better than using the bare language-provided types, do it. (Things could be different if adding another layer of types affected the efficiency of the code that is actually run, but I’m fairly certain that in this case it doesn’t.)
1