dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: [F8] Singleton in AS2

  1. #1
    Retired SCORM Guru PAlexC's Avatar
    Join Date
    Nov 2000
    Location
    NJ
    Posts
    1,387

    [F8] Singleton in AS2

    I've skimmed Colin Moock's chapter on it and googled some tutorials, but I have a question about implementing a Singleton class in AS2. I'm building a (new) nav/tracking frame for courseware, and only want one instance of the shell class I'm writing for it.

    Most tutorials I've seen have a static method called getInstance, and calls to your implemented Singleton class go like this:

    mySingleton.getInstance().doMethod();

    Wouldn't it be easier ot make doMethod a static function?
    mySingleton.doMethod();

    Also, if I want to assign my lone class to a _global var, does that present any problems? And how do I do it?

    import com.MyClass;
    _global.shell = MyClass;

    Or is there a more direct way?
    "What really bugs me is that my mom had the audacity to call Flash Kit a bunch of 'inept jack-asses'." - sk8Krog
    ...and now I have tape all over my face.

  2. #2
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Well, whether you want to make doMethod() a staic method depends on your design...

    Generally, you use a Singleton when you want to access a class from multiple places, but you want to make sure that all those references point to a single instance of the class - say, for example, a Config class that loads and parses an XML file, and can then be called by other classes to get data they need to initialize themselves...

    On the other hand, static class methods are called on the class itself, and do not have any instance at all. Static methods are better for one time function calls, that do not rely on retaining data between calls - like for example, the Math class, where you simply pass in two numbers and get back a result. So, in my previous example, a static method would not work for the Config class, as there is no instance to store the parsed XML data in...

    If you decide to go for the Singleton approach, there isn't really any need to also make it a global var, as any object that references mySingleton.getInstance() will return the same instance of the Singleton object...

    HTH,

    K.

  3. #3
    Retired SCORM Guru PAlexC's Avatar
    Join Date
    Nov 2000
    Location
    NJ
    Posts
    1,387
    Thanks, sorta helps.

    Does that mean if I wanted to reference it by the class name I'd have to import the class into every file that will make a call against it?

    I'm delcaring it as a global for 2 reasons:
    1.) Content that needs to call methods on it is going to be loaded into multiple levels.
    2.) Other people are going to be building content, telling them to reference "shell.doMethod()" is an easier habit to get them into than "CourseShell.getInstance().doMethod()". They're timeline developers, very little scripting capability.

    So you're saying there's no real reason to make it a singleton if I'm going to instansiate it as a global?
    "What really bugs me is that my mom had the audacity to call Flash Kit a bunch of 'inept jack-asses'." - sk8Krog
    ...and now I have tape all over my face.

  4. #4
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Well, you can always do this:
    code:

    _global.shell=CouseShell.getInstance();


    But if everything is going to access it through the global var anyway, it is kind of redundant / unnecessary to make it a Singleton...

    K.

  5. #5
    Retired SCORM Guru PAlexC's Avatar
    Join Date
    Nov 2000
    Location
    NJ
    Posts
    1,387
    That's kinda what I thought. Thanks.
    "What really bugs me is that my mom had the audacity to call Flash Kit a bunch of 'inept jack-asses'." - sk8Krog
    ...and now I have tape all over my face.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center