ActionScript 3.0 Design Patterns

(Chris Devlin) #1
When to Use the Singleton Pattern | 121

To test this particular implementation, you will need a slightly more elaborate Flash


document file. The following steps show what you need to do.



  1. Open a new Flash document and save it in the same folder as yourTuner.as file.

  2. Using the Oval tool, draw an oval with a radius of 15 on the stage. Select the
    oval, and then press the F8 button to open the Convert to Symbol dialog box.
    Type inSongPlayin the Name window, and select Button for the Type. Click the
    Export for ActionScript checkbox in the Linkage group. The class name should
    showSongPlayand the base class will showflash.display.SimpleButton. Click
    OK. Delete the button from the stage. (It still remains in the Library.)

  3. Open the Properties panel, and, in the Document class window, type in
    DoMusicBtn. Save the file once more.

  4. In the folder where you’ve saved the ActionScript (.as) and Flash document files
    (.fla), add two MP3 files using the file names shown in Example 3-14. (You can
    change the names in your MP3 files or the names in Example 3-14.)

  5. Test the application by pressing Ctrl + Enter (Command + Return on the Mac).


You should see three buttons appear. The two buttons on the left let you select one


of the two MP3 files, and the one on the right will stop whatever is currently play-


ing. If you click either of the buttons on the left while a tune is still playing, it’ll stop


the tune and start play from the beginning of the selected tune.


Using Multiple References in a Shopping Cart


This final example of a Singleton design pattern shows how a Singleton instance can


have multiple references and still maintain accuracy. Like the previous examples, this


one’s also simple, with the focus on both the role of the Singleton design and some


insights into the Singleton structure


The online Shopping Cart is really nothing more than a conceptual image of a digital


accumulator. As each new item is added to the cart, the most current entry is accu-


mulated, and at the time of checkout, this total is computed with applicable ta xand


playTune.playMe ("blues.mp3");
}
function doTwo (e:MouseEvent):void
{
playTune.playMe ("class1.mp3");
}
function doStop (e:MouseEvent):void
{
playTune.stopMe ( );
}
}
}


Example 3-14. DoMusicBtn.as

Free download pdf