2

I am trying to understand a Java code that I received (I am a C# programmer)

public interface BaseDA {
    ...
}

public class BaseDAImp extends HibernateDaoSupport implements BaseDA {
    ...
}

public interface TxnDA extends BaseDA {
    public Txn getTxn(long id);
}

public class TxnDAImp extends BaseDAImp implements TxnDA {

    public Txn getTxn(long id) {
        ....
    }
}

And in another class it is used this way

protected void ShowTxn(long int) {
    TxnDA txnda = (TxnDA) appContext.getBean("txnDA");
    txnda.getTxn(id)
}

All classes extend a base class and implements an interface. This is almost done for every single class in the java code.

Question 1 - Can someone please explain this code to me? I already know we use interfaces (in C#) when we might want to couple software, plugin, hide implementation details.

2 - As none of above conditions are true for this java library, is it safe to modify the code like below?

public class BaseDA extends HibernateDaoSupport {
    ...
}

public class TxnDA extends BaseDA {

    public Txn getTxn(long id) {
        ....
    }
}
3
  • 1
    I think pretty much the same discussion can be had for C# code. Not terribly language-specific. Commented Nov 13, 2015 at 8:25
  • @Thilo, I am programming C# since 2001 and haven't seen someone make an interface for every single class in code, but this is not the first library in java that I receive and is written like this. I was wondering if this is a best practice, necessity or something that I'm not aware of . Commented Nov 13, 2015 at 8:38
  • 2
    I saw libraries like this in C# also. The practice would be good if needed, that's if you need more than one impl for same capability. In this case I think the lib is about data access, you could basically re-implement interfaces for another db engine Commented Nov 13, 2015 at 8:41

1 Answer 1

1

This is a classical approach which, in theory, should ensure the code being easily expandable and loosely coupled. If you look in old Java books you will find this in many code-architecture examples.

However, this approach is NOT clean, not really profitable and you definitely should not do it yourself, unless it is your preferred style (which it is not I assume, as you are trying to understand it).

Software engineering has evolved a great deal and the modern approach is to focus on the code being understandable and, well, clean. Writing it in the above way will result in very hard debugging and a lot of time required for a new programmer to get the hand of the project.

On the other hand, this is simply an architectural decision, so it is not straightforwardly 'bad'. It is simply outdated :-)

As for Question #2 - yes, this seems reasonable, although I can not guarantee it will work, you did not provide enough code nor context.

Sign up to request clarification or add additional context in comments.

5 Comments

I wouldn't even call it outdated. It's overkill in this scenario, probably introduced with exactly the idea that there'd possibly be multiple implementations in the future which never materialised. For a public interface to an API that needs to be accessed from outside the application it's perfectly feasible to do it like this even if you know there's only going to be one implementing class too, but then of course you'd package the interfaces in their own jar.
Sure, it is an overkill, but it was actually considered the best approach. Leaving the code open for extension and so on. However, there are new patters for that now, which do not leave you with thousands of single-use interfaces.
@jwenting, I guess your assumption is right, the classes and interfaces are packaged in same jar file.
Actually there is not much code in each class, I think some of the classes that are implemented can be merged too, because their functionality are just used once. and you are right, we are having trouble with debugging an issue in application and tracing to source of class is not easy because all declarations are to interfaces and not classes.
Yeah, with projects like this the only usable debugging option is using a break-point and following the execution to the actually-used class... This sucks and is really counter-productive :-( So yes, flattening the package a bit is a good idea, you can always un-flatten it (and in a better way) later if you need to add some alternative implementation :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.