A Flash Developer Resource Site

Results 1 to 18 of 18

Thread: abstract classes and interfaces questions

  1. #1
    Senior Member
    Join Date
    Jul 2008
    Posts
    418

    abstract classes and interfaces questions

    i'm reading a book about design patterns. i'm reading about the factory method pattern now. i got some questions.

    he mentions that abstract classes don't exist in AS3, so you'll have to make normal class and treat it as if it we're abstract. i get that, but i don't really understand what an abstract class exactly is. is it the same as an interface?

    and why would you want to use interfaces? then you have a list of methods, but that do nothing. then you have to define those functins in the implementing class, as if the interface doesn't exist. then why would you need the interface?

    and last: i dont get delegating. is it referencing an interface? why would you want to do that?

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    An abstract class contains methods. A regular class extends the abstract class. You can now make use of the methods without rewriting them all the time but referring to the abstract class. Or if you want to change a method of the abstract class you can override it in your subclass. So if you have a complex application and you want to change something you can either add a new subclass without touching any of the other classes or you make a change in only one subclass.

    An example of an abstract class is the CellRenderer class for several components like List or datagrid for example. With a new class, which extends the CellRenderer class and overriding a method you can easily change your application without changing the components themselves.

    http://flashscript.biz/flashas3/data...enderer_2.html

    Interfaces are good to tie classes to one application. Interfaces are nothing else than a collection of methods. If you create an instance of a class belonging to an interface, you can use the name of the interface as the datatype.
    Last edited by cancerinform; 10-25-2008 at 06:43 AM.
    - The right of the People to create Flash movies shall not be infringed. -

  3. #3
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    ahh. abstract classes are clear now.

    but why is it nessesairy to use the name of an interface as the datatype? is this only for like debugging? or can you actually do something with it?

    ps. when you do not state super() in a class constructor, will it use the given paramaters for it's own constructor for it's inherited constructer?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Interfaces are useful as datatypes because they allow you to treat distinct classes as a single common type. For instance, if you have a method which takes some object and needs to call that object's "sort" method. It would be really neat to know that that object MUST have a sort method.

    Code:
    public interface Sortable {
      function sort():void;
    
      function getFirst():*;
    }
    Code:
    public function getLeast(s:Sortable):*{
      s.sort();
      return s.getFirst();
    }
    Now, you can pass getLeast a List, or a Tree, or anything else that implements Sortable, knowing that you're not going to get "no such method". And you also know at compile time that if you try to give it a Sprite, it won't let you.

  5. #5
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    It is not necessary but it will bind the class to the interface and this can be an advantage if there are different interfaces. For example in a complex application using the interface as a datatype will identify a class as belonging to one interface.

    You do not necessarily have to state super. That will be added by default. Think about extending to the Sprite class for example nobody adds super. However you need to state super when the there are arguments in the Superclass constructor.
    - The right of the People to create Flash movies shall not be infringed. -

  6. #6
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    ok i get it.
    but why is it helpfull to know the datatype of an object?

  7. #7
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    Well, for one thing, strict typing your variables greatly speeds up an application because casting data to different types on the fly...and thereby leaving open the possibility of any object having any number of dynamic properties -- slows down the player.
    But more than that, let's say you have an 'abstract' class that defines a data tree and you want to display it in several different ways; as pull-down menus, as an openable list, and as a tree drawing, for example. Rather than having each of those graphics classes treat the basic data tree as just an object, it would be helpful for them to know that each node in the tree had certain properties definitely in common that it could call on. So if each tree node is an instance of a node class, and that class implements an interface with a functions like "openme" and "closeme", then if at some later point in your application you need to add different kinds of nodes with different properties, well, as long as they implement the node interface, you can just tell your graphics class that they are basic nodes, and then cast them to more specific types of nodes later on.

  8. #8
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    that makes sense.
    but why would you delegate it? (if i am right, that is having a reference to an interface?)

  9. #9
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    You don't have to create an interface. All classes that extend the base class are expected by the compiler to have the functions of the base class. But in situations where you may have more than one base class where each needs to have certain functions in common, and particularly if you want other coders to be able to add their own classes to the package, it's a useful organizational tool. It makes for cleaner code.

  10. #10
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    yeah i see.
    it's just that i read in the book actionscript 3.0 design patterns, that it is useful to have within an abstract class a reference to an interface.
    i dont mean
    PHP Code:
    public AbstrClass class implements interface1 
    i mean
    PHP Code:
    //Composition: Reference to two interfaces
    var playMedia:IPlayMedia;
    var 
    recordMedia:IRecordMedia

  11. #11
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    Well, uh, what does the book suggest is the usefulness of doing that?
    I don't see the point. I've never seen that in anyone's code I've come across, either. You can have a class that implements two interfaces:
    Code:
    public AbstrClass class implements Interface1, Interface2 {

  12. #12
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Composition is a useful technique (not even complex enough to call a pattern), but there's really no reason it needs to be associated with an abstract class. Any old class can have a reference to another class.

    It's often used when extending Proxy. Your new class will extend Proxy and have an instance of some other class that it is wrapping and delegating method calls to.

  13. #13
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    but it's not a class it's an interface :
    this is the class the book gives:
    PHP Code:
    //Abstract class
    class Media
    {
    //Composition: Reference to two interfaces
    var playMedia:PlayMedia;
    var 
    recordMedia:RecordMedia;
    public function 
    Media( ) {}
    public function 
    doPlayMedia( ):void
    {
    //Delegates to PlayMedia
    playMedia.playNow( );
    }
    public function 
    doRecordMedia( ):void
    {
    //Delegates to RecordMedia
    recordMedia.recordNow( );
    }

    and this is the interface
    PHP Code:
    //Interface for playing media
    interface PlayMedia
    {
    function 
    playNow( ):void;

    this class extends the abstract class:
    PHP Code:
    //Concrete Media subclass: Audio
    public class Mp3 extends Media
    {
    public function 
    Mp3( )
    {
    //Inherits composition references from superclass
    playMedia = new PlayAudio( );
    recordMedia = new RecordAudio( );
    }


  14. #14
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yeah, you can use either an actual class or an interface as a type. It doesn't change the meaning of the pattern.

    Composition = This class has an instance of a <blank> where blank is some type. It could be a concrete class, an abstract class, or an interface.

    In the example you gave, the point of Media is to abstract the media controlling logic from the implementation of a specifc medium. It makes writing Mp3 a matter of filling in just a few blanks. And if you needed to write a Divx class, it would also be a matter of filling in just a few blanks. Assuming you have Divx implementations which implement PlayMedia and RecordMedia.

  15. #15
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    i see.
    however, how can you acces a function within an interface? the interface's function is not defined.

    ps. about composition. i just did a benchmark. when i do the functions within a class i get about 800 ms for doing certain functions.
    when i reference to an instance of a class within another class and do:
    PHP Code:
    var bla1:AccFricDirSpd = new AccFricDirSpd();
    with (bla1){
    //functions

    it takes 4500 ms. how come with takes more time than just bla1.function? if u use with u only need 1 referenct, right? just this one time. but when u repeat bla1.function 1000 times, the comp needs to search bla1 1000 times, right?
    Last edited by omniscient232; 10-28-2008 at 01:54 AM.

  16. #16
    Senior Member joshstrike's Avatar
    Join Date
    Jan 2001
    Location
    Alhama de Granada, España
    Posts
    1,136
    You're not actually calling the function as defined in the interface, in that example...you're calling the function in the instance of PlayAudio or RecordAudio -- not in PlayMedia or RecordMedia. But by having typed those vars to the interface, you're letting the compiler know that they can be instantiated by an extending class to anything that implements that interface.
    If you didn't type var playMedia to the interface, you'd either have to type it to PlayMedia, in which case you couldn't later instantiate it as another class that didn't extend PlayMedia (would be illegal coercion) ... or you'd have to type it as * or Object, or else not define it beforehand and leave the class dynamic; all three of those things would slow your code significantly.

  17. #17
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I suspect that in your benchmark code you are creating bla1 over and over again. Instantiation of a class takes some time, and could definitely skew your results. Using "with" doesn't really change what needs to be looked up, it just changes the scope of the code. Calling a function does have overhead, but I wouldn't expect much difference between calling a function in "this" and calling a function on any other instance.

    But that's all beside the point. We were talking about design, not optimization. A jet engine strapped to a bicycle might go really really fast, but I'd rather have one attached to a jet.

  18. #18
    Senior Member
    Join Date
    Jul 2008
    Posts
    418
    ahh i think i get it.
    delegation is the same as creating a var of the datatype MyAbstrClass, but instead with an interface (which in this case is practically the same, right?).
    is this true?
    @5tonsofFlax. i know you're talking about design, i was too, but the problem i had about "with" was just a side question, that i stumbled upon when thinking about this.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center