ActionScript 3.0 Design Patterns

(Chris Devlin) #1

28 | Chapter 1: Object-Oriented Programming, Design Patterns, and ActionScript 3.0


Using Interfaces and Abstract Classes in ActionScript 3.0


Inheritance can also be linked to two other structures;interfacesandabstract classes.


However, the connection between theinterfacestructure (a statement in Action-


Script 3.0) or the abstract class and inheritance is a bit different from the one with


theclass structure we’ve examined thus far.


Interface constructs


First of all, in this context,interfacerefers to an ActionScript 3.0 statement and not


the object interface discussed in the “Encapsulation and Design Patterns” section.


While a class can be said to be an abstraction of an object, aninterfaceis an


abstraction of methods. They are widely used in design patterns. Beginning in


Chapter 5 with the adapter pattern, you will see interfaces at work in several of the


other design patterns.


To begin to see what an interface does, a simple example illustrates the use of one.


However, once you start seeing how they’re used in design patterns, you will better


see their utility. Example 1-17 shows how a typical interface is created. The applica-


tion is made up of Example 1-17 to Example 1-20.


The first thing we’ll do is to make our interface. As you can see in Example 1-17, the


single function is quite simple and devoid of content. It does have a name, parame-


ter, and return type, but note that the function is only a single line.


Each implementation of the interface must have exactly the same structure in all of


the methods in the interface, and if anything is not the same, you’ll get an error mes-


sage. As long as the signature for the methods is the same, everything should work


fine. Example 1-18 is the first implementation of the of theBandFace interface.


Example 1-17. BandFace.as


package
{
//Interface
public interface BandFace
{
function playInstrument(strum:String):void;
}
}


Example 1-18. Guitar.as


package
{
public class Guitar implements BandFace
{
public function Guitar( ) {}

Free download pdf