{"id":30114,"date":"2022-11-25T18:21:37","date_gmt":"2022-11-25T12:51:37","guid":{"rendered":"https:\/\/blazeclan.com\/singleton-approach-coding-pro-right-design-patterns\/"},"modified":"2023-03-11T21:55:41","modified_gmt":"2023-03-11T16:25:41","slug":"singleton-approach-coding-pro-right-design-patterns","status":"publish","type":"post","link":"https:\/\/blazeclan.com\/india\/blog\/singleton-approach-coding-pro-right-design-patterns\/","title":{"rendered":"The Singleton Approach &#8211; Coding like a Pro with the Right Design Patterns"},"content":{"rendered":"<p>Code like a Pro | Courtesy: blogs.warwick.ac.uk<\/p>\n<p>We all know how important design patterns are, right? Well Just to bring you to perspective. Imagine, you compiled a code of say 10,000 lines without any definite structure. How difficult would it be for you to modify &amp; restructure the code if required. Even if someone else wants to work on your code if would be a challenge without a definite Blue Print.<\/p>\n<p>Design Patterns are nothing but this blue print &amp; are a popular topic in software development arena. A design pattern is a common, well-described solution to a common software problem. So the sensible use of the right design patterns can result in not only increased code maintainability, design patterns can also be recognized by other developers, thus reducing the learning curve when dealing with a particular piece of code.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>So what are these Design Patterns? Let\u2019s Jump right in !!<\/h3>\n<p><i>A&nbsp;<b>design pattern<\/b>&nbsp;describes the classes and interacting objects used to solve a general design problem in a specific context.<span id=\"more-1879\"><\/span><\/i><\/p>\n<p>There are many design patterns available, some of the important ones that we will be discussing are:<\/p>\n<p>1.Singleton Pattern<br \/>\n2.Decorator Pattern<br \/>\n3.Factory Pattern<br \/>\n4.Abstract&nbsp;Factory Pattern<\/p>\n<p>So let us start with the Singleton Pattern &amp; cover the common know hows on how to implement it.<\/p>\n<p><b><i>Definition:<\/i><\/b><i>&nbsp;The Singleton pattern ensures that a class has only one instance and provide a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element.<\/i><\/p>\n<p>An Example would be the office of the President of India, it\u2019s is a Singleton. The Indian Constitution specifies the means by which a president is elected, limit the term of office and defines the order of succession. As a result, there can be at most one active President at any given time. Regardless of the personal identity of the active President, the title \u201cThe President of India\u201d is a global point of access that identifies the person in the office.<\/p>\n<p>&nbsp;<\/p>\n<h3>Why do we use these Singleton Patterns?<\/h3>\n<ul>\n<li>In Singleton, it takes care that a class only has one instance and provides the global access for it. It encapsulated on the first use.<\/li>\n<li>It&nbsp;speeds up the development process by providing a tested, proven development paradigm. It &nbsp;also improves code readability for coders and architects who are familiar with these patterns.<\/li>\n<li>Singleton Design patterns provide general solutions, documented in a format that does not require specifics tied to the particular problem.<\/li>\n<\/ul>\n<p>Here are some real situations where the singleton is used:<\/p>\n<ol>\n<li><b>Logger Classes:<\/b>&nbsp;&nbsp;Singleton Design patterns help in designing&nbsp;logger classes. In this class, it creates the global access point to use logger operations.<\/li>\n<li><b>Configuration Classes:<\/b>&nbsp;Singleton patterns are useful in creation of configuration object and can be keep it in cache. This avoids the reloading of data and also avoids the open and close operations with the system.<\/li>\n<li><b>Accessing shared resources:<\/b>&nbsp;In multi-threading application Singleton plays an important role. Many threads are accessed by the same method and the result is updated; in this case Singleton can perform better holding of the global access point.<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h3>How to use a Singleton J2EE Environment for the Creations of Heavy Database Instances?<\/h3>\n<p>How to Construct a Singleton<\/p>\n<p>Nowadays Java web&nbsp;applications&nbsp;use <a title=\"Learn more about ORM\" href=\"https:\/\/en.wikipedia.org\/wiki\/Object-relational_mapping\" target=\"_blank\" rel=\"noopener noreferrer\">ORM (Object Relational Mapping)<\/a> &nbsp;for database like Hibernate. In Hibernate as you know, SessionFactory is a long live multi threaded&nbsp;object.&nbsp;Usually one session factory should be created for one database. If you assume the scenario that you are using one database called mysql in your application then the following is a way to create the SessionFactory object, the java code&nbsp;snippets are:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"prettyprint prettyprinted\" style=\"\"><span class=\"kwd\">public<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">class<\/span><span class=\"pln\"> <\/span><span class=\"typ\">HibernateUtil<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\">\n        <\/span><span class=\"kwd\">private<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">static<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">final<\/span><span class=\"pln\"> <\/span><span class=\"typ\">SessionFactory<\/span><span class=\"pln\"> sessionFactory<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\n        <\/span><span class=\"kwd\">static<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\">\n                <\/span><span class=\"kwd\">try<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\">\n                        sessionFactory <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">new<\/span><span class=\"pln\"> <\/span><span class=\"typ\">Configuration<\/span><span class=\"pun\">().<\/span><span class=\"pln\">configure<\/span><span class=\"pun\">()<\/span><span class=\"pln\">\n                                        <\/span><span class=\"pun\">.<\/span><span class=\"pln\">buildSessionFactory<\/span><span class=\"pun\">();<\/span><span class=\"pln\">\n                <\/span><span class=\"pun\">}<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">catch<\/span><span class=\"pln\"> <\/span><span class=\"pun\">(<\/span><span class=\"typ\">Throwable<\/span><span class=\"pln\"> ex<\/span><span class=\"pun\">)<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\">\n                        <\/span><span class=\"typ\">System<\/span><span class=\"pun\">.<\/span><span class=\"pln\">err<\/span><span class=\"pun\">.<\/span><span class=\"pln\">println<\/span><span class=\"pun\">(<\/span><span class=\"str\">\"Initial SessionFactory creation failed.\"<\/span><span class=\"pln\"> <\/span><span class=\"pun\">+<\/span><span class=\"pln\"> ex<\/span><span class=\"pun\">);<\/span><span class=\"pln\">\n                        <\/span><span class=\"kwd\">throw<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">new<\/span><span class=\"pln\"> <\/span><span class=\"typ\">ExceptionInInitializerError<\/span><span class=\"pun\">(<\/span><span class=\"pln\">ex<\/span><span class=\"pun\">);<\/span><span class=\"pln\">\n                <\/span><span class=\"pun\">}<\/span><span class=\"pln\">\n        <\/span><span class=\"pun\">}<\/span><span class=\"pln\">\n\n        <\/span><span class=\"kwd\">public<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">static<\/span><span class=\"pln\"> <\/span><span class=\"typ\">SessionFactory<\/span><span class=\"pln\"> getSessionFactory<\/span><span class=\"pun\">()<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\">\n                <\/span><span class=\"kwd\">return<\/span><span class=\"pln\"> sessionFactory<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\n        <\/span><span class=\"pun\">}<\/span><span class=\"pln\">\n<\/span><span class=\"pun\">}<\/span><\/pre>\n<p><span style=\"font-size: 0.75rem; line-height: 1.25rem;\">When we are use more than one database in our application then we use the HibernateUtil class which is implemented based on singleton design pattern, which insures that one and only one SessionFactory object will be created for the entire application.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3>How would we use Singleton in a Mobile Application Development?<\/h3>\n<p>Usually in mobile, many applications run concurrently in the screen stack or in the background. Mobile operating systems do not support large databases like Oracle, Mysql etc. instead it supports light weight databases e.g.,<a title=\"What is SQLite ?\" href=\"https:\/\/en.wikipedia.org\/wiki\/SQLite\" target=\"_blank\" rel=\"noopener noreferrer\"> SQLite<\/a>. The reason is the issue of memory. Suppose one application is running on screen which stores the user data in the database(SQLite) and there are four applications that are running in the background which updates some data when a particular event occurs. &nbsp;In this case all applications can be woken up on the same time and this will create many instances of the database which is expensive and results may be OutOfMemory or StackOverFlow! If you use a Singleton multithreaded database instance however, you can run apps smoothly. Following is an Android code snippet which uses Singleton:<\/p>\n<pre class=\"prettyprint prettyprinted\" style=\"\"><button class=\"clipboard\"><img decoding=\"async\" src=\"\/wp-content\/plugins\/cc-syntax-highlight\/clipboard\/clipboard.svg\"><\/button><code class=\"prettyprint\"><span class=\"kwd\">public<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">class<\/span><span class=\"pln\"> <\/span><span class=\"typ\">DatabaseHelper<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">extends<\/span><span class=\"pln\"> <\/span><span class=\"typ\">SQLiteOpenHelper<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\"> \n\n  <\/span><span class=\"kwd\">private<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">static<\/span><span class=\"pln\"> <\/span><span class=\"typ\">DatabaseHelper<\/span><span class=\"pln\"> sInstance<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\n\n  <\/span><span class=\"kwd\">private<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">static<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">final<\/span><span class=\"pln\"> <\/span><span class=\"typ\">String<\/span><span class=\"pln\"> DATABASE_NAME <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> <\/span><span class=\"str\">\"database_name\"<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\n  <\/span><span class=\"kwd\">private<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">static<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">final<\/span><span class=\"pln\"> <\/span><span class=\"typ\">String<\/span><span class=\"pln\"> DATABASE_TABLE <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> <\/span><span class=\"str\">\"table_name\"<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\n  <\/span><span class=\"kwd\">private<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">static<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">final<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">int<\/span><span class=\"pln\"> DATABASE_VERSION <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> <\/span><span class=\"lit\">1<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\n\n  <\/span><span class=\"kwd\">public<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">static<\/span><span class=\"pln\"> <\/span><span class=\"typ\">DatabaseHelper<\/span><span class=\"pln\"> getInstance<\/span><span class=\"pun\">(<\/span><span class=\"typ\">Context<\/span><span class=\"pln\"> context<\/span><span class=\"pun\">)<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\">\n\n    <\/span><span class=\"com\">\/\/ Use the application context, which will ensure that you <\/span><span class=\"pln\">\n    <\/span><span class=\"com\">\/\/ don't accidentally leak an Activity's context.<\/span><span class=\"pln\">\n    <\/span><span class=\"com\">\/\/ See this article for more information: <a class=\"vglnk\" href=\"https:\/\/bit.ly\/6LRzfx\" rel=\"nofollow\"><span>https<\/span><span>:\/\/<\/span><span>bit<\/span><span>.<\/span><span>ly<\/span><span>\/<\/span><span>6LRzfx<\/span><\/a><\/span><span class=\"pln\">\n    <\/span><span class=\"kwd\">if<\/span><span class=\"pln\"> <\/span><span class=\"pun\">(<\/span><span class=\"pln\">sInstance <\/span><span class=\"pun\">==<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">null<\/span><span class=\"pun\">)<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\">\n      sInstance <\/span><span class=\"pun\">=<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">new<\/span><span class=\"pln\"> <\/span><span class=\"typ\">DatabaseHelper<\/span><span class=\"pun\">(<\/span><span class=\"pln\">context<\/span><span class=\"pun\">.<\/span><span class=\"pln\">getApplicationContext<\/span><span class=\"pun\">());<\/span><span class=\"pln\">\n    <\/span><span class=\"pun\">}<\/span><span class=\"pln\">\n    <\/span><span class=\"kwd\">return<\/span><span class=\"pln\"> sInstance<\/span><span class=\"pun\">;<\/span><span class=\"pln\">\n  <\/span><span class=\"pun\">}<\/span><span class=\"pln\">\n\n  <\/span><span class=\"com\">\/**\n   * Constructor should be private to prevent direct instantiation.\n   * make call to static factory method \"getInstance()\" instead.\n   *\/<\/span><span class=\"pln\">\n  <\/span><span class=\"kwd\">private<\/span><span class=\"pln\"> <\/span><span class=\"typ\">DatabaseHelper<\/span><span class=\"pun\">(<\/span><span class=\"typ\">Context<\/span><span class=\"pln\"> context<\/span><span class=\"pun\">)<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\">\n    <\/span><span class=\"kwd\">super<\/span><span class=\"pun\">(<\/span><span class=\"pln\">context<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> DATABASE_NAME<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">null<\/span><span class=\"pun\">,<\/span><span class=\"pln\"> DATABASE_VERSION<\/span><span class=\"pun\">);<\/span><span class=\"pln\">\n  <\/span><span class=\"pun\">}<\/span><span class=\"pln\">\n<\/span><span class=\"pun\">}<\/span><\/code><\/pre>\n<p>&nbsp;<\/p>\n<h3>So when Should You NOT use a Singleton Approach?<\/h3>\n<p>Reality check, Singleton patterns are one of the most inappropriately used patterns in today\u2019s world. Most of the programmers use it for replacing the global variable idea because of misguidance. The real problem with the Singleton approach is that they give you a good excuse not to think carefully about the appropriate visibility of an object. So think before you choose your design pattern so that your Code works efficiently!<\/p>\n<p>Singletons are also bad when it comes to&nbsp;<b>clustering<\/b>. Because then, you do not have \u201cexactly one singleton\u201d in your application anymore. Let\u2019s check this out.<\/p>\n<p>Consider the following situation: As a developer, you have to create a web application which accesses a database. To ensure that concurrent database calls do not conflict each other, you create a thread-save SingletonDao as follows:<\/p>\n<pre class=\"prettyprint prettyprinted\" style=\"\"><button class=\"clipboard\"><img decoding=\"async\" src=\"\/wp-content\/plugins\/cc-syntax-highlight\/clipboard\/clipboard.svg\"><\/button><code class=\"prettyprint\"><span class=\"kwd\">public<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">class<\/span><span class=\"pln\"> <\/span><span class=\"typ\">SingletonDao<\/span><span class=\"pln\"> <\/span><span class=\"pun\">{<\/span><span class=\"pln\">\n    <\/span><span class=\"com\">\/\/ songleton's static variable and getInstance() method etc. omitted<\/span><span class=\"pln\">\n    <\/span><span class=\"kwd\">public<\/span><span class=\"pln\"> <\/span><span class=\"kwd\">void<\/span><span class=\"pln\"> writeXYZ<\/span><span class=\"pun\">(...){<\/span><span class=\"pln\">\n        <\/span><span class=\"kwd\">synchronized<\/span><span class=\"pun\">(...){<\/span><span class=\"pln\">\n            <\/span><span class=\"com\">\/\/ some database writing operations...<\/span><span class=\"pln\">\n        <\/span><span class=\"pun\">}<\/span><span class=\"pln\">\n    <\/span><span class=\"pun\">}<\/span><span class=\"pln\">\n<\/span><span class=\"pun\">}<\/span><\/code><\/pre>\n<p>So you are sure that only one singleton in your application exists and all databases go through this one and only&nbsp;<a title=\"See what People are saying About SingletonDao\" href=\"https:\/\/www.linkedin.com\/groups\/should-we-make-our-Data-145246.S.112264905\" target=\"_blank\" rel=\"noopener noreferrer\"><b>SingletonDao<\/b><\/a>.&nbsp;Your production environment now looks like this:<\/p>\n<p>&nbsp;<\/p>\n<p>One Singleton in your App<\/p>\n<p>Now, consider you want to set up multiple instances of your web application in a cluster. You suddenly have something like this now:<\/p>\n<p>&nbsp;<\/p>\n<p>Multiple Instances of your Web App<\/p>\n<p>&nbsp;<\/p>\n<p><b>So you have many singletons in your application<\/b>. And that is exactly what a singleton is not supposed to be: Having many objects of it. This is especially bad if you, as shown in this example, want to make synchronized calls to a database. But the message of this example is:&nbsp;You can not rely that there is exactly one instance of a singleton in your application \u2013 especially when it comes to clustering.<\/p>\n<p>So Singleton is not applicable here. What can you use instead? You can solve this by creating different instances of &nbsp;the database and can be use <a title=\"Learn More on Database Connection Pooling\" href=\"https:\/\/docs.oracle.com\/cd\/A97335_02\/apps.102\/a83724\/connpoc2.htm\" target=\"_blank\" rel=\"noopener noreferrer\">database connection pooling<\/a> to control the number of instances. You can &nbsp;also use the&nbsp;<b>Factory Pattern<\/b>&nbsp;(which we will discuss in our later blogs)&nbsp;which creates a different component of databases (You should off-course provide database pooling).<\/p>\n<p>Stay tuned for more Design Patterns coming your way here! Subscribe to our Blogs to know more about Design Patterns &amp; Other Tech Know-Hows!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code like a Pro | Courtesy: blogs.warwick.ac.uk We all know how important design patterns are, right? Well Just to bring you to perspective. Imagine, you compiled a code of say 10,000 lines without any definite structure. How difficult would it be for you to modify &amp; restructure the code if required. Even if someone else [&hellip;]<\/p>\n","protected":false},"author":192,"featured_media":15804,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[914,2037,1949],"tags":[3645,2359,3647,3651,3654],"class_list":["post-30114","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cloud","category-news-more-india","category-tech-india","tag-code-design-pattern-india","tag-coding-best-practices-india","tag-learn-about-singleton-design-pattern-india","tag-singleton-design-pattern-india","tag-what-is-singleton-design-pattern-india"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>The Singleton Approach - Coding like a Pro with the Right Design Patterns - Blazeclan<\/title>\n<meta name=\"description\" content=\"We all know how important design patterns are, right? Well Just to bring you to perspective. Imagine, you compiled a code of say 10,000 lines without any definite structure.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blazeclan.com\/india\/blog\/singleton-approach-coding-pro-right-design-patterns\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"The Singleton Approach - Coding like a Pro with the Right Design Patterns - Blazeclan\" \/>\n<meta property=\"og:description\" content=\"We all know how important design patterns are, right? Well Just to bring you to perspective. Imagine, you compiled a code of say 10,000 lines without any definite structure.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/\" \/>\n<meta property=\"og:site_name\" content=\"Blazeclan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/blazeclan.hq\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-25T12:51:37+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-11T16:25:41+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1650\" \/>\n\t<meta property=\"og:image:height\" content=\"680\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Team Blazeclan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@blazeclan_hq\" \/>\n<meta name=\"twitter:site\" content=\"@blazeclan_hq\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Team Blazeclan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/blazeclan.com\/india\/blog\/singleton-approach-coding-pro-right-design-patterns\/\"},\"author\":{\"name\":\"Team Blazeclan\",\"@id\":\"https:\/\/blazeclan.com\/india\/#\/schema\/person\/779910eccddff4a1ea6663b6bfb271e8\"},\"headline\":\"The Singleton Approach &#8211; Coding like a Pro with the Right Design Patterns\",\"datePublished\":\"2022-11-25T12:51:37+00:00\",\"dateModified\":\"2023-03-11T16:25:41+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blazeclan.com\/india\/blog\/singleton-approach-coding-pro-right-design-patterns\/\"},\"wordCount\":1212,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blazeclan.com\/india\/#organization\"},\"image\":{\"@id\":\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png\",\"keywords\":[\"Code Design Pattern\",\"Coding Best Practices\",\"Learn about Singleton Design Pattern\",\"Singleton Design Pattern\",\"What is Singleton Design Pattern\"],\"articleSection\":[\"Cloud\",\"News &amp; More\",\"Tech\"],\"inLanguage\":\"en-IN\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blazeclan.com\/india\/blog\/singleton-approach-coding-pro-right-design-patterns\/\",\"url\":\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/\",\"name\":\"The Singleton Approach - Coding like a Pro with the Right Design Patterns - Blazeclan\",\"isPartOf\":{\"@id\":\"https:\/\/blazeclan.com\/india\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png\",\"datePublished\":\"2022-11-25T12:51:37+00:00\",\"dateModified\":\"2023-03-11T16:25:41+00:00\",\"description\":\"We all know how important design patterns are, right? Well Just to bring you to perspective. Imagine, you compiled a code of say 10,000 lines without any definite structure.\",\"breadcrumb\":{\"@id\":\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#breadcrumb\"},\"inLanguage\":\"en-IN\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-IN\",\"@id\":\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#primaryimage\",\"url\":\"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png\",\"contentUrl\":\"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png\",\"width\":1650,\"height\":680},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blazeclan.com\/india\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"The Singleton Approach &#8211; Coding like a Pro with the Right Design Patterns\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blazeclan.com\/india\/#website\",\"url\":\"https:\/\/blazeclan.com\/india\/\",\"name\":\"Blazeclan\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/blazeclan.com\/india\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blazeclan.com\/india\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-IN\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/blazeclan.com\/india\/#organization\",\"name\":\"Blazeclan\",\"url\":\"https:\/\/blazeclan.com\/india\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-IN\",\"@id\":\"https:\/\/blazeclan.com\/india\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blazeclan.com\/wp-content\/uploads\/2024\/10\/ITCI-Blazeclan_logo.svg\",\"contentUrl\":\"https:\/\/blazeclan.com\/wp-content\/uploads\/2024\/10\/ITCI-Blazeclan_logo.svg\",\"caption\":\"Blazeclan\"},\"image\":{\"@id\":\"https:\/\/blazeclan.com\/india\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/blazeclan.hq\/\",\"https:\/\/x.com\/blazeclan_hq\",\"https:\/\/www.instagram.com\/blazeclantechnologies\/\",\"https:\/\/www.linkedin.com\/company\/blazeclan-technologies\/\",\"https:\/\/www.youtube.com\/channel\/UCCKF4Lcbtus-pUoZr7Lxrow\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/blazeclan.com\/india\/#\/schema\/person\/779910eccddff4a1ea6663b6bfb271e8\",\"name\":\"Team Blazeclan\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-IN\",\"@id\":\"https:\/\/blazeclan.com\/india\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a43c1fa01bb3c7e839254c9084bf11ed422d7e633231f9e935096045af416ba2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a43c1fa01bb3c7e839254c9084bf11ed422d7e633231f9e935096045af416ba2?s=96&d=mm&r=g\",\"caption\":\"Team Blazeclan\"},\"sameAs\":[\"http:\/\/localhost\/ps-local-wp\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"The Singleton Approach - Coding like a Pro with the Right Design Patterns - Blazeclan","description":"We all know how important design patterns are, right? Well Just to bring you to perspective. Imagine, you compiled a code of say 10,000 lines without any definite structure.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blazeclan.com\/india\/blog\/singleton-approach-coding-pro-right-design-patterns\/","og_locale":"en_US","og_type":"article","og_title":"The Singleton Approach - Coding like a Pro with the Right Design Patterns - Blazeclan","og_description":"We all know how important design patterns are, right? Well Just to bring you to perspective. Imagine, you compiled a code of say 10,000 lines without any definite structure.","og_url":"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/","og_site_name":"Blazeclan","article_publisher":"https:\/\/www.facebook.com\/blazeclan.hq\/","article_published_time":"2022-11-25T12:51:37+00:00","article_modified_time":"2023-03-11T16:25:41+00:00","og_image":[{"width":1650,"height":680,"url":"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png","type":"image\/png"}],"author":"Team Blazeclan","twitter_card":"summary_large_image","twitter_creator":"@blazeclan_hq","twitter_site":"@blazeclan_hq","twitter_misc":{"Written by":"Team Blazeclan","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#article","isPartOf":{"@id":"https:\/\/blazeclan.com\/india\/blog\/singleton-approach-coding-pro-right-design-patterns\/"},"author":{"name":"Team Blazeclan","@id":"https:\/\/blazeclan.com\/india\/#\/schema\/person\/779910eccddff4a1ea6663b6bfb271e8"},"headline":"The Singleton Approach &#8211; Coding like a Pro with the Right Design Patterns","datePublished":"2022-11-25T12:51:37+00:00","dateModified":"2023-03-11T16:25:41+00:00","mainEntityOfPage":{"@id":"https:\/\/blazeclan.com\/india\/blog\/singleton-approach-coding-pro-right-design-patterns\/"},"wordCount":1212,"commentCount":0,"publisher":{"@id":"https:\/\/blazeclan.com\/india\/#organization"},"image":{"@id":"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#primaryimage"},"thumbnailUrl":"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png","keywords":["Code Design Pattern","Coding Best Practices","Learn about Singleton Design Pattern","Singleton Design Pattern","What is Singleton Design Pattern"],"articleSection":["Cloud","News &amp; More","Tech"],"inLanguage":"en-IN","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blazeclan.com\/india\/blog\/singleton-approach-coding-pro-right-design-patterns\/","url":"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/","name":"The Singleton Approach - Coding like a Pro with the Right Design Patterns - Blazeclan","isPartOf":{"@id":"https:\/\/blazeclan.com\/india\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#primaryimage"},"image":{"@id":"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#primaryimage"},"thumbnailUrl":"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png","datePublished":"2022-11-25T12:51:37+00:00","dateModified":"2023-03-11T16:25:41+00:00","description":"We all know how important design patterns are, right? Well Just to bring you to perspective. Imagine, you compiled a code of say 10,000 lines without any definite structure.","breadcrumb":{"@id":"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#breadcrumb"},"inLanguage":"en-IN","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/"]}]},{"@type":"ImageObject","inLanguage":"en-IN","@id":"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#primaryimage","url":"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png","contentUrl":"https:\/\/blazeclan.com\/wp-content\/uploads\/2014\/01\/The-Singleton-Approach-\u2013-Coding-like-a-Pro-with-the-Right-Design-Patterns-2.png","width":1650,"height":680},{"@type":"BreadcrumbList","@id":"https:\/\/blazeclan.com\/blog\/singleton-approach-coding-pro-right-design-patterns\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blazeclan.com\/india\/"},{"@type":"ListItem","position":2,"name":"The Singleton Approach &#8211; Coding like a Pro with the Right Design Patterns"}]},{"@type":"WebSite","@id":"https:\/\/blazeclan.com\/india\/#website","url":"https:\/\/blazeclan.com\/india\/","name":"Blazeclan","description":"","publisher":{"@id":"https:\/\/blazeclan.com\/india\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blazeclan.com\/india\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-IN"},{"@type":"Organization","@id":"https:\/\/blazeclan.com\/india\/#organization","name":"Blazeclan","url":"https:\/\/blazeclan.com\/india\/","logo":{"@type":"ImageObject","inLanguage":"en-IN","@id":"https:\/\/blazeclan.com\/india\/#\/schema\/logo\/image\/","url":"https:\/\/blazeclan.com\/wp-content\/uploads\/2024\/10\/ITCI-Blazeclan_logo.svg","contentUrl":"https:\/\/blazeclan.com\/wp-content\/uploads\/2024\/10\/ITCI-Blazeclan_logo.svg","caption":"Blazeclan"},"image":{"@id":"https:\/\/blazeclan.com\/india\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/blazeclan.hq\/","https:\/\/x.com\/blazeclan_hq","https:\/\/www.instagram.com\/blazeclantechnologies\/","https:\/\/www.linkedin.com\/company\/blazeclan-technologies\/","https:\/\/www.youtube.com\/channel\/UCCKF4Lcbtus-pUoZr7Lxrow"]},{"@type":"Person","@id":"https:\/\/blazeclan.com\/india\/#\/schema\/person\/779910eccddff4a1ea6663b6bfb271e8","name":"Team Blazeclan","image":{"@type":"ImageObject","inLanguage":"en-IN","@id":"https:\/\/blazeclan.com\/india\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a43c1fa01bb3c7e839254c9084bf11ed422d7e633231f9e935096045af416ba2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a43c1fa01bb3c7e839254c9084bf11ed422d7e633231f9e935096045af416ba2?s=96&d=mm&r=g","caption":"Team Blazeclan"},"sameAs":["http:\/\/localhost\/ps-local-wp"]}]}},"_links":{"self":[{"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/posts\/30114","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/users\/192"}],"replies":[{"embeddable":true,"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/comments?post=30114"}],"version-history":[{"count":0,"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/posts\/30114\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/media\/15804"}],"wp:attachment":[{"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/media?parent=30114"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/categories?post=30114"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blazeclan.com\/india\/wp-json\/wp\/v2\/tags?post=30114"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}