Introduction

Flash is a lightweight cross-platform runtime for rich media, enterprise applications and mobile applications, as well as an integrated development environment. Flash can be programmed in ActionScript 1/2/3.

Friday, April 16th, 2004 at 2:24 pm

Interfaces

I was shocked when I read in a forum on Flash from a developer I really admire that he has never used interfaces as type definitions. He even asked if that really works. I just thought: “Huh! What’s going on?” Thus here a few thinks I learned about interfaces:

The Key Sentence:
Program to interfaces rather concrete implementations.

That’s the sentence you should always keep at the back of your mind while programming. Following this golden rule offers the following advantages:

1. Loose coupling between objects which promotes flexibility. You are not bound to concrete implementations. Thus you can change them easily when you have to without affecting calling code.
2. You can write simple stub implementations to ease the testing of classes. It also enables teams to work in parallel after they have agreed on interfaces.

Interfaces in Flash are sadly not that mature yet. There are a few problems related to working with them in practice. Martin and me came across them during the early days of the as2lib. He has summed them up in two posts on the as2lib blog: Interface Mistakes by Macromedia and :Object is Evil. But even with this limitations you should nevertheless work with interfaces because the advantages much outweigh the problems of them in Flash.
Read on to see interfaces in action.

Example of how interfaces could be used:
Imagine the engine of a car. Every car has one and every engine can be started and stopped. Thus you need an interface called “Engine” with the two operations “start()” and “stop()”.

  1. interface Engine {
  2.   public function start(Void):Void;
  3.   public function stop(Void):Void;
  4. }

You also have an interface called “Car” with (for the sake of the example) only one operation: “setEngine()”.

  1. interface Car {
  2.   public function setEngine(newEngine:Engine):Void;
  3. }

Now to become a little more concrete we’ll create three classes: “CarMini”, “FourStrokeEngine” and “EightStrokeEngine”. The class “CarMini” implements the “Car” interface and the two classes “FourStrokeEngine” and “EightStrokeEngine” the interface “Engine”.

  1. class CarMini implements Car {
  2.   private var engine:Engine;
  3.   public function CarMini(newEngine:Engine) {
  4.     setEngine(newEngine);
  5.   }
  6.   public function setEngine(newEngine:Engine):Void {
  7.     engine = newEngine;
  8.   }
  9. }
  10.  
  11. class FourStrokeEngine implements Engine {
  12.   public function FourStrokeEngine(Void) {
  13.   }
  14.   public function start(Void):Void {
  15.     // start the lame four stroke engine
  16.   }
  17.   public function stop(Void):Void {
  18.     // stop the lame four stroke engine
  19.   }
  20. }
  21.  
  22. class EightStrokeEngine implements Engine {
  23.   public function EightStrokeEngine(Void) {
  24.   }
  25.   public function start(Void):Void {
  26.     // start the agile eight stroke engine
  27.   }
  28.   public function stop(Void):Void {
  29.     // stop the agile eight stroke engine
  30.   }
  31. }

You buy a Mini with a four stroke engine (because it is the standard). After a few rides you realize that the engine just is too lame. Thus you decide to put a eight stroke engine in. This isn’t a problem at all because you have a standard (the “Engine” interface) for all engines and you do not rely on concrete implementations. What a good luck for you. Because otherwise you had to buy a new car. :)

  1. // buy the mini
  2. var myMini:Car = new CarMini(new FourStrokeEngine());
  3. // take a few rides
  4. ….
  5. // decide that you need a faster engine
  6. // put the new one in
  7. myMini.setEngine(new EightStrokeEngine());
  8. // take a few rides
  9. // be happy :)