0

In the java.time.LocalDate of Core Java 8, the format method is implemented as the following:

@Override  // override for Javadoc and performance
public String format(DateTimeFormatter formatter) {
    Objects.requireNonNull(formatter, "formatter");
    return formatter.format(this);
}

Similarly, the static parse method is implemented as the following:

public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) {
  Objects.requireNonNull(formatter, "formatter");
  return formatter.parse(text, LocalDate::from);
}

This is different from composition or aggregation because, LocalDate is immutable, and thus adding an instance variable to the LocalDate object would break the immutability. From the aspect of responsibility, it doesn't seem to be delegation, either, according to the book Refactoring: Improving the Design of Existing Code, where the delegation also requires a corresponding instance variable

Question: What design technique or pattern is applied? is it just the weakest dependency? or also a delegation? or something else?

Rui
  • 3,454
  • 6
  • 37
  • 70
  • It’s not composition or aggregation, but it definitely is delegation. But what actual programming problem does giving the thing a name solve? – Holger Sep 20 '18 at 06:15
  • Thanks for your reply, this is good design technique in the design of immutable objects – Rui Sep 20 '18 at 06:19
  • @Rui hmmm how is delegation good for that? Immutability means to retrict access to data – Eugene Sep 20 '18 at 09:13
  • @Eugene IMO, the design of `LocalDate.format()` and `LocalDate.parse()` is dynamic in that the given `LocalDate` instance can be *formatted* or *parsed* depending on the `formatter` parameter, say when calling the `format` or `parse` during different time different `formatter` can be given to get different result, and the *immutable* feature of `LocalDate` keeps valid in any case. So I said the relationship between `LocalDate` and the `DateTimeFormatter` is *weak dependency* and *delegation* – Rui Sep 20 '18 at 09:19

0 Answers0