Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static factory methods in effective java

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.

like image 360
Prasad Weera Avatar asked Dec 09 '12 05:12

Prasad Weera


People also ask

Should factory class have static methods?

Static factory methods can have meaningful names, hence explicitly conveying what they do. Static factory methods can return the same type that implements the method(s), a subtype, and also primitives, so they offer a more flexible range of returning types.

What is the purpose of static factory method?

The static factory method pattern is a way to encapsulate object creation.

Which of the following options is a disadvantage of using a static factory method?

The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.

Which method uses static factory method design pattern in JDK?

getInstance() method of java. util. Calendar, NumberFormat, and ResourceBundle uses factory method design pattern.


1 Answers

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").

like image 180
Bohemian Avatar answered Oct 08 '22 19:10

Bohemian