{"id":11744,"date":"2014-02-14T13:53:59","date_gmt":"2014-02-14T08:23:59","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11744"},"modified":"2016-12-19T15:31:06","modified_gmt":"2016-12-19T10:01:06","slug":"singleton-pattern-with-javascript","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/singleton-pattern-with-javascript\/","title":{"rendered":"Singleton Pattern with JavaScript"},"content":{"rendered":"<p>Many a times, we face a requirement where we need only one object of a class to communicate across the application. <strong>Singleton<\/strong> <strong>Pattern<\/strong> comes into role in this scenario. <strong>Singleton<\/strong> is one of my favorite <strong>Design<\/strong> <strong>Patterns<\/strong>. <strong>Singleton<\/strong> <strong>pattern<\/strong> restricts us to initialize more than one object. We implement <strong>Singleton<\/strong> by creating a class, having a method named as <strong>getInstance()<\/strong>, which returns reference to object of that class. If object has already been initiated, then it will return reference to that object, otherwise will initiate a new instance and return reference to that newly initiated object. In <strong>JavaScript<\/strong>, we can easily implement <strong>Singleton<\/strong> <strong>Pattern<\/strong>.<\/p>\n<p>[js]<br \/>\n\/*<br \/>\n * ApplicationContext is Singleton class.<br \/>\n *\/<br \/>\nvar ApplicationContext = (function () {<br \/>\n    var instance; \/\/ instance variable to hold the reference of Object.<br \/>\n    function initialize() {<br \/>\n        \/\/ private members here<br \/>\n        var _this = this;<br \/>\n        var secretKey = +new Date() + &quot;&quot; + parseInt(Math.random()*1000, 10);<br \/>\n        return {<br \/>\n            \/\/ public members.<br \/>\n            getSecretKey: function () {<br \/>\n                return port;<br \/>\n            }<br \/>\n        };<br \/>\n    }<br \/>\n    return {<br \/>\n        \/*<br \/>\n         * If instance variable is already having reference to the ApplicationContext, then return it,<br \/>\n         * Otherwise initialize the instance variable first and then return.<br \/>\n         *\/<br \/>\n        getInstance: function () {<br \/>\n            if (!instance) {<br \/>\n                instance = initialize();<br \/>\n            }<br \/>\n            return instance;<br \/>\n        }<br \/>\n    };<br \/>\n})();<br \/>\nvar app1 = ApplicationContext.getInstance();<br \/>\nvar app2 = ApplicationContext.getInstance();<br \/>\nconsole.log(app1.getSecretKey() == app2.getSecretKey()); \/\/ true<br \/>\n[\/js]<\/p>\n<p><strong>app1<\/strong> and <strong>app2<\/strong> both are having same object, so <strong>app1.getSecretKey() == app2.getSecretKey()<\/strong> is logging true.<\/p>\n<p>Amit Kumar<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit.kumar\/\">amit.kumar@intelligrape.com<\/a><br \/>\nin.linkedin.com\/in\/amitkumar0110<br \/>\ntwitter.com\/amit_kumar0110<br \/>\n<strong><a href=\"http:\/\/www.tothenew.com\/blog\/author\/amit-kumar\/\">More Blogs by Me<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Many a times, we face a requirement where we need only one object of a class to communicate across the application. Singleton Pattern comes into role in this scenario. Singleton is one of my favorite Design Patterns. Singleton pattern restricts us to initialize more than one object. We implement Singleton by creating a class, having [&hellip;]<\/p>\n","protected":false},"author":52,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1185],"tags":[55,1308,1177,1309],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11744"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/52"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11744"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11744\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11744"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11744"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11744"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}