{"id":2083,"date":"2017-03-26T12:57:44","date_gmt":"2017-03-26T12:57:44","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=2083"},"modified":"2024-02-11T07:36:25","modified_gmt":"2024-02-11T07:36:25","slug":"singleton-vs-factory-pattern","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/singleton-vs-factory-pattern\/","title":{"rendered":"Singleton vs Factory pattern"},"content":{"rendered":"<p><strong>Q1.What is the difference between Singleton vs Factory pattern?<\/strong><br \/>\nA singleton pattern ensures that you always get back the same instance of whatever type you are retrieving, whereas the factory pattern generally gives you a different instance of each type.<\/p>\n<p>The purpose of the singleton is where you want all calls to go through the same instance. An example of this might be a class that manages a disk cache, or gets data from a static dictionary; wherever it is important only one known instance interacts with the resource. This does make it less scalable.The purpose of the factory is to create and return new instances. Often, these won&#8217;t actually be the same type at all, but they will be implementations of the same base class. However, there may be many instances of each type<\/p>\n<p><strong>Q2.How to stop create instance via Reflection in Singleton<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nprivate Singleton() {\r\n    if (singleton != null) {\r\n        throw new IllegalStateException(&quot;Singleton already constructed&quot;);\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Q3.What are different ways of preventing object creation in singleton?<\/strong><br \/>\nThere are 5 ways of creating Objects. The below code explains how to prevent object creation by all 5 methods. Instead of<br \/>\ndoing all these we can create singleton by using <strong>ENUM<\/strong><br \/>\n<strong>Singleton.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic class Singleton implements Serializable \r\n{\r\n private static final long serialVersionUID = 3119105548371608200 L;\r\n private static final Singleton singleton = new Singleton();\r\n \r\n \/\/Prevents Class creation by Constructor\r\n private Singleton() \r\n {\r\n     \/\/Prevents Object creation by reflection\r\n     if( Singleton.singleton != null ) \r\n     {\r\n        throw new InstantiationError( &quot;Creating of this object is not allowed.&quot; );\r\n     }\r\n }\r\n \r\n public static Singleton getInstance() \r\n {\r\n  return singleton;\r\n }\r\n \r\n \/\/Prevents Class creation during cloning\r\n @Override\r\n protected Object clone() throws CloneNotSupportedException \r\n {\r\n  throw new CloneNotSupportedException(&quot;Cloning of this class is not allowed&quot;);\r\n }\r\n \r\n \/\/Prevents New Object Creation by returning same singleton object\r\n protected Object readResolve() \r\n {\r\n  return singleton;\r\n }\r\n}\r\n<\/pre>\n<p><strong>Main.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\ntry {\r\n    Class&lt;Singleton&gt; singletonClass = (Class&lt;Singleton&gt;) Class.forName(&quot;test.singleton.Singleton&quot;);\r\n    Singleton singletonReflection = singletonClass.newInstance();\r\n} catch (ClassNotFoundException e) {\r\n    e.printStackTrace();\r\n} catch (CloneNotSupportedException e) {\r\n    e.printStackTrace();\r\n} catch (IllegalAccessException e) {\r\n    e.printStackTrace();\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Q1.What is the difference between Singleton vs Factory pattern? A singleton pattern ensures that you always get back the same instance of whatever type you are retrieving, whereas the factory pattern generally gives you a different instance of each type. The purpose of the singleton is where you want all calls to go through the&hellip; <a href=\"https:\/\/codethataint.com\/blog\/singleton-vs-factory-pattern\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[224,64],"tags":[],"class_list":["post-2083","post","type-post","status-publish","format-standard","hentry","category-interview-questions-design-patterns","category-design-patterns"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2083","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=2083"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2083\/revisions"}],"predecessor-version":[{"id":3347,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/2083\/revisions\/3347"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=2083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=2083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=2083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}