What is the best way to program against an anticipated type change. Say, I may have to use Joda DateTime instead of Java Date in the future. Since Java doesn't encourage anti pattern like typedef, what is the best way to ensure an easy refactoring in the future.

+4  A: 

Programming to interfaces and proper layering is the best defensive measure I can think of.

When you separate what is done from how it's done, you leave yourself the possibility of changing implementations without affecting clients as long as the interface is unchanged.

If you have to change the interface, or if you end up doing a new problem altogether, all bets are off. No language has built-in clairvoyance.

+2  A: 

Wrap the API/type in question behind a wrapper interface, and use it only through the wrapper. Then you need to change only the wrapper code to switch the implementation.

This would be the general solution. However, Tom is right in pointing out that Date is so fundamental a type that choosing it should be an architectural decision, not to be changed often.

Péter Török
Would the downvoter mind giving an explanation?
Péter Török
+5  A: 

Certainly the "Single Responsibility Principle" or something near it and encapsulation should help limit dependency creep.

However, choice of basic types is an architectural decision. If you were to change string or integer types, you'd expect change across your code. Date isn't that much different.

Tom Hawtin - tackline
+1  A: 

Sound to me like you want to accomplish "dynamic reclassification",
I think you should be able to achieve this with the State pattern.
Please review an example here: http://cnx.org/content/m17225/latest/

+1  A: 

You could try eclipse's IAdaptable


My take on this is that a good Java IDE can drastically reduce the pain of a large-scale change of data types. You search for points at which your code uses the old class, replace with the new class, and fix the ensuing compilation errors, and repeat. When you are done with the changes, run your unit / regression tests, fix the bugs and (touch wood!) you are done.

OK ... it may not be that simple. In particular:

  • Reflective dependencies can be difficult to find.
  • Dependencies in external wiring / configuration file can be difficult to find.
  • A large-scale application comprised of components written / maintained by lots of people can be problematic. (You cannot really just hack on everyone elses code.)
Stephen C
+1  A: 

What is the best way to program against an anticipated type change.

Keeping your design as simple and clean as possible, and maintaining a good suite of unit tests.

This helps you with all future changes, including unanticipated ones (which are much more common).

Michael Borgwardt
agree, but I wasn't that generic, but specific to "type" changes.
@bsreekanth: the generic answer still applies. Much more so than the other answers, IMO. For the example you give, any kind of wrapping or layering trying to anticipate the change would end up requiring more work (and introduce more bugs) than simply doing the change.
Michael Borgwardt