Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling Interface uder different package.

 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kevin P Smith
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic