AS3 Enforced Singleton


/ Published in: ActionScript 3
Save to your folder(s)

This implementation of the singleton pattern provides a way to enforce singleton usage. There are numerous Singleton implementations for AS3 (due to the lack of private constructor) this method, is the cleanest I've seen so far.


Copy this code and paste it in your HTML
  1. package mypackage
  2. {
  3. public class SingletonExample
  4. {
  5. public function SingletonExample(enforcer:SingletonEnforcer)
  6. {
  7. }
  8.  
  9. private static var _instance : SingletonExample;
  10.  
  11. public static function getInstance():SingletonExample
  12. {
  13. if (_instance == null)
  14. _instance = new SingletonExample(new SingletonEnforcer());
  15.  
  16. return _instance;
  17. }
  18. }
  19. }
  20. class SingletonEnforcer {}

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.