<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Poorna Senani Gamage</title>
    <description>The latest articles on DEV Community by Poorna Senani Gamage (@notitazone).</description>
    <link>https://dev.to/notitazone</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F317925%2Fe93ab2b8-70b2-4eb1-b8ed-32e80bc84a06.jpeg</url>
      <title>DEV Community: Poorna Senani Gamage</title>
      <link>https://dev.to/notitazone</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/notitazone"/>
    <language>en</language>
    <item>
      <title>Singleton Pattern</title>
      <dc:creator>Poorna Senani Gamage</dc:creator>
      <pubDate>Sat, 18 Jan 2020 10:13:10 +0000</pubDate>
      <link>https://dev.to/notitazone/singleton-pattern-2p23</link>
      <guid>https://dev.to/notitazone/singleton-pattern-2p23</guid>
      <description>&lt;p&gt;Singleton pattern is the simplest pattern in java and is under the creational pattern. some key points to use in singleton pattern.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It is static in nature&lt;/li&gt;
&lt;li&gt;private constructor&lt;/li&gt;
&lt;li&gt;private static instance of the class&lt;/li&gt;
&lt;li&gt;public static getter method&lt;/li&gt;
&lt;li&gt;no parameters to the constructor&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;create singleton class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Singleton {
    private static Singleton object = null;
    private Singleton() {
    }
    public static Singleton getInstance() {
        if (object == null) {
            object = new Singleton();
        }
        return object;
    }
    public void showMessage() {
        System.out.println("Singleton Pattern");
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;create demo class&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Demo {
    public static void main(String[] args) {
        Singleton object = Singleton.getInstance();
        object.showMessage();
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;but this classic implementation not thread safe. therefore using synchronized make sure that the one thread can execute the getInstance();at one time.&lt;br&gt;
As this;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Singleton {
    private static Singleton object = null;
    private Singleton() {
    }
    public static synchronized Singleton getInstance() {
        if (object == null) {
            object = new Singleton();
        }
        return object;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>design</category>
      <category>pattern</category>
      <category>singleton</category>
      <category>java</category>
    </item>
  </channel>
</rss>
