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

前端 未结 5 1832
时光取名叫无心
时光取名叫无心 2021-02-19 11:41

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:20

    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.

提交回复
热议问题