{"id":11,"date":"2007-12-17T16:00:09","date_gmt":"2007-12-17T22:00:09","guid":{"rendered":"http:\/\/todesco-technologies.com\/unboxed\/?p=4"},"modified":"2007-12-17T16:00:09","modified_gmt":"2007-12-17T22:00:09","slug":"singletons-implemented-the-right-way","status":"publish","type":"post","link":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/2007\/12\/17\/singletons-implemented-the-right-way\/","title":{"rendered":"Singleton\u2019s implemented the right way"},"content":{"rendered":"<p>The Singleton pattern implementation out of the gang of four book [Gamma, Helm, Johnson, and Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.] is not thread save (at least in C# it isn\u2019t). Besides that it uses a GetInstance() method and makes not use of C# properties. I think a simple Instance property is nicer and follows the general C# style.<\/p>\n<p>Check out my version of a Singleton implemenation:<\/p>\n<pre>public class Singleton \n\n{ \n\n    private readonly static Singleton singleton = new Singleton(); \n\n    public static Singleton Instance() \n\n    { \n\n        return singleton; \n\n    } \n\n    private Singleton() \n\n    { \n\n    } \n\n}<\/pre>\n<p>Upsides of this approach are that it is thread save and look quite simple. Downside is you don\u2019t have exact control over <strong>when<\/strong> the Instance is created. In the few cases where this is important (Let\u2019s say the creation of the instance is very time consuming and you want to guarantee that it is only created when needed you might want to check out the double-check locking approach more detailed explained here: <a target=\"_blank\" href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms998558.aspx\" onclick=\"return top.js.OpenExtLink(window,event,this)\">http:\/\/msdn2.microsoft.com\/en<wbr><\/wbr>-us\/library\/ms998558.aspx<\/a>.<\/p>\n<p>The double-check locking approach is also a solution if you need to do some extra stuff when you create the one instance.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Singleton pattern implementation out of the gang of four book [Gamma, Helm, Johnson, and Vlissides. Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley, 1995.] is not thread save (at least in C# it isn\u2019t). Besides that it uses a GetInstance() method and makes not use of C# properties. I think a simple Instance property &hellip; <a href=\"https:\/\/todescotechnologies.com\/unboxed\/index.php\/2007\/12\/17\/singletons-implemented-the-right-way\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Singleton\u2019s implemented the right way<\/span> <span class=\"meta-nav\">&rarr;<\/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":[1],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":0,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"wp:attachment":[{"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/todescotechnologies.com\/unboxed\/index.php\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}