Programming to an interface with the Event class

One of the principles of Object Oriented Programming and Design Pattern Architecture is the idea of programming to interfaces rather than concrete implementations. The word interface here does not necessarily imply the interface programming structure rather, the idea of programming to a super class or to an interface. The idea is simple - if class A is the super class of class B, class C and class D, then a variable a:A can contain A, B, C or D.

var a:A = new A;
a = new B();
a = new C();
a = new D();

This is actually in its very basic form, the main scaffolding of a Finite State Engine.

Recently I had to deal with creating accessible structures for an application. I was faced with the option of creating separate listener methods for MouseEvent.ROLL_OVER, and FocusEvent.FOCUS_IN in spite of the fact that they did the same thing. The solution was to 'Program to an interface'. In this case, the Event class is the interface or superclass of all the events. Therefore rather than have the listener method parameter datatyped to the concrete type, it was datatyped to the super class. The result is that you can set all the subscriber objects to listen to the same listener method.

a.addEventListener(FocusEvent.FOCUS_IN, interfaceHandler);
a.addEventListener(MouseEvent.ROLL_OVER, interfaceHandler);

public function interfaceHandler(e:Event):void{

aYo Binitie II

1 comment:

Mad Jack said...

Thanks for the post. I was looking for a comprehensive definition of the phrase programming to an interface and you provided me with a nice, succinct definition and example.