0

I want to create a DAO class named BaseDAO that should have the JPA and JDBC capabilities in Spring. I mean, I want to extend JPADAOSupport and JDBCDAOSupport classes of spring in to my BaseDAO class. I am aware that multiple inheritance is not an option in Java.

I have created two separate Base classes like BaseJPADao and BaseJdbcDao extending the respective classes. Is it possible to have a single class to extend both? Is there any design pattern solving this issue. Please advise.

Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Anand
  • 727
  • 3
  • 14
  • 39

3 Answers3

2

Why don't you have a DaoGateway bean having injected the actual JPA DAO and the JDBC DAO beans.

This gateway can then decide which DAO to delegate a given request (to JPA or to JDBC).

You should always favour composition vs inheritance when reusing functionalities.

Community
  • 1
  • 1
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
  • Thanks for your comment. I did use injecting the beans in spring. But what I want to do is that.... I have BusinessDAO which will use JPATemplate feature and SimpleDAO will use JDBCTemplate feature. I also have a BaseDAO where I have some other methods, which needs to be implemented/used by the BusinessDAO and SimpleDAO. What I thought was, we can extend JPADaoSupport and JdbcDaoSuport in BaseDAO and then the other dao classes can extend the BaseDao. Hope you got my question. – Anand Jun 23 '14 at 12:27
  • You can't extend both JPADaoSupport and JdbcDaoSuport because Java doesn't allow multiple class inheritance. The BaseDao may define both JPADaoSupport and a JdbcDaoSuport as proeprties, and when you override the class you can get which ever you want. – Vlad Mihalcea Jun 23 '14 at 12:46
0

no it is not. if it was possible, you would still have the same result as in one class extending JPADAOSupport and JDBCDAOSupport, which you yourself say you know is not possible because multiple inheritance is impossible.

you can write to an interface, and provide two implementations, though.

Stultuske
  • 9,296
  • 1
  • 25
  • 37
  • "you can write to an interface, and provide two implementations, though." - And implement the methods in both the interfaces?. This means myClass is both JPADAOSupport and JDBCDAOSupport . Which is wrong from a design perspective. Also the OP could as well implemnet all the methods without implementing the interface. Interfaces dont help in code reusability. – TheLostMind May 28 '14 at 07:29
  • have I said they do? but: InterfaceType obj = new Implementation1(); doesn't work? obj = new Implementation2(); it makes it possible to have both, indeed, not in one class, but for one variable. – Stultuske May 28 '14 at 07:39
  • As for what's *right and wrong* from a design perspective, That's an opinion. You asked us to answer your question and you have lots of offers. – Christian Bongiorno May 29 '14 at 01:33
0

This would be easy to do with delegation if they both had interface level access you want:

public class MyUberClass implements WhateverJPADAOSupportDoes, WhateverJDBCDAOSupportDoes {
    private JPADAOSuport jpa;
    private JDBCDAOSupport jdbc;

    // now implement all methods specified by the interfaces on the class signature and delegate to their respective member
}

But it seems you want access to all of their public methods. As there is no interface for both you can do the same as above but it can't be of both types simultaneously. The language expressly denies you this.

Your only other option is to create an adapter interface that your code can rely on and then use the combination delegation. If you're hoping to have one class that you can just drop in as a substitution for both then the answer is you can't.

Christian Bongiorno
  • 5,150
  • 3
  • 38
  • 76