Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the main purpose of the new interface java.time.InstantSource over existing abstract class Clock?

A new interface java.time.InstantSource was added in Java 17. What is the use case of that additional abstraction if all implementations of that interface are Clock implementations too anyway?

like image 206
Олег Мельник Avatar asked Sep 15 '21 20:09

Олег Мельник


People also ask

Why do we use interface instead of abstract class in Java?

The short answer: An abstract class allows you to create functionality that subclasses can implement or override. An interface only allows you to define functionality, not implement it. And whereas a class can extend only one abstract class, it can take advantage of multiple interfaces.

What is a difference between abstract class and interface in Java?

Abstract Class Vs. Interface: Explore the Difference between Abstract Class and Interface in Java. The Abstract class and Interface both are used to have abstraction. An abstract class contains an abstract keyword on the declaration whereas an Interface is a sketch that is used to implement a class.

Can you explain what is called interface in Java and point out main difference between abstract class and interface?

We can run an abstract class if it has main() method but we can't run an interface because they can't have main method implementation. Interfaces are used to define contract for the subclasses whereas abstract class also define contract but it can provide other methods implementations for subclasses to use.


1 Answers

With thanks to NoDataFound for the link in the comment (link repeated at the bottom of this answer). The “bug” report by Stephen Colebourne (original developer of java.time) says:

Problem

Since java.time was first added in Java 8, it has become apparent that there is a missing concept - a source of Instant independent of time zone. Put simply, if the only thing you want is an Instant, then Clock isn't the right API because it forces you to think about time zones.

A good architectural design for time-based code would have a separation between the abstraction of the OS clock (dependency injected for unit testing), and the time zone linked to user localization. Passing these two things around separately is key. To achieve this as it stands, developers must either write their own TimeSource or InstantSource interface, or use Clock and "hold their nose" to ignore the time zone held within.

A Supplier<Instant> obviously performs similar functionality, but it lacks discoverability and understandability. Plus, injecting generified interfaces tends to be painful.

In my words: The Clock class was introduced as a source of the current time. Conceptually a source of the current time should be independent of time zone. The Clock nevertheless has a time zone. Possibly for practical purposes so that you can draw all sorts of date-time objects from it: LocalDate, ZonedDateTime, LocalTime, etc.

The InstantSource interface asked about realizes the clock concept that I just described: a source of the current time that is independent of time zone. For when a clock with time zone is too heavyweight for your purpose.

One of the purposes of both Clock and InstantSource is testability: With those you can control which time is considered “the current time” when your tests are run, which is often required when you want reproducible tests.

… if anyway all implementations of that interface are Clock implementations also?

I haven’t checked whether all InstantSource implementations shipped with Java 17 are Clock subclasses. Even if this is the case, (1) it is not the point, and (2) there’s no one stopping you implementing an InstantSource that is not a Clock. Abstraction is a key concept in programming. If you want only Instants, then you want to program against a simple interface that can give you only Instants. InstantSource abstracts away the fact that the object you are using would also be able to play the rôle of a Clock in some other context. The InstantSource interface is a school example of applying the well-respected Façade design pattern (link below). With Stephen Colebourne’s descriptive words from the bug system, the interface frees the programmer from „holding their nose” while obtaining Instants.

Links

  • Add java.time.InstantSource in the OpenJDK Bug System.
  • Facade pattern on Wikipedia.
like image 185
Ole V.V. Avatar answered Nov 15 '22 08:11

Ole V.V.