Why does the Collections class contain standalone (static) methods, instead of them being added to the List interface?

前端 未结 5 1126
臣服心动
臣服心动 2021-02-19 12:07

For all the methods in Collections that take a List as their first argument, why aren\'t those methods simply part of the List interface?

My intuition is: given a List

相关标签:
5条回答
  • 2021-02-19 12:09

    They are utility methods and not core List functionality. The List interface would just get bloated if you added every possible operation you could do on a List. And the operations in Collections do not need to know about the internals of a List, they operate on the public interface so can happily live in an external class.

    0 讨论(0)
  • 2021-02-19 12:21

    The point is that given suitable primitive operations (remove, set etc) a bunch of more high level operations (sort, shuffle, binary search) can be implemented once rather than being implemented by every single list implementation.

    Effectively, java.util.Collections is like .NET's Enumerable class - full of general purpose methods which can work on any collection, so that they can share a single implementation and avoid duplication.

    0 讨论(0)
  • 2021-02-19 12:22

    It's certainly a judgement call at some level. I think the main trade-off to consider is this: When you add a method to an interface, every implementer of that interface must write code to implement it.

    If the semantics of that method are such that different implementations of the interface will best implement those semantics in very different ways, then it's better to put it in the interface. (Of course, if the semantics simply can't be defined in terms of other methods in the interface, then it must be its own method in the interface.)

    On the other hand, if the semantics are such that they can be defined in terms of other methods in the interface, and implementers of the interface will just tend to write the same code over and over again, then it's better to make a utility method that takes an instance of the interface as an argument.

    0 讨论(0)
  • 2021-02-19 12:25

    There are two explanations here:

    1. Historical: Collections class was created after List interface. Designers chose to preserve backward compatibility of already existing interface. Otherwise a lot of developers would have to change their code.

    2. Logical: The methods you are talking about do not require internal knowledge on List implementation and can be implemented over ANY collection implementing it.

    0 讨论(0)
  • 2021-02-19 12:27

    Rational Behind the List Interface's Methods

    1. The List interface is a very core part of the Java runtime and is already a little onerous to fully implement all of the members when rolling out your own List implementations. So, adding extra methods that aren't directly related to the definition of a list is a bit extraneous. If you need those methods on a List implementation, why not subclass the interface and then require them?
    2. If you where going to come along say in version 1.3 and add functionality to the List interface by adding new utility methods, you will break all past implementors of the interface.
    3. From a Domain-Driven Design perspective, the utility methods in Collections are not part of the normal domain of a list.
    4. Regarding OO design principals, I think it would be important to make the distinction between application OO design and language runtime OO design.

    The authors of Java may do things very differently now that they have hindsight and perspective of many years of usage of the API. That said the C# IList interface is quite similar to Java's and C#'s authors did have the perspective.

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