In core Java 8's date and time library, i.e. those classes in the package java.time
, I found a special common trait: there is no public constructor in each class of this package, and thus all classes can be instantiated only through some static
methods such as of
, now
etc. In this sense, the classes inside java.time
resembles the factory design pattern. However, these classes do differ from factory design pattern in that the essence of factory design pattern is to loose couple the code to instantiate various types of objects with a common method (usually static
), and thus the type of the returned object instance is determined until runtime.
However, esp. in the class java.time.LocalDate
and java.time.ZonedDateTime
, the keyword factory was mentioned. One can find the keyword factory from:
So I would like to know did java.time.LocalDate
and java.time.ZonedDateTime
apply the factory design pattern at all? If not, what design pattern did they apply?
The LocalDate represents a date in ISO format (yyyy-MM-dd) without time.
ZonedDateTime is an immutable representation of a date-time with a time-zone. This class stores all date and time fields, to a precision of nanoseconds, and a time-zone, with a zone offset used to handle ambiguous local date-times.
The LocalDate class from the java. time package helps us achieve this. LocalDate is an immutable, thread-safe class. Moreover, a LocalDate can hold only date values and cannot have a time component.
Instant and LocalDateTime are two entirely different animals: One represents a moment, the other does not. Instant represents a moment, a specific point in the timeline. LocalDateTime represents a date and a time-of-day. But lacking a time zone or offset-from-UTC, this class cannot represent a moment.
I think that they wanted to apply the rule
Consider static factory methods instead of constructors
from the book Effective Java by Joshua Bloch.
The goal of that rule is that static methods can be more descriptive as constructors, because a method has a name.
For example:
ZonedDateTime.now();
is more descriptive than
new ZonedDateTime();
is, isn't it?
You might also want to read: Joshua Bloch #Item 1: Consider static factory methods instead of constructors
I see at least three reasons for this choice (and yes that is the factory pattern) :
This way it could in future return a specialized LocalDate
for example, which could be extended only internally (since there are no user-availabe constructors). This way the specialized classes would not be exposed to the clients - only known internally. That seems like exactly your point about different types at runtime - at the moment they don't return any particular subtype, but they might in future.
It would potentially cache some instances for example - I don't think it does that at the moment, but that still remains an option.
Some of the methods have descriptive names: ofYearDay
, ofInstance
, etc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With