JavaRush /Java Blog /Random EN /Singleton pattern in your own words
Roman_kh
Level 33
Харьков

Singleton pattern in your own words

Published in the Random EN group

Hello everyone, javaRush!

Today I’ll tell you about the design pattern Singleton(single). Singleton pattern in your own words - 1Goal: create a class that will have only ONE object. This means that no matter how many times it is accessed, the same object that was created the first time will be returned. This is a convenient thing and necessary in many places, it’s not for nothing that it is being implemented into frameworks. Application:
  • For example, you need to connect a database to a project and a class that will be responsible for connecting to it. The connection is created once and there is no need to create it again and again
  • Application settings— a class responsible for the connection settings that are needed for the application: database host and port, etc. They are created once and used throughout the application's operation.
  • There are many more examples that I haven’t mentioned, so write your options in the comments! =)
After this introduction, as I understand it, we can show an example of this class: (Although I am sure that each of us can come up with an implementation of this) Here is the simplest example, when we make the constructor private, i.e. You cannot create an object explicitly. And there is a static method getInstance()that provides an object.

public class Singleton {
  private static Singleton instance;
  private Singleton () {}

  public static Singleton getInstance() {
    if (instance == null) {
      instance = new Singleton();
    }
    return instance;
  }
}
There are problems with multithreading and then you can put a getInstance()marker method synchronized:

public class Singleton {
  private static Singleton instance;
  private Singleton () {}

  public static synchronized Singleton getInstance() {
    if (instance == null) {
      instance = new Singleton();
    }
    return instance;
  }
}
In the end, as usual, I want to say that if you think differently or find a mistake in me, write in the comments! We will discuss everything with pleasure :) If you liked the article, write “+” and I will know it. This is important to me :) PS I’m adding more implementations: According to Joshua Bloch , this is the best way to implement the templateEnum Singleton

public enum Singleton {
   INSTANCE;
}
Double Checked Locking & volatile

public class Singleton {
        private static volatile Singleton instance;
   
        public static Singleton getInstance() {
      Singleton localInstance = instance;
      if (localInstance == null) {
         synchronized (Singleton.class) {
            localInstance = instance;
            if (localInstance == null) {
               instance = localInstance = new Singleton();
            }
         }
      }
      return localInstance;
   }
}
And further On Demand Holder idiom:

public class Singleton {
      
   public static class SingletonHolder {
      public static final Singleton HOLDER_INSTANCE = new Singleton();
   }
      
   public static Singleton getInstance() {
      return SingletonHolder.HOLDER_INSTANCE;
   }
}
+Lazy initialization +High performance -Cannot be used for non-static class fields Any questions/suggestions - write in the comments! See also my other articles:
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION