ActionScript 3.0 Design Patterns

(Chris Devlin) #1

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


interface. You’ll need an MP3 file namedbongo.mp3(use any handy MP3 file) saved


in the same folder as theMakeSound.as file.


Note that both instances,guitarandbongo, are typed to the supertype,BandFace, and


not to either theGuitaror theBongoclasses. This practice follows the first principle


of reusable object-oriented design:Program to an interface, not an implementation.


The purpose of doing so is to maintain flexibility and reusability. This principle will


be fully explored elsewhere in this chapter and in Chapter 8, but for now, just note


that fact.


A word about the interface and abstract class naming conventions
used in this book: with the focal point of this book on object-oriented
programming and design patterns, we use naming conventions that
best reflect and clarify the structure of the different design patterns. As
a result, we don’t always follow some of the naming conventions. One
convention is to name interfaces beginning with a capitalI. So follow-
ing that convention, theBandFaceinterface would have been named
IBandFace. Where using theIdoes not interfere with clarifying a
design pattern structure, we use it, but where we have several inter-
faces in a design pattern, often we forego that convention. Another
convention is to name abstract classes usingAbstract+Something. So,
AbstractHorseswould be a name for a class you’d otherwise name
Horses. Again, our focus on revealing structure supersedes using these
conventions. We differentiate abstract from concrete classes using
comments in the code. Throughout the book, however, we attempt to
be as clear as possible in naming the different classes. You may want
to adopt some of these more common conventions, once you better
understand them, to aid in keeping your code clear.

Example 1-20. MakeSound.as


package
{
import flash.display.Sprite;
public class MakeSound extends Sprite
{
private var guitar:BandFace;
private var bongo:BandFace;


public function MakeSound( ):void
{
guitar=new Guitar( );
guitar.playInstrument("Gibson");


bongo=new Bongo( );
bongo.playInstrument("bongo.mp3");
}
}
}

Free download pdf