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?
Possibly, but without a bit more information about the objects you're getting and the kind of initialisation you're doing based off them, it's hard to tell - this is one of those "it depends" answers.
以上就是Simple/elegant way to do object to object transformation in Java?的详细内容，更多请关注web前端其它相关文章！