AlecMcE.com

Coding on the Flash Platform

Abstract Classes in AS3

View Comments

As we started building a new library at work, we were pondering what was the best strategy for abstract classes. Despite arguments to the contrary, we respectfully believe that abstract classes are useful! A quick Google search produces a lot of alternatives. Here are some of them.

All of these examples assume that the class is called AbstractExample and that any necessary imports have been made. I will concentrate just on the constructor method.

getQualifiedClassName Approach @ PixelBreaker

public function AbstractExample()
{
	if(Class(getDefinitionByName(getQualifiedClassName(this))) == AbstractExample)
		throw new Error("AbstractExample must not be directly instantiated");
}

toString Approach @ Tink

public function AbstractExample()
{
	if(toString() == "[object AbstractExmample]")
		throw new Error("AbstractExample must not be directly instantiated");
}

Object Constructor Approach @ various

public function AbstractExample()
{
	if(Object(this).constructor === AbstractExample)
		throw new Error("AbstractExample must not be directly instantiated");
}

Self Reference Approach @ JoshTalksFlash

Note that with this approach (unlike the others) you have work to in the instantiating class, calling super(this); at the top of your constructor.

public function AbstractExample(self:AbstractExample)
{
	if(self != this)
		throw new Error("AbstractExample must not be directly instantiated");
}

A review

The lack of a genuine abstract class is a little annoying. It is interesting to see the different mechanisms by which various coders have worked around the problem. For me though, what I have coined the Object Constructor Approach is the compelling choice. It is quick to write (or template in Eclipse), doesn’t need any imports, and generally looks the most clean. For our new code library, the choice seems straightforward.

Written by alec

July 29th, 2009 at 2:04 pm

Posted in as3, opinion

  • When I first began doing OOP, I used the crap out of abstract classes. Now I rearely use them.

    In your example I would cut 'AbstractExample' and have 'XMLExample', 'BinaryExample' and 'SqlliteExample' implement IExample.

    I'm sure that whatever data each concrete class is working on is unique to it's own implementation and should not be exposed. If your overriding methods in super class, then why bother have the abstract methods?
  • That's a fair enough point, and you can definitely not use abstract classes if you prefer. However, sometimes implementations have some duplicated code, and then I would rather abstract commonalities into a common class, just because it reduces duplication and increases coding speed. When the situation isn't like that, I don't use abstract classes either! What I do dislike though, is having made my design choice to use them, then having to write error throwing methods because as3 doesn't support "abstract". I think it's all about choice.
  • For sure Keith, abstract classes are definitely not necessary, just useful. :)

    We have found the most compelling reason for using them comes while designing the model of a large application. We want to use define an interface, then implement the interface with mock objects, then implement the interface via XML, then maybe through a socket connection to a database, and so on...

    We find that under a package we have several implementations, all using the same data types and exposing the same interface. We end up with maybe:

    interface IExample
    class AbstractExample implements IExample
    class XMLExample extends AbstractExample
    class BinaryExample extends AbstractExample
    class SqlliteExample extends AbstractExample

    A cleaner architecture would be if the 'abstract' AbstractExample class could be really abstract, which we could use instead of the interface.
  • I agree abstract classes would be useful, but question how vital they are. People are obviously building things in ActionScript without them. :)
blog comments powered by Disqus