6

In Effective Java , Item 1, it says that the static factory methods made the Collections framework much smaller than it would have been. Could someone please explain how ? I can't understand how the following is possible just because of using static factory methods ? I mean we still have to implement those separate implementations don't we ?

The Collections Framework API is much smaller than it would have been had it exported thirty-two separate public classes, one for each convenience implementation.

Prasad Weera
  • 1,223
  • 3
  • 13
  • 25

2 Answers2

3

By "smaller" they mean "less classes".

Instead of providing lots of classes for each variation of implementation, instead factory methods have been provided that return such implementations without the need to have their classes declared as top-level classes (less "class bloat").

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Can you explain the disadvantages of having too many top-level classes ? I am not sure I understand that part of the answer. – Viraj Feb 26 '17 at 02:33
  • @viraj too many classes are a form of "code clutter", especially when the concrete classes that would need needed are effectively flavours of a few interfaces. Factory methods also provide a stable API behind which the actual classes used are free to be changed as the code base evolves. – Bohemian Feb 26 '17 at 04:30
  • Correct me if I haven't understood it right, hiding the actual implementations(by making the classes non-public) behind interfaces in factory methods help in cases when you want to change the implementations as the code base evolves. This means, lets say you have 2 implementations for a particular interface in version 1.1, you could simply remove 1 implementation of the 2 in version 1.2 ? – Viraj Feb 26 '17 at 05:12
0

I think the meaning here is that there's only core implementations in java collections API, if you need synchronisation, etc "wrappers" are provided by static factories.

So there's 8 implementations and without "wrappers" there would be more (size gain).

See http://docs.oracle.com/javase/7/docs/technotes/guides/collections/overview.html for more details