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:
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.
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.
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!
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.