I have to take over and improve/finish some code that transforms Java objects from a third party library into internal objects. Currently this is done through a big if-else statement along the lines of:
if (obj instanceOf X) { //code to initialize internal object } else if (obj instanceOf Y) { //code to initialize different object } else if (obj instanceOf Z) { //more init code } ...
Personally I don’t find this solution satisfactory; it’s long and messy and to make matters worse many of the if-else blocks contain more if-else blocks dealing with subclasses and edge cases. Is there a better solution to this problem?
Create an interface like this
and implement it for each object of X,Y,Z. Then put all known converters into a Map and get happy!