-
-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Do not use interfaces for concrete implementations #252
Comments
Makes sense! |
I thought a little bit about it and also read Abstract Classes Compared to Interfaces, in particular the bullet-list there. Instead of interfaces or abstracted classes I would prefer to use classes for It is right that it is suspicious, when an interface internally relies on specific implementations, like interface Security: Via reflection we are still able to call private constructors of classes. In general we should embrace the open/closed principle. Looking at Your ideas are very valuable. I will keep track of it. |
There is a relation between Scala's (trait, companion object) and Java's (interface, static interface methods). Static interface methods are most common factory methods. Java interfaces are used as real mixins with default methods. I'm clear now that this design principle, as used all over in Javaslang, makes absolutely sense, and will be the base for the emerging collection API (and also orher parts of Javaslang). I have great examples in mind and think, my next blog post will cover this in depth to understand Javaslang collections and API design with Java 8. |
What I don't like about java interface is that you cannot annotate a (default) method with final (unlike scala trait). therefore you cannot be sure that the implementation you see in a default method will be the one you get at runtime. (there also can be a performance advantage to final methods, but not sure...) |
I'm sure there are also disadvantages when using classes. There are many disign possibilities with Java 8 interfaces. Example:
I keep this issue open but let us concentrate first on issues with business value and not start too much technical re-design: |
I understand the thoughts of @jbgi but I think it is not sufficient to have classes. The same problem arise when converting all interfaces (Option, Either, List, Stream, ...) to classes as long as the classes are not final. They cannot be final because there are implementations (Some/None, Left/Right, Cons/Nil, Cons/Empty, ...). What we need is something like the I will close this ticket for now as #wontfix |
I am uncomfortable with the use of interfaces for concrete implementations. If write a method that expect a BinaryTree I worry that I may receive any implementation, not only the one from the static factory methods of BinaryTree. I think BinaryTree and other 'concrete' interfaces should be converted to final classes or abstract classes with private constructor.
This can potentially be a security issue.
The text was updated successfully, but these errors were encountered: