Skip to main content

What is singleton design pattern?

Singleton design pattern

Image result for singleton design pattern
Singelton Design Pattern.

 The singleton design pattern is a software design pattern which ensures that a class of anyone object has only one instance (of computer science). In other words, we can also say that it restricts the instantiation of a class of any object. We require a singleton pattern because it may sometimes happen that we require only one object to coordinate with the action across the system. This concept is also applicable when we have to restrict instantiation over a number of objects.


The singleton pattern helps us to solve problems like:
  • Helps to ensure that a class has one instance.
  • Helps to access the sole instance of a class easily.
  • Helps to a class to control its instantiation.
  • Helps to restrict a number of instances of a class.
Now, you might be thinking about how to solve such problems. Here are a few key points which might help you:
  • The first key point is to make a class which is responsible by itself for controlling its instantiations.
  • The second point is to hide the constructor (those objects which are created from classes by subroutines) of a class. This is important to do because by doing this you declare privacy that ensures the class would never be instantiated from outside of the class.
  • The last point is to define a public static operation (for eg. , getInstance()) as this would return the sole instance of the class and is also easily accessible by using class name and operation name.
Another question might strike you when to use Singleton? Here is the answer to that, you need to use Singleton when you find that these criteria mentioned below are satisfying:
  • There must be ownership of a single instance.
  • There is no provision of global access.
  • And there is lazy initialization (kind of lazy evaluation of instantiation of objects/classes).
Abstract factory, builder and prototype pattern use singleton pattern for their implementations. State objects and façade objects are also singletons. The reason behind why Singleton patterns are more preferable than compared to global variables is that while using Singleton pattern at that time you are sure enough how many numbers of instances you are working with and you can change your mind and manage any number of instances you want to.
 The java class that allows us to create one object per JVM is called a singleton java class.
 E.g
 The Logger class of the Log4j API is given as singleton java class.

 Rules
 To create the singleton class, we need to have the following things.
 1. Static member
 It gets memory only once because of static, it contains the instance of the Singleton class.
 2. Private constructor
 It will prevent to instantiate the Singleton class from outside the class.
 3. Static factory method
 This provides the global point of access to the Singleton object and returns the instance to the caller.

Here is the syntax of Singleton implementation in two very known languages Java 
Singleton implementation in Java
  1. public final class Singleton
  2. {
  3. private static final Singleton INSTANCE = new Singleton();
  4. private Singleton() {}
  5. public static Singleton getInstance() {
  6. return INSTANCE;
  7. }
  8. }

Comments

Popular posts from this blog

What is JDK, JRE and JVM?

What are JDK, JRE, and JVM: JDK :- Java Development Kit (in short JDK) is Kit which provides the environment to Develop and execute(run ) the Java program. For eg. You(as Java Developer) are developing an accounting application on your machine, so what do you going to need into your machine to develop and run this desktop app? You are going to need  J-D-K  for that purpose for this you just need to go to the official website of sun or oracle to download the latest version of JDK into your machine. Hence, JDK is a kit(or package) which includes two things i) Development Tools(to provide an environment to develop your java programs) and ii) JRE (to execute your java program). JDK is only used by Java Developers. JRE: - Java Runtime Environment (to say JRE) is an installation package which provides an environment to only run(not develop) the java program(or application)onto your machine. For eg(continuing with the same example) after developing your accounting ap...

The Future of Java Developers in the Age of AI and ChatGPT

  In today's rapidly evolving tech landscape, AI tools like ChatGPT are becoming more integrated into the software development process. As a Java developer, whether you're just starting out or have years of experience, you might wonder: How can I stay relevant and succeed in the age of AI? In this post, we'll explore practical strategies to ensure you not only survive but thrive as a Java developer in this AI-driven world. 1. Embrace AI as a Tool, Not a Threat AI isn't here to replace you—it's here to help you. Tools like ChatGPT can automate repetitive tasks, assist with coding challenges, and even generate code snippets, allowing you to focus on more complex and creative aspects of development. For Freshers: Use AI tools to learn and understand Java more quickly. Let them assist you in writing code, but make sure you're also learning the logic and concepts behind it. For Experienced Developers: Integrate AI tools into your workflow to enhance productivity. L...

What are the best opportunities for java in 2021?

Firstly, Java is widely used for building enterprise-scale web applications. Java is known to be extremely stable and so, many large enterprises have adopted it. If you are looking for a development based job at a large organization, Java is the language that you should learn. It is widely used in Android App Development. So if anyone is willing to find a career in Android App Development, he/she should have an intermediate level of experience in Java. Some of the different domains where Java is used widely are as follows: Financial services:  It is used in server-side applications. Big Data:  Hadoop MapReduce framework is written using Java. Banking:  To deal with transaction management. Stock market:  To write algorithms as to which company they should invest in. Retail:  Billing applications that you see in a store/restaurant are completely written in Java. Android:  Applications are either written in Java or use Java API. Scientific and Research Communi...