|
Re: Java API Design Guidelines
|
Posted: Dec 30, 2005 2:43 AM
|
|
These guidelines are very interesting.
Interfaces may be overvalued, yet the arguments are not very convincing. The examples based on String and Integer show that interfaces are not the best for implementing immutable types, but that's about it. Also, String and Integer are not really the typical classes you define when you design an API. For example, I don't think I ever needed to call the String constructor. With Java 5, it seems it is the same with Integer, and that the use of Integer.valueOf(int) is preferred over new Integer(int) .
I don't agree with the argument that there is no point in exposing an interface if the user can't provide his own implementation. There are plenty of SPIs that you usually don't need to implement yourself, and this is not a problem. One of the interest I found in using interfaces (and a factory interface) in a product API is that you can provide different implementations. If you provide classes instead, the user code is filled with new ProvidedClass1 and you will never have the possibility to change the implementation in a later release. If you accept providing interfaces and telling users not to implement it, then the only points against using interfaces are serialization and the missing static methods/constructors.
I would like to have guidelines more closely linked to use cases patterns. In an API, you usually find abstract types implemented as immutable classes (enums for example), domain classes that are usually related (like the car has four wheels) and contain properties of abstract types, utility classes and interfaces, like static utilities or visitor interfaces and implementations.
It seems the question of providing interfaces or classes is mostly interesting for domain classes. If you go for interfaces, then you may wonder whether to separate the interfaces between readonly interfaces and writeonly interfaces... While there is some interest in general guidelines, like the only visible fields whould be static and final, I think it's easier to discuss the merits of each "solution" for a typical use case.
|
|