10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

interface. You’ll need an MP3 file named bongo.mp3 (use any handy MP3 file) saved<br />

in the same folder as the MakeSound.as file.<br />

Example 1-20. MakeSound.as<br />

package<br />

{<br />

import flash.display.Sprite;<br />

public class MakeSound extends Sprite<br />

{<br />

private var guitar:BandFace;<br />

private var bongo:BandFace;<br />

}<br />

}<br />

public function MakeSound( ):void<br />

{<br />

guitar=new Guitar( );<br />

guitar.playInstrument("Gibson");<br />

}<br />

bongo=new Bongo( );<br />

bongo.playInstrument("bongo.mp3");<br />

Note that both instances, guitar and bongo, are typed to the supertype, BandFace, and<br />

not to either the Guitar or the Bongo classes. This practice follows the first principle<br />

of reusable object-oriented design: Program to an interface, not an implementation.<br />

The purpose of doing so is to maintain flexibility and reusability. This principle will<br />

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

that fact.<br />

A word about the interface and abstract class naming conventions<br />

used in this book: with the focal point of this book on object-oriented<br />

programming and design patterns, we use naming conventions that<br />

best reflect and clarify the structure of the different design patterns. As<br />

a result, we don’t always follow some of the naming conventions. One<br />

convention is to name interfaces beginning with a capital I. So following<br />

that convention, the BandFace interface would have been named<br />

IBandFace. Where using the I does not interfere with clarifying a<br />

design pattern structure, we use it, but where we have several interfaces<br />

in a design pattern, often we forego that convention. Another<br />

convention is to name abstract classes using Abstract+ Something. So,<br />

AbstractHorses would be a name for a class you’d otherwise name<br />

Horses. Again, our focus on revealing structure supersedes using these<br />

conventions. We differentiate abstract from concrete classes using<br />

<strong>com</strong>ments in the code. Throughout the book, however, we attempt to<br />

be as clear as possible in naming the different classes. You may want<br />

to adopt some of these more <strong>com</strong>mon conventions, once you better<br />

understand them, to aid in keeping your code clear.<br />

30 | Chapter 1: Object-Oriented Programming, <strong>Design</strong> <strong>Patterns</strong>, and <strong>ActionScript</strong> <strong>3.0</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!