An interface is used to make sure your code is not tightly coupled. Instead
of saying which class should be given, you say which interface should be
given. This means that a developer has more freedom to implement his own
version of an interface.

Lets say you make a person interface called IPerson. This interface has name
getters and setters:

*public interface IPerson
{
   function get name():String;
   function set name(name:String):void;
};*

Now you want to fill up a classroom. The only thing needed within the
classroom is the name of a person. To add a person to the classroom you call
the addPerson method. The class:

*public class ClassRoom
{
   public function addPerson(person:IPerson):void
   {
        //do stuff with the person
   };
};*

The addPerson method has a person argument typed as IPerson. This means that
I can give any type of instance to this function as long as it implements
the IPerson interface. This interface makes sure that the class given to the
addPerson method has a name getter and setter.

An example of a person:

*class Jack implements IPerson
{
   private var _name:String;

   public function get name():String
   {
       return _name;
   };

   public function set name(name:String):void
   {
      _name = name;
   };
};*

How my class implements the IPerson interface does not matter to the
ClassRoom class. The only thing the ClassRoom class is interested in is the
name getter and setter.

There are a few cases where interfaces come in handy:

- If you want to be able to easialy switch an implementation without
breaking existing code
- If you are using a module and want to call (from the module) a method in
your framework. The framework should be typed as an interface to prevent it
from being compiled into the module.
- If you are not taking care of the actual implementation (another developer
will write this piece of code), you only want a certain method to be
available because you are using this implementation.
- To give freedom to other developers of your code
(mx.core.UIComponentimplements IUIComponent in Flex. And within Flex
everything is typed as
IUIComponent. This means you could write your own version of IUIComponent if
needed.

On a side note. On complex applications I start with writing interfaces for
every part of my application to prevent me from writing actual code and
thinking about the structure. This approach has only personal benefits and
has officially no added value except for the ones named above (in a later
stage).


Greetz Erik

On 4/1/08, Omar Fouad <[EMAIL PROTECTED]> wrote:
>
> Yeah, but I've read that a Class that implements an interface an call
> function from other classes that allready extends other Classes.
> it's like a multiple inheritance. But how can I achieve it?
>
>
_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to