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

IPL 2025: The Ultimate Season Preview, Stats, and Predictions

 The Indian Premier League (IPL) 2025 is set to be bigger and better than ever. With a mega auction, new regulations, and rising superstars, this season is shaping up to be the most thrilling edition in IPL history. Let’s dive deep into team analyses, key player stats, historic milestones, and predictions to see what IPL 2025 has in store! IPL 2025 at a Glance Start Date: March 22, 2025 Final Match Venue: Eden Gardens, Kolkata Total Matches: 74 Teams: 10 New Rule: Introduction of 'Power Boost Over' (Allows a team to add an extra over to Powerplay if all wickets are intact by the 8th over) --- Interesting IPL 2025 Data & Records 1. The ₹100 Crore Auction - Highest Spending in IPL History Total Money Spent: ₹1050 crore (~$125 million) Most Expensive Player: Rishabh Pant (Lucknow Super Giants) - ₹27 crore Shreyas Iyer (Punjab Kings) - ₹26.75 crore Trent Boult (Delhi Capitals) - ₹18.5 crore 2. The ‘Orange Cap’ Race - Run-Scoring Machines Predicting the top contenders for the hi...

Java 24 Released: What's New and Why It Matters?

 Oracle has officially released Java 24 on March 18, 2025, bringing a host of new features aimed at improving developer productivity, security, and performance. This release includes over 20 enhancements, making Java even more powerful for modern application development. Let’s dive into the key features and improvements in Java 24 and understand why they matter. --- 1. AI and Machine Learning Support Java 24 introduces features designed to enhance AI-powered applications. Developers can now integrate AI models and perform AI inference more efficiently using built-in Java capabilities. Why It Matters? With AI becoming a crucial part of modern applications, Java 24 makes it easier to process and deploy AI models without relying heavily on third-party libraries. --- 2. Post-Quantum Cryptography To prepare for the future threats of quantum computing, Java 24 includes new cryptographic algorithms that protect against potential security breaches. Why It Matters? Quantum computers could p...

The Mysterious Journey of Astronaut Sunita Villms: A Struggle Between Earth and the Unknown

 Astronaut Sunita Williams and her colleague, Barry "Butch" Wilmore, recently returned to Earth after an unexpected nine-month mission aboard the International Space Station (ISS). Originally scheduled for a brief eight-day stay, their mission was extended due to technical issues with the Boeing Starliner spacecraft, leading to a prolonged 286-day duration in space.  Mission Extension and Challenges The mission's extension was primarily due to propulsion problems with the Starliner craft, necessitating their continued presence on the ISS. This unforeseen prolongation presented both physical and psychological challenges for the astronauts. Prolonged exposure to microgravity can lead to muscle atrophy, bone density loss, and other health concerns. To mitigate these effects, astronauts adhere to rigorous exercise regimens while aboard the station.  Health Observations Upon Return Upon their return, noticeable physical changes were observed, particularly with Sunita Williams,...