Have java.time.LocalDate and java.time.ZonedDateTime applied a factory pattern

In the Java 8 date and time base library, that is, in the classes in the package java.time, I found a special common feature: in each class of this package there is no public constructor, and thus, all classes can be created only through some methods static, such as of, nowand etc. In this sense, the classes inside java.timeresemble the factory design pattern. However, these classes differ from the factory design pattern in that the essence of the factory design pattern is to untie the code to instantiate various types of objects using a common method (usually static) and thus the instance type of the returned object is determined before time fulfillment.

However, especially. in the classroom java.time.LocalDate, and java.time.ZonedDateTimeit mentioned the keyword factory . The factory keyword can be found in:

So, I would like to know that java.time.LocalDateand java.time.ZonedDateTimeused the factory design pattern at all? If not, what design pattern did they apply?

+6
source share
2 answers

I think they wanted to apply the rule.

Consider static factory methods instead of constructors

from Joshua Bloch's Effective Java Book .

, , .

:

ZonedDateTime.now();

,

new ZonedDateTime();

?

: Joshua Bloch #Item 1: factory

+2

( , factory):

, , , LocalDate, ( , ). , - . - - , .

, , , , .

: ofYearDay, ofInstance ..

+2

All Articles