Skip to content

Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. It seems to be a very simple design pattern but when it comes to implementation, it comes with a lot of implementation concerns. The implementation of Singleton pattern has always been a controversial topic…

Notifications You must be signed in to change notification settings

Design-pattrns/Singleton-Pattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Pattern Singleton

Pattern Singleton: > One Class, one Instance.

Singleton is one of the Gangs of Four Design patterns and comes in the Creational Design Pattern category. Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the Java virtual machine. The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, driver objects, caching and thread pool. Singleton design pattern is also used in other design patterns like Abstract Factory, Builder, Prototype, Facade etc. Singleton design pattern is used in core Java classes also, for example java.lang.Runtime , java.awt.Desktop.

Screenshot of the Full code

alt text

To implement Singleton pattern, there are really many approaches but all of them have following common concepts:

  • A private constructor to avoid instantiation of the class,
  • A private static variable from the same class that's the only instance of the class.
  • public static method that returns the instance of the class, this is the global access point for the outer world to get the instance of the class.

We'll implement the thread safe one here. Classes are in the package com.singleton;

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

   public static Singleton getInstance(){
       if (instance == null){
           return instance = new Singleton();
       }
       return instance;
   }
}

About

Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the java virtual machine. It seems to be a very simple design pattern but when it comes to implementation, it comes with a lot of implementation concerns. The implementation of Singleton pattern has always been a controversial topic…

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages