Why do LocalDate, LocalTime, and Stream objects use the factory () method instead of the constructor?

Why LocalDate , LocalTime , Stream , etc. do objects use factory of() method instead of constructor?

I found an explanation why factory methods should be used instead of new here . This answer provides a number of reasons, but the only thing related to the Java Date / Time API is the following:

unlike constructors, they are not required to create a new object every time theyre invoked

Since LocalDate and LocalTime immutable, it probably makes sense to use a factory and reuse existing objects rather than creating a new object each time.

Is this the reason why objects like LocalDate and LocalTime are created using the factory method (i.e. LocalDate.of() )? Are there any other reasons?

In addition, Stream objects are mutable. Why is the factory ( Stream.of() ) method used to create a Stream ?

+5
source share
2 answers

Why is the factory (Stream.of ()) method used to create the stream?

Using the factory method means you don't need to know the exact class. This is a good example, since Stream is an interface, and you cannot instantiate an interface.

From source for Stream.of

 /** * Returns a sequential {@code Stream} containing a single element. * * @param t the single element * @param <T> the type of stream elements * @return a singleton sequential stream */ public static<T> Stream<T> of(T t) { return StreamSupport.stream(new Streams.StreamBuilderImpl<>(t), false); } /** * Returns a sequential ordered stream whose elements are the specified values. * * @param <T> the type of stream elements * @param values the elements of the new stream * @return the new stream */ @SafeVarargs @SuppressWarnings("varargs") // Creating a stream from an array is safe public static<T> Stream<T> of(T... values) { return Arrays.stream(values); } 

Note: these methods call other factory methods.

You can see that you have different designs depending on what it's called. You do not need to know that this or that created class, which is ReferencePipeline.Head

+11
source

+1 to reply to Peter.

Another reason for using factory methods is that they act as "named constructors".

For example, LocalDate has 6 static factory methods (at least I can't be exhaustive here):

  • of(int year, int/Month month, int dayOfMonth) (two overloads)
  • ofEpochDay(long epochDay)
  • ofYearDay(int year, int dayOfYear)
  • parse(CharSequence text)
  • parse(CharSequence text, DateTimeFormatter formatter)

I think it’s much clearer to understand the values ​​of various parameters, using them as separately named methods, rather than a bunch of constructors with very similar types of parameters; you can pretty much guess what the parameters should be for factory methods, while you might have to read Javadoc (shock horror) if they were "unnamed" constructors.

Admittedly, of is the least understood of these names - ofYearMonthDayOfMonth may be required - however, I suspect this is the most commonly used factory method and it is too cluttered to enter this long name all the time.


If you have not read it, step 1 of Effective Java 2nd Edition is all about why and when you need statically static factory methods on constructors. The "named constructor" benefit that I mention here is actually the first benefit of Bloch.

+8
source

All Articles