{"id":285,"date":"2014-02-07T18:18:50","date_gmt":"2014-02-07T22:18:50","guid":{"rendered":"http:\/\/www.joebillman.com\/blog\/?p=285"},"modified":"2014-02-07T18:18:50","modified_gmt":"2014-02-07T22:18:50","slug":"as3-singleton","status":"publish","type":"post","link":"https:\/\/www.joebillman.com\/blog\/?p=285","title":{"rendered":"AS3 Singleton"},"content":{"rendered":"<p>Singleton classes are very useful. So I wanted to show a simple sample of a Singleton class. I like to use these as models to store data in one location that multiple parts of your application can access. Singleton classes in ActionScript 3 for the most part are just like they are in other OOP languages. There is one minor difference. Since you can&#8217;t make a constructor private you have to add a few lines of code in the constructor. Here is a sample:<\/p>\n<pre style=\"background-color: #e5e5e5;\">public class Singleton\n{\n\n\tprivate static var _instance:Singleton = new Singleton();\n\n\tpublic function Singleton()\n\t{\n\t\tif(_instance != null)\n\t\t{\n\t\t\tthrow new Error(\"Instantiation failed\");\n\t\t}\n\t}\n\n\tpublic static function getInstance():Singleton\n\t{\n\t\treturn _instance;\n\t}\n\n}<\/pre>\n<p>That is my preferred way to do it for models that you know are going to be instantiated. Then you just add public variables and you have a model. Generally when I&#8217;m using models I always plan to instantiate them, but if you are not sure if you are going to instantiate the class you may want to do it like this:<\/p>\n<pre style=\"background-color: #e5e5e5;\">public class Singleton\n{\n\n\tprivate static var _instance:Singleton;\n\n\tpublic function Singleton()\n\t{\n\t\tif(_instance != null)\n\t\t{\n\t\t\tthrow new Error(\"Instantiation failed\");\n\t\t}\n\t}\n\n\tpublic static function getInstance():Singleton\n\t{\n\t\tif(_instance == null)\n\t\t{\n\t\t\t_instance = new Singleton();\n\t\t}\n\t\treturn _instance;\n\t}\n\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Singleton classes are very useful. So I wanted to show a simple sample of a Singleton class. I like to use these as models to store data in one location that multiple parts of your application can access. Singleton classes in ActionScript 3 for the most part are just like they are in other OOP &hellip; <a href=\"https:\/\/www.joebillman.com\/blog\/?p=285\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;AS3 Singleton&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11],"tags":[49,102],"class_list":["post-285","post","type-post","status-publish","format-standard","hentry","category-actionscript","category-flex","tag-as3","tag-singleton"],"_links":{"self":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=285"}],"version-history":[{"count":0,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/285\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}