How to implement Singleton Design Pattern using Java?
Tags: beginners, programming, webdev, opensource
Published: 2021-12-28 17:04:38


## Design Pattern

Hello 👋Its Rohan Kadam😊

Maybe you are a newbie to coding👩‍💻 or programming, maybe experienced👴 or maybe a FrontEnd Developer or BackEnd Developer you all have may have come across the word Design Pattern as a principle or as a concept. The design pattern is a way or approach to writing code or developing an application. In this article, we try to answer certain questions What is Design Pattern? Why implement a design pattern? How to implement a Design pattern to be precise **Singleton Design Pattern**?

![How to implement Singleton Pattern?](https://cdn-images-1.medium.com/max/3840/1*A7gLv9L-EdhUT6g78v3fdA.png)

### What is Design Pattern?

**Design patterns** are the solutions to commonly occurring problems in software design. They are like pre-fabricated blueprints that we can customize to solve a recurring design problem in your code.

They are not pieces of code or libraries that can be imported and used in the development of software or solving a particular problem. We can follow the pattern details and implement a solution that suits the realities of your own program.

We often confused ourself between design pattern and algorithms. While an algorithm always defines a clear set of actions that can achieve some goal, a pattern is a more upper-level description of a solution.

An analogy to an algorithm is a cooking dish: both have clear steps to achieve a goal. They’re not libraries or modules; they’re guidelines you integrate into the core of your designs, giving you a leg up in creating flexible and maintainable object-oriented systems.

### Why implement a Design Pattern?

We have encountered design patterns only in a nutshell🌰, they’re general object-oriented solutions that you can use in your own designs. Crafted by experienced 👴object-oriented practitioners, design patterns can make your designs more flexible, more resilient to change, and easier to maintain. So, if design patterns still aren’t part of your development toolbelt, here are five reasons you should add them.

1 → Make our life easier by not reinventing the wheel

2 → Improve our object-oriented Skills

3 → Recognize Patterns in libraries and languages

4 → Use the power of a shared vocabulary

5 → Find Truth and beauty

### How to implement a Singleton Design Pattern?

Before jumping directly into the implementation of the design pattern we need to answers certain questions such as What is Singleton design pattern? Why incorporate the design pattern? and finally How to implements a Singleton Design Pattern?

**1 →What is Singleton Design Pattern?**

Singleton is part of the Creational Design Pattern Family, it let us ensure that a class or object only has one instance. A real-life example will be, **Imagine you work for a big company that has cloud storage💾 system for storing shared resources of files, images and documents we create shared storage as creating separate cloud storage💾 for every user👩‍💻 may be costly**.

**Definition:** 
*The singleton pattern is a design pattern that restricts the instantiation of a class to one object.*

**2 → Why implement Singleton Design Pattern?**

 1. We can be sure that a class has only a single instance.

 2. We gain a global access point to that instance.

 3. The Singleton Object is initialized only when it's requested for the first time.

**3 → How to Implement Singleton Design Pattern?**

In section, we try to implement Singleton Design Pattern using Java. For example, we will use a shared resource cloud to understand Singleton Design Pattern.

In this example, the Shared Cloud class acts as a **Singleton**. This class doesn’t have a public constructor, so the only way to get its object is to call the getSharedCloudInstance method. This method caches the first created object and returns it in all subsequent calls.

![Singleton Class of Shared Cloud](https://cdn-images-1.medium.com/max/2568/1*amSQgUD7iQrWBwS6pvX9eA.png)

In this example, the Design Pattern class acts as a **Main**.

![Main Class of Design Pattern](https://cdn-images-1.medium.com/max/3996/1*5evsg2VQ80wLc2LlGwP4zg.png)

In this example, below is **Output**. If we can notice that all the Users accessing the shared cloud resource since all of them have the **same** **hashcode**.

![Output](https://cdn-images-1.medium.com/max/2488/1*6tWb8MzNouqc_DCeZUm4XQ.png)

### CodeBase:-

Below Example, CodeBase is available on Github.Checkout below link for the same
[**GitHub - Rohan2596/Design-pattern-examples at pattern/singleton**](https://github.com/Rohan2596/Design-pattern-examples/tree/pattern/singleton)

### Conclusion:-

In the article, we tried to answers many questions related to Design Pattern and especially **Singleton Design Pattern** how it helps developers around the world to write better code and built faster applications.

Please do share and like💖 if you find the article useful. Follow me on medium [Rohan Ravindra Kadam](undefined) and on Twitter at [**rohankadam25](https://twitter.com/rohankadam25)**

### Bibliography:-
[**5 reasons to finally learn design patterns**](https://www.oreilly.com/content/5-reasons-to-finally-learn-design-patterns/)
[**Singleton**](https://refactoring.guru/design-patterns/singleton)

![Thank You, Viewers — Rohan Kadam](https://cdn-images-1.medium.com/max/2800/1*mRwSuSvhuRcZKS3uElP6bg.png)