Forums Register Login

Calling Interface uder different package.

+Pie Number of slices to send: Send
Is it OK to call and Interface from a different package, or is this 'bad practice'?

I ask this because i have a simple database pooling project, which has the following structure:

+ com.myproject.connection
- ConnectionPoolmanager (well it's a DB connection pool manager)
- ConnectionManager (Uses ConnectionPoolManager to create/drop DB connecitons, returns Connection CONN)
- ConnectionInterface (interface to create/drop connection methods in Connectionmanger class)
+ com.myproject.sql
- SQLBuilder (builds SQL query based on fields, colums, table)

And basic code doing the following...
SQLBuilder.class:-
import com.myproject.connection.ConnectionInterface;
import com.myproject.connection.ConnectionManager;
...
ConnectionInterface Icon = new ConnectionManager();
...

My question is, what's the point of the Interface (or am I using it completely wrongly) because surely I can do teh exact same thing with the fllowing code:

import com.myproject.connection.ConnectionManager;
...
ConnectionManager Icon = new ConnectionManager();
...

Just I don't see the benifit of the Interface, it just seems to be another 'step' for the sake of it, I know it means there is no direct conneciton to the Connectionmanager class but still don't see the benifit.

Cheers
+Pie Number of slices to send: Send
Since you're certainly familiar with JDBC, take a few minutes and think about how JDBC works. Connection, Statement, ResultSet, and the like are all interfaces. Quick -- name some classes that implement these interfaces!

You can't, right? That's because, although each JDBC driver implements all these interfaces for you, the classes themselves are hidden behind factory methods like "getConnection()". Here's where the power of interfaces comes out: you can write code using only the interfaces, and it will work with many different implementations, without changing your code at all! Note the important point that your JDBC client code doesn't contain the names of any of the driver classes -- this is what makes the whole scheme work.
+Pie Number of slices to send: Send
So am I using (understanding) the Interface correctly?

Is the purpose of the Interface really just to 'hide' my ConnectionManger (and in effect ConnectionPool manager) from my SQLBuid class?

import com.myproject.interfaces.*;
...
ConnectionInterface iconn = null;
...
iconn.getConnection();
[ October 01, 2007: Message edited by: Keith Seller ]
+Pie Number of slices to send: Send
 

Originally posted by Ernest Friedman-Hill:
Since you're certainly familiar with JDBC, take a few minutes and think about how JDBC works. Connection, Statement, ResultSet, and the like are all interfaces. Quick -- name some classes that implement these interfaces!


com.mysql.jdbc.Connection
com.mysql.jdbc.Statement
com.mysql.jdbc.ResultSet


I only know these ones since I put the MySQL connector in my \lib\etc subfolder of my JRE, and Eclipse keeps wanting to import these instead of the interfaces.
+Pie Number of slices to send: Send
 

Originally posted by Keith Seller:

Is the purpose of the Interface really just to 'hide' my ConnectionManger (and in effect ConnectionPool manager) from my SQLBuid class?



Yes, that's one important purpose of interfaces. This only makes sense, of course, if it's possible that at some point there will be more than one class that implements ConnectionInterface. If there will never be, then perhaps using an interface is unnecessary.

Of course, "never" is a dangerous word.
Do the next thing next. That’s a pretty good rule. Read the tiny ad, that’s a pretty good rule, too.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 906 times.
Similar Threads
Question about ConnectionFactory implementation
Image inside jar file
In ConnectionPoolManager how synch two diff Lists?
Singleton Pattern
jdbc 2 pool size
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:33:44.