jeudi 28 janvier 2016

Refactoring: when visitor pattern can't be used to replace switch/instanceof

I'm trying to do some refactoring on a piece of code using instanceof in order to determine the type. I think subtyping/polymorphism wouldnt work in this case (and even if it'd be too much of an overkill in this situation) and visitor pattern won't work here since I don't have the possibility to add an accept method to the annotation. Are there any other ways to make this sort of code cleaner/more readable?

for (Annotation annotation : method.getAnnotations()) {
            if (annotation instanceof DELETE) {
                setHttpMethod(annotation.annotationType().getSimpleName(), false);
                parsePath(((DELETE) annotation).value());
            } else if (annotation instanceof GET) {
                setHttpMethod(annotation.annotationType().getSimpleName(), false);
                parsePath(((GET) annotation).value());....

Aucun commentaire:

Enregistrer un commentaire