Java – Static Methods vs Interface Implementation

designdesign-patternsinterfacesjavastatic methods

Here is my problem:

I stared to create an e-commerce web site info collector.

So I created a parser for each site.
The parser class is stateless.

I have got methods like:

getItemPrice(WebElement page)
getItemTitel(WebElement page)
getItemDescription(WebElement page)
etc..

While creating a JUnit tests for that the convenient way was to make them static methods.

While creating the next parser for the next page ,again I needed the same methods.
So it just popped out that an interface of an IItem is required here for future changes, etc.
Therefore I have a problem to set these methods as static.
Which require to create an instance of a stateless class which lead to simple factory to create these instances etc.

I'm not sure whether this Interface is causing me too much of an overhead or not.

Any ideas?

Best Answer

I don't think there will be too much overhead when using an interface. You can even argue that the use of static methods could generate more overhead in this case, depending on how much different implementations you want to support and how many methods you need.

When using static methods, you need to have some logic which implementation you have to use, which is basically a big switch statement for every method. This can be simplified by putting a wrapper around all those implementations, but evaluating the same switch statement over and over again for each method you want to call is not really advisable.

With the use of interfaces this will be reduced to just one evaluation of the switch statement (in the factory).