[ajug-members] correct interface programming
Kerry Wilson
kwilson at wmsco.com
Thu Jul 24 22:18:53 EDT 2008
Not weird at all in a real world situation. Consider the following:
interface AccountService {
public boolean save(Account account);
public Account get(String acctNumber);
}
// this class uses a file to save
class AccountServiceFileImpl implements AccountService {
public boolean save(Account account) {
// save an account to a file
}
public Account get(String acctNumber) {
// read an account from a file
}
}
// this class uses a db to save
class AccountServiceDatabaseImpl extends DatabaseServiceImpl implements
AccountService {
public boolean save(Account account) {
Connection conn = getConnection();
// save an account to a database
}
public Account get(String acctNumber) {
Connection conn = getConnection();
// read an account from a database
}
}
abstract class DatabaseServiceImpl {
protected Connection getConnection() {
// return a valid database connection
}
}
This is just one example of programming to interface and I have also
demonstrated how to use inheritance vs. interface. The
AccountServiceDatabaseImpl inherits the getConnection() method from
DatabaseServiceImpl. As you can see if we wanted to swap our storage method
from database to file we could just use a different class. Ideally this
would just be changed in a config file.
Hope that helps!
-----Original Message-----
From: mcdaniel [mailto:521 at ofig.org]
Sent: Thursday, July 24, 2008 9:22 PM
To: ajug-members at ajug.org
Subject: Re: [ajug-members] correct interface programming
good text/message w.morris- true programming-to-the-interface seems
weird but makes sense... did you (w.morris) say that the a method-caller
does not have to implement all, as a class-extender does?
barclay
Webb Morris wrote:
> Barclay,
>
> An interface is a form of contract between the caller and implementation.
The caller can generally only see the methods of the interface class (as
opposed to the implementation class) because the caller casts the
implementation to the interface:
>
> MyInterface mii = (MyInterface)MiiFactory.getAMyInterface();
>
> There is no reason that the caller would have to call all methods of an
interface, it only calls the ones it needs. However, the implementation
class must implement all methods of the interface it implements. If a
method has no use to your situation, you still must implement it, but could
leave it empty or throw a run time exception if it is called.
>
> In this fashion, the caller of an interface does not care what the
implementation class does, only that the contract of the interface is met.
>
> You can only extend one class, but you can implement as many interfaces as
you would like in an implementation class.
>
> I hope this got to the crux of your questions. There are volumes of
information out there about Interfaces, Abstract Classes, and Extension that
would probably be helpful to you. I don't have any of those links at my
fingertips, but I am sure someone will be happy to help.
>
> Regards,
>
> Webb
>
>
>
> ----- Original Message ----
> From: 521 <521 at ofig.org>
> To: ajug-members at ajug.org
> Sent: Thursday, July 24, 2008 3:43:06 PM
> Subject: [ajug-members] correct interface programming
>
> am i saying this correctly?, that which is below... -barclay
>
> {{{
> if i program to an interface, i do not have to worry about the number of
> interfaces that are implemented, do i? but do i not have to use all
methods
> in an implemented interface? i do have to- it's a contract. correct? and
> because you can only extend one class, you have no contract there.
correct?
> can someone see what my question is?
>
> a tree; a tree can grow, a tree can provide shade; and a tree can provide
> lumber for table-tennis.
>
> so my tree class is designed...
>
> then if i am implementing a 'tree'- it depends on which implementation i
use
> thus have a contract to use certain methods
> }}}
>
>
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
>
>
>
>
>
> _______________________________________________
> ajug-members mailing list
> ajug-members at ajug.org
> http://www.ajug.org/mailman/listinfo/ajug-members
>
>
>
_______________________________________________
ajug-members mailing list
ajug-members at ajug.org
http://www.ajug.org/mailman/listinfo/ajug-members
More information about the ajug-members
mailing list