{"id":85,"date":"2006-09-16T10:17:54","date_gmt":"2006-09-16T18:17:54","guid":{"rendered":"http:\/\/www.ericfeminella.com\/blog\/2006\/09\/16\/as3-true-singleton-implementation\/"},"modified":"2012-03-04T23:30:22","modified_gmt":"2012-03-05T04:30:22","slug":"as3-true-singleton-implementation","status":"publish","type":"post","link":"https:\/\/www.ericfeminella.com\/blog\/2006\/09\/16\/as3-true-singleton-implementation\/","title":{"rendered":"AS3 Singleton Pattern Implementation"},"content":{"rendered":"<p>Many times when developing you will find that your application calls for a single instance of a class to manage a specific service across the system. For example, let&#8217;s say that your application needs to have one single instance of a class that manages all events within the system. Or another scenario could be where the application calls for a single instance of a class to manage all WSDL calls across the system. In order to support the required functionality for the application you would need to create a class that implements what is known as the Singleton design pattern.<\/p>\n<p>The Singleton pattern is implemented by creating a class that has a static public method which instantiates a new instance of the object if one does not exist. If an instance already exists, it simply returns a reference to that object which is a static property of the class, typically named &#8216;instance&#8217;. A best practice naming convention for this method is &#8216;getInstance();&#8217;. To make sure that the object cannot be instantiated in any other way, the constructor is made either private or protected. A Singleton is also considered to be an anti-pattern to some extent as it can be used as a euphemism for a global variable. However, in Object Oriented Programming it is an extreamly useful tool when used correctly.<\/p>\n<p>As I mentioned earlier, the constructor for a Singleton is made either private or protected to restrict it&#8217;s access from being instantiated. Since constructors can only be declared as public in ActionScript 3 this is not allowed. A common solution to this is to simply add a inner class within the same file as the class definition and pass an instance of the private class as an argument to the constructor. This way only the getInstance(); method has access to the private class. The only problem with this approach is that the constructor can be called from anywhere within the application with a null value passed in as the argument to the constructor. My solution to this is simple, check that the parameter passed to the constructor is not null. This is how a true Singleton implementation is achieved. The only time that this will not work is if the singleton class is a sub class and the constructor needs to make a call to super. The reason that this will not work is that a call to a super classes constructor must be executed in the very first line of code, therefore a parameter value can not be validated.<\/p>\n<p>Below I have added a simple example demonstrating how to implement the Singleton pattern in ActionScript 3.0:<\/p>\n<pre lang=\"actionscript\">\r\n\r\npackage\r\n{\r\n    public final class Singleton\r\n    {\r\n        \/**\r\n         * Defines the unique instance of the class\r\n         *\/\r\n        private static var instance:Singleton;\r\n\t\t\r\n        \/**\r\n         * \r\n         * Singleton constructor which is never to be directly \r\n         * instantiated \r\n         * \r\n         * @param Inner class which restricts constructor \r\n         * access to private\r\n         * \r\n         *\/\r\n        public function Singleton(access:Private)\r\n        {\r\n            if (access == null)\r\n            {\r\n                throw new Error(\"Singleton Exception...\");\r\n            }\r\n            instance = this;\r\n        }\r\n        \r\n        \/**\r\n         * \r\n         * All calls to a Singleton are to be made via \r\n         * Singleton.getInstance(); in order to retrieve \r\n         * the singleton instance of the Singleton\r\n         * \r\n         * @return the Singleton of the class\r\n         * \r\n         *\/        \r\n        public static function getInstance() : Singleton\r\n        {\r\n            if (instance == null) \r\n            {\r\n                instance = new Singleton( new Private() );\r\n            }\r\n            return instance;\r\n        }\r\n    }\r\n}\r\n\r\n\/**\r\n * Inner class which restricts constructor access to private\r\n *\/\r\nfinal class Private {}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Many times when developing you will find that your application calls for a single instance of a class to manage a specific service across the system. For example, let&#8217;s say that your application needs to have one single instance of a class that manages all events within the system. Or another scenario could be where the application calls for a&#8230; <a class=\"read-more\" href=\"https:\/\/www.ericfeminella.com\/blog\/2006\/09\/16\/as3-true-singleton-implementation\/\">Continue Reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[23,31],"tags":[],"class_list":["post-85","post","type-post","status-publish","format-standard","hentry","category-design-patterns","category-oop"],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/85","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/comments?post=85"}],"version-history":[{"count":0,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/posts\/85\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/media?parent=85"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/categories?post=85"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ericfeminella.com\/blog\/wp-json\/wp\/v2\/tags?post=85"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}