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.

To create a class, we need a public constructor function within a package. Because<br />

we cannot create a private class within a package, we’ll have to create one outside a<br />

package, and then somehow get it to work inside a package. So, using a single .as<br />

file, the following script first creates a public class, closes the package, and then creates<br />

a private class outside the package. The private class is then called from within<br />

the public class. Example 3-2 shows how this is done.<br />

Example 3-2. PublicClass.as<br />

1 package<br />

2 {<br />

3 import flash.display.Sprite;<br />

4 public class PublicClass extends Sprite<br />

5 {<br />

6 public function PublicClass( )<br />

7 {<br />

8 PrivateClass.alert( );<br />

9 }<br />

10 }<br />

11 }<br />

12 //End of Package<br />

13 //**************<br />

14 //Private class<br />

15 class PrivateClass<br />

16 {<br />

17 public function PrivateClass( )<br />

18 {<br />

19<br />

20 public static function alert( ):void<br />

21 {<br />

22 trace("This is from a private class");<br />

23 }<br />

24 }<br />

Save the file as PublicClass.as, and, in an FLA file, type in PublicClass as the Document<br />

class, and save the FLA file as TestPrivate.fla. When you test the program, you<br />

will see:<br />

This is from a private class<br />

in the Output panel. Notice in Line 8 that the class, PrivateClass, is employed simply<br />

by typing the class name and the method. No constructor statement is used at all.<br />

Creating a Private Class Instance<br />

Keeping in mind the implementation in Example 3-1, we have to work out a way for<br />

the class constructor to act like a private class. If the private class can be part of the<br />

instantiation method (getInstance( )), then we’ll be able to have the same effect as a<br />

private class constructor.<br />

104 | Chapter 3: Singleton Pattern

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

Saved successfully!

Ooh no, something went wrong!