I am reading Sedgewick’s book on algorithms. In the chapter regarding Abstract Data Types (ADT), they defined two different date datatype implementations, one which takes more space, but less compute time, other saving space, but takes more compute time to provide the same results.
class Date {
private final int month;
private final int day;
private final int year;
public BasicDate(int m, int d, int y) { //constructor
this.month = m;
this.day = d;
this.year = y;
}
public int month() {
return month;
}
public int day() {
return day;
}
public int year() {
return year;
}
public String toString() {
return month() + "/" + day() + "/" + year();
}
}
And the second implementation with more compute cycles, but less memory needs :
class Date{
private final int value;
public SmallDate(int m, int d, int y) {
this.value = y*512 + m*32 + d;
}
public int month() {
return (value / 32) % 16 ;
}
public int day() {
return value % 32 ;
}
public int year() {
return value / 512 ;
}
public String toString() {
return month() + "/" + day() + "/" + year();
}
}
And then they say this : Java has various advanced language mechanisms for maintaining multiple implementations without needing to change client code, but we use them sparingly
because their use is challenging (and even controversial) even for experts, especially in
conjunction with other advanced language features that we do value (generics and iterators).
What are those features? And how can I maintain these two implementations simultaneously?