I think Hans (and Claus ;) explained it very well, so just to
reiterate, sometimes it's just useful to specify methods instead of
ancestors as an argument type.

For an example, have a look at flash.utils.IDataInput. It's
implemented by ByteArray, Socket and URLStream. Obviously, a ByteArray
and a Socket don't have much in common, in terms of how they do their
thing, but they both are used to read (and write) data. Often, that's
all you need to know.

Say you write a class that draws a pretty chart. It has a method that
reads a series of values by repeatedly calling readInt(), which is
guaranteed by IDataInput, and draws that data. If your method takes a
ByteArray, it works fine for preloaded or locally stored data. If it
takes a Socket, it works for a live data stream off the net. But if it
takes an IDataInput, it can do both, without any additional effort --
you pass it a /data source/, and decide at runtime where you get it
from.

Another example could be some code that calculates the distance
between two coordinates. It takes two objects that both have an x and
a y property. You'd use essentially the same code to calculate the
distance between Points or Sprites, but you'd have to write two
methods: There is no ICoordinate interface (requiring an x and y
property) that they could implement. If there was, you'd only need one
method to do it, and on top of that it could also calculate the
distance between a Point and a Sprite, as a free bonus.
(Calculating a distance may seem like a trivial example, but it could
also be, say, a quad tree for hit testing.)

To solve Hans' adapter problem, you'd use the IContinentalOutlet
interface, which is required by his continental player to work. The
various adapters he collected during his travels all look different
and even are from different vendors. But they all implement the
IContinentalOutlet interface, /how/ they do it doesn't matter.

You don't have to work in a team to appreciate interfaces. They simply
make code more flexible, that's a good thing, use them where
appropriate (I'd even go as far and say "when in doubt, use an
interface").

Mark




On Tue, Aug 26, 2008 at 12:44 PM, Hans Wichman
<[EMAIL PROTECTED]> wrote:
> Hi,
>
> interfaces are pretty simple in reality and they are everywhere.
> Imagine every wall outlet looked different, and not only different, but that
> in order to use them, you had to remove the outlet first, take a look at the
> wiring and then bolt it back on with you finally knowing how to use it.
>
> Would we accept that as a fact of life? No sir.
> So what would we do?
>
> We'd come up with a brilliant plan to take over the world, and it involves
> agreeing on what a wall outlet looks like and what we expect it to do. So no
> matter the device I want to plug in, as long as my device agrees with the
> interface it has been offered, it's good to go.
>
> Will it work without problems everywhere and always? Nah try to plug in your
> dutch mp3player in the uk, no go, but within a certain context they can work
> wonders.
>
> hth :)
> JC
>
> On Tue, Aug 26, 2008 at 9:49 AM, Cor <[EMAIL PROTECTED]> wrote:
>
>> Very good, Claus,
>>
>> Do you have a visual of this... LOL
>>
>> Regards
>> Cor
>>
>> -----Original Message-----
>> From: [EMAIL PROTECTED] [mailto:
>> [EMAIL PROTECTED] On Behalf Of Claus Wahlers
>> Sent: dinsdag 26 augustus 2008 9:42
>> To: Flash Coders List
>>  Subject: Re: [Flashcoders] A Question that I've been asking for years!!
>>
>> An example is worth a thousand words.
>>
>> public interface IBounce {
>>    function bounce():void;
>> }
>>
>> public class Balls implements IBounce {
>>    public function bounce():void { }
>> }
>>
>> public class Boobs implements IBounce {
>>    public function bounce():void { }
>> }
>>
>> var balls:Balls = new Balls();
>> var boobs:Boobs = new Boobs();
>> doSomethingWith(balls);
>> doSomethingWith(boobs);
>>
>> function doSomethingWith(bouncyObject:IBounce):void {
>>    bouncyObject.bounce();
>> }
>>
>> Cheers,
>> Claus.
>>
>> Omar Fouad wrote:
>>
>> > This could seem weird...
>> > But what the hell is an interface!!!???????? I've read lots of books and
>> > posts without getting the answer. I bought "Essential AS3" to read about
>> > interfaces and he says that helps for multi inheritance. In other places
>> I
>> > read that it is a "deal" to ensure that a class has some methods and so
>> on.
>> > But what is the real benefit that I can come out with using
>> interfaces????
>> >
>> > Maybe that is stupidity or I am not smart enough to get the concept but
>> > believe me... its is been two years now!!
>> >
>> > Please Help!!!
>> >
>>
>>
>> _______________________________________________
>> Flashcoders mailing list
>> Flashcoders@chattyfig.figleaf.com
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
>>
>> _______________________________________________
>> Flashcoders mailing list
>> Flashcoders@chattyfig.figleaf.com
>> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>>
> _______________________________________________
> Flashcoders mailing list
> Flashcoders@chattyfig.figleaf.com
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to