Is it better to use List or Collection?

前端 未结 8 689
失恋的感觉
失恋的感觉 2021-02-03 19:33

I have an object that stores some data in a list. The implementation could change later, and I don\'t want to expose the internal implementation to the end user. However, the us

相关标签:
8条回答
  • 2021-02-03 20:14

    Returning a List is in line with programming to the Highest Suitable Interface.

    Returning a Collection would cause ambiguity to the user, as a returned collection could be either: Set, List or Queue.

    0 讨论(0)
  • 2021-02-03 20:24

    Independent of the ability to index into the list via List.get(int), do the users (or you) have an expectation that the elements of the collection are in a reliable and predictable order? Can the collection have multiples of the same item? Both of these are expectations of lists that are not common to more general collections. These are the tests I use when determining which abstraction to expose to the end user.

    0 讨论(0)
  • 2021-02-03 20:33

    Yes, your first alternative does leak implementation details if it's not part of your interface contract that the method will always return a List. Also, allowing user code to replace your collection instance is somewhat dangerous, because the implementation they pass in may not behave as you expect.

    Of course, it's all a matter of how much you trust your users. If you take the Python philosophy that "we're all consenting adults here" then the first method is just fine. If you think that your library will be used by inexperienced developers and you need to do all you can to "babysit" them and make sure they don't do something wrong then it's preferable not to let them set the collection and not to even return the actual collection. Instead return a (shallow) copy of it.

    0 讨论(0)
  • 2021-02-03 20:33

    Were I concerned with obscuring internal representation of my data to an outside user, I would use either XML or JSON. Either way, they're fairly universal.

    0 讨论(0)
  • 2021-02-03 20:37

    Using the most general type, which is Collection, makes the most sense unless there is some explicit reason to use the more specific type - List. But whatever you do, if this is an API for public consumption be clear in the documentation what it does; if it returns a shallow copy of the collection say so.

    0 讨论(0)
  • 2021-02-03 20:37

    It depends on what guarantees you want to provide the user. If the data is sequential such that the order of the elements matter and you are allowing duplicates, then use a list. If order of elements does not matter and duplicates may or may not be allowed, then use a collection. Since you are actually returning the underlying collection you should not have both a get and set function, only a get function, since the returned collection may be mutated. Also, providing a set function allows the type of collection to be changed by the user, whereas you probably want for the particular type to be controlled by you.

    0 讨论(0)
提交回复
热议问题