Use visitor pattern instead of instanceof