Skip to content

The Singleton Pattern - The One True Way to Create Objects

Published: at 7 min read

If you’re a “Software Engineer/Developer or a student of CSE” you must have heard of this term. Ever wondered what is a ‘Design Pattern’ Or what is the ‘Singleton Design Pattern’ if not we are here to save you. In this blog we will discuss about the Design Patterns and Singleton Design Pattern.

Singleton

Imagine you’re building a Lego masterpiece without any instructions. It’s fun at first, but soon you realize you’re missing a piece or two, and things aren’t fitting together as smoothly as you’d like. This is where ‘Design Patterns’ come in, like the missing Lego instructions for software developers. They’re like proven recipes for solving common problems in a way that makes your code both efficient and easy to manage. So, whether you’re a coder or just curious about how software works, let’s explore the world of design patterns and how they make software building a lot easier!

Table of contents

Open Table of contents

Design Pattern

Before knowing Design Pattern can you tell me what is a class ? A blueprint for object creation, Right ?
Great, In the same way you can refer the ‘Design Patterns’ are also some well tested approach or the best practices that can be used to solve the common problems while designing the applications or system. It is a description or template for how to solve a problem that can be used in many different situations.
But don’t be confused that Design Patterns are the solutions for a specific problem rather than It is a description or template for how to solve a problem that can be used in many different situations.
Design patterns had originally been categorized into 3 sub-classifications based on what kind of problem they solve.

Now you must have this question that :-

Hmm, Let's have a scenario :- Suppose you are building a house, I am sure you must have seen some houses or buildings and wished you might build one house for you as well, like that building. There are many ways to build a house, Right ? but some methods are more effective and efficient than others. Of course, you can build a house for you as you want without following anyone else, You could just wing it and hope for best. But if you want to build a sturdy and well-designed house, it's a great idea to use blueprints.
The same is true for software development. Design Patterns are the house or the method using which you can create an effective, efficient and reusable software. You don't have to use design patterns, but they can help you to write code that is more readable, maintainable, and extensible.

Singleton Design Pattern

The singleton design pattern is a one of the most use design pattern software design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to co-ordinate the actions across the system.
Sounds too technical is it?, Imagine you are living in a PG or a hostel and for the whole PG there is only one washing machine is there. So now everyone has to use that washing machine only for washing their cloth.
Now assume we are developing a Java Application to a school. Therefore, A School can have only one principal. As a result, a singleton class is used to represent a principal. These are the examples of ‘Singleton Design Pattern’.
In other words it is used to create a class that can have only one instance that can be accessed globally. Singleton is classified as a Creational Design Pattern.

Note:- If you’re a Java developer you’ll get only one instance per JVM which means only one singleton object can be made, per container.

Use cases for the Singleton Design Pattern

Some of the most common use cases are :-

Pros and Cons of Singleton Design Pattern

How to implement Singleton Design Pattern ?

Now I believe you have a better understanding about the Singleton Design Pattern,Now here is a common way of implementing the Singleton Design Pattern in JAVA :-


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

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

Here is the breakdown of the code:-

Final Takeway

The singleton design pattern is a creational pattern that ensures that a class has only one instance, and provides a global point of access to it. The singleton pattern can be a good choice when you need to ensure that there is only one instance of a class, such as when the class represents a shared resource. The singleton pattern can be a useful tool, but it is important to use it carefully. If not used correctly, the singleton pattern can lead to problems such as tight coupling and code that is difficult to test.

Share :
Written by:Akshat Jaiswal

Interested in Writing Blogs, showcase yourself ?

If you're passionate about technology and have insights to share, we'd love to hear from you! Fill out the form below to express your interest in writing technical blogs for us.

If you notice any issues in this blog post or have suggestions, please contact the author directly or send an email to hi@asdevs.dev.