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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 3-12. Tuner.as<br />

}<br />

}<br />

}<br />

//Stop Play<br />

public function stopMe ( ):void<br />

{<br />

if (_goChannel != null)<br />

{<br />

_goChannel.stop ( );<br />

}<br />

}<br />

class PrivateClass<br />

{<br />

public function PrivateClass ( )<br />

{<br />

trace ("PrivateClass called");<br />

}<br />

}<br />

The first thing to note about this Singleton script is that it imports packages and<br />

classes. Up to this point, all of the imports were done in the scripts that called the<br />

Singleton class. However, whenever a method or property has a reference, including<br />

abstract ones, to a class in a package, it needs to import that class. Likewise, you’ll<br />

see that the class (DoMusic) that implements the Singleton class (Tuner) requires no<br />

further import of the objects associated with the packages and classes already<br />

imported in the Singleton class.<br />

Because this application simply makes sure that only one MP3 file is played at any<br />

time, the Singleton implementation is very simple. Example 3-13 shows the code for<br />

this class:<br />

Example 3-13. DoMusic.as<br />

package<br />

{<br />

import flash.display.Sprite;<br />

public class DoMusic extends Sprite {<br />

public function DoMusic( ) {<br />

var playOne:Tuner = Tuner.getInstance( );<br />

//Use any MP3 file you have available<br />

playOne.playMe("blues.mp3");<br />

}<br />

}<br />

}<br />

Save Example 3-13 as DoMusic.as in the same folder as Tuner.as. Open a Flash document<br />

file, type DoMusic in the Class document window in the Properties panel, and<br />

save it as Music.fla in the folder with the two <strong>ActionScript</strong> files. Finally, place any<br />

When to Use the Singleton Pattern | 119

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

Saved successfully!

Ooh no, something went wrong!