11.06.2012 17:43
Interfaces in Flash [basic]
In programming, by ‘Interface’ we understand an abstract definition of methods that by themselves don’t do anything, except for forcing a certain syntax in a class that implements them. In other words we define a function and its arguments in one place and then implement its mechanics in another (target class). Of course by no means its the best way to create a function in Flash, but interfaces have a special trait that makes them irreplaceable in certain scenarios. This trait is ability to cast an object to the interface they implement, which in turn allows us to communicate with it, without actually know what the object is.
I think it is best to demonstrate it on a example, starting from the very beginning: let’s say we want to create a game that displays some information through pop-ups in such way, that each of them contains an “OK” button:
I think it is best to demonstrate it on a example, starting from the very beginning: let’s say we want to create a game that displays some information through pop-ups in such way, that each of them contains an “OK” button:
public class MyPopup extends MovieClip { private var myButton:OKButton; public function MyPopup() { myButton = new OKButton(this); addChild(myButton); } public function close():void { this.visible = false; } }
public class OKButton extends MovieClip { private var myPopup:MyPopup; public function OKButton(popup:MyPopup) { myPopup = popup; this.addEventListener(MouseEvent.CLICK,onMouse); } private function onMouse(e:MouseEvent):void { myPopup.close(); } }At first everything might like fine, but as soon as we start adding more pop-ups the limitation of the above code become apparent - what if we want to reuse the same button for MyPopup2 ? Is adding another argument ( OKButton(popup:MyPopup, popup2:MyPopup2) {} ) enough? Should we repeat this for each new pop-up we want to add? One solution for sure is to create a base class for every pop-up, so we cast it down to use shared methods. However such root classes might bring some troubles in Flash, that I won’t be discussing here. At this point it is probably quite obvious what I am trying to say - the best solution is to use Interfaces. So, let’s create a new AS file:
public interface IPopup { function close():void }And then modify existing classes:
public class MyPopup extends MovieClip implements IPopup{ private var myButton:OKButton; public function MyPopup() { myButton = new OKButton(this); addChild(myButton); } public function close():void { this.visible = false; } }
public class MyButton extends MovieClip { private var myPopup:IPopup; public function MyButton(popup:IPopup) { myPopup = popup; this.addEventListener(MouseEvent.CLICK,onMouse); } private function onMouse(e:MouseEvent):void { myPopup.close(); } }The difference is quite small, but possibilities definitely much broader. From now on MyButton can communicate with every pop-up without actually knowing what class it is, as long as they implement the proper interface.