When you define an interface for your classes, it helps with dependency injection. Your Spring configuration files have nothing about the interfaces themselves in them - you just specify the class name.
But if you want to introduce another class that offers "equivalent" functionality, using an interface really helps.
For example, saying that you have a class that parses the contents of a website, and you enter it using Spring. If the classes you enter in order to find out what the actual class is, then to change it, you will have to change the whole code to use another specific class. But if you created the Analyzer interface, you can just as easily insert the original DefaultAnalyzer as you could mock DummyAnalyzer or even another that does essentially the same thing as PageByPageAnalyzer or something else. To use one of them, you just need to change the class name that you enter in the Spring configuration files, and not go through the classes that change the code.
It took me about one and a half projects before I really started to see the utility. Like most things (in corporate languages) that eventually become useful, at first it seems like a pointless addition of work until your project starts to grow, and then you know how much time you saved by doing a little more work.
Sean Schulte Nov 02 '08 at 5:04 2008-11-02 05:04
source share