java - Is it acceptable to return unmodifiableList or should I return array? -
I have the method list & lt; Foo & gt; GetFoos ()
which receives and returns data from remote server.
Of course, the user should not change the number of items in the list because the data will not synchronize the data with the server (and if he wants to change the number of objects, then he will be given special codes such as AddFoo ()
).
The first method was to return the array and change the signature of the method on foo [] getFoos ()
. But in Java and generally it is more convenient for the user to work with the archive, so I used the list & lt; Foo & gt; GetFoos ()
.
Archive. Unmodile List (Original List)
Therefore, when the user tries to change the list, it will get a runtime exception.
Is there any recommendations about API design in similar cases?
Collections.unmodifiableList
should be completely acceptable and fast ( No need to create an array).
Edit - In the context of API design, you should clarify your java code! Those who use a method without reading their documents, they are worthy of surprising: p
Comments
Post a Comment