1

My class implements Connection interface. In my class i have got Object that implements Connection. Now i want to implements all my class method from Connection interface with my object in that class. Is there any fast method?

@Override
public Statement createStatement() throws SQLException {
    return connection.createStatement();
}

I can do that thing for all methods, but it is really boiler code.

@Edit I know i can write getter for that object, but I do not want to do that.

B_Osipiuk
  • 888
  • 7
  • 16
  • Manually: the IDEs often can create _delegating_ and certainly _override_ methods. So make some class implementing Connection. Let the IDE in _your_ class generate the missing methods calling super methods. Then replace `super.` with `object.`. – Joop Eggen Jan 14 '19 at 10:24
  • 1
    By the way classes around JDBC ones are very tempting in my experience, but are almost certainly not a good idea. Even custom JDBC drivers can do it differently. – Joop Eggen Jan 14 '19 at 10:27
  • Yeah @JoopEggen, this can work. I will use it if the proxy will be too complicated for me. I want to wrap connection, because i use OracleConnection wrapper, and I found [there](https://stackoverflow.com/questions/8225921/unwrap-to-oracleconnection#comment31597216_13879502) that I should close wrapped connection, not the unwrapped. I could use my new class as simple connection (using unwrapped connection), but when i want to close it, it will close wrapped connection. I do not know it will work, but i will try. – B_Osipiuk Jan 14 '19 at 10:48
  • @Hulk I think i cannot use it, because i cannot extend class. I just have interface. Or maybe i am wrong and missing something? When i implements Connection, then i have to implement all the methods. – B_Osipiuk Jan 14 '19 at 11:05

1 Answers1

0

This is a typical case for the proxy design pattern.

https://en.wikipedia.org/wiki/Proxy_pattern#What_solution_does_the_Proxy_design_pattern_describe?

Your object is the proxy as it is the main receiver of the events, and it has to forward these events to the final object (your class).

Hichem BOUSSETTA
  • 1,791
  • 1
  • 21
  • 27