refactoring
Eclipse refactoring voodoo
Assuming you have a class that handles page navigation, and you need to introduce some extended behaviour in order to have a second type of navigation on the same page, you have two choices:
- Add code to the existing class, add hooks for the distinction of the navigation type in every place you need them, or
- Add a second class that represents the new kind of navigation.
Assuming additionally that you would like to keep the JSP tags for your view layer as they are, and only add a minimal amount of code there, what would you do?
There are, once again, at least to ways to do this:
- Put all code for the first kind of navigation inside an if statement, put all code for the second type of navigation into the else block, duplicate lots of code, just because the types are not the same
- Use interfaces.
Option (2) enables you to write code like this:
I_Navigation nav; if(navType == NavType.ONE) { nav = FirstTypeOfNavigation.getInstance(); } else if(navType == NavType.TWO) { nav = SecondTypeOfNavigation.getInstance(); }
And then, later on, to continue using almost exactly the existing code without changing it much.
So, how would you go ahead at creating the interface I_Navigation?
In Eclipse, it’s easy to generate the interface from the existing navigation class using refactoring. Simply select the class, right-click, select Refactor -> Extract Interface, choose your options, and be happy. Nice, isn’t it? This refactoring saves you something like an hour of typing. I love Eclipse. :)