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.

However, does that mean that only a single reference can be associated with the single<br />

instance? What happens, for example, if I first create one instance with one label,<br />

and then attempt to create another instance with a different label?<br />

To see that a single instance can be associated with more than one reference (name<br />

or label), some changes have to be made to the primary Singleton class. Using a separate<br />

folder from the Singleton class, in Example 3-4 open a new <strong>ActionScript</strong> file and<br />

add the script in Example 3-8. Save it as Singleton.as in the newly created folder.<br />

Example 3-8. Singleton.as<br />

package<br />

{<br />

}<br />

public class Singleton<br />

{<br />

private var _msg:String;<br />

private static var _instance:Singleton;<br />

public function Singleton(pvt:PrivateClass)<br />

{<br />

}<br />

public static function getInstance( ):Singleton<br />

{<br />

}<br />

}<br />

if(Singleton._instance == null)<br />

{<br />

Singleton._instance=new Singleton(new PrivateClass( ));<br />

trace("Singleton instantiated");<br />

}<br />

return Singleton._instance;<br />

//Get and set methods<br />

public function getMsg( ):String<br />

{<br />

return _msg;<br />

}<br />

public function setMsg(alert:String):void<br />

{<br />

_msg = alert;<br />

}<br />

class PrivateClass<br />

{<br />

public function PrivateClass( ) {<br />

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

}<br />

}<br />

110 | Chapter 3: Singleton Pattern

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

Saved successfully!

Ooh no, something went wrong!