On Mar 22, 2007, at 16:14 UTC, Arnaud Nicolet wrote:

> May I ask you for a simple example where interface is the only way to
> solve a problem?

I don't know if anything is ever the "only" way to solve a problem, but
I can give examples where it's the best way.

One has already been discussed: dynamically responding to a button or
timer in code.  Suppose actionReceiver were not an interface, but
instead a base class that you'd have to subclass in order to do this
trick.  Then, you would not be able to do it with a Window, or with
your own control (e.g. a Canvas subclass that needs to periodically
update itself), or with some subclass of Sprite or Object3D in a game,
or anything else.  It would be nearly worthless.  But as it is, you can
add the actionReceiver interface to ANY class, regardless of what it's
already subclassing or what other interfaces it may need to implement. 
Very handy.

Another example: suppose you want to write code that can read data of a
particular format, but you want to make it general -- the data could be
coming from a TCPSocket, or a Serial port, or even a BinaryStream, and
there's no reason your code should care.  So you have it accept a
Readable, which is an interface all those classes (plus more the user
of your code could define!) implement.  A BinaryStream IsA Readable; a
TCPSocket IsA Readable; an so on.  So the caller can pass in any of
those, and your code will operate on them just fine.  How would you do
this without interfaces?

HTH,
- Joe


--
Joe Strout -- [EMAIL PROTECTED]
Verified Express, LLC     "Making the Internet a Better Place"
http://www.verex.com/

_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to