Exploring Design Patterns - Singleton design pattern

Exploring Design Patterns - Singleton design pattern

Singleton Design Pattern

Singleton design pattern is the easiest design pattern to get into the world of design patterns. singleton design pattern is a type of creational design patterns as it deals with how the object is created. In fact most of us have already used this design pattern unintentionally, so let's get litter deeper into it and see the how to implement part.


Singleton design pattern consists of the three main parts :

  1. private static instance
  2. private constructor
  3. single global point of access



Article content
singleton design pattern

Let's see whet are these and how it is useful, specially if you think there's too much to digest.

private static instance

Ensures that class should have single instance (static) which should not be accessed directly (private).

private constructor

Making constructors private so that no one will be able to create the object of the class outside of the class.

single point of global access

Ensures that for each request to an instance of the class, there should be only single instance which has to be provided, sharing the instance globally across the entire application.


Example

let say if someone comes and asks you the same question again and again what will you do ? You will write it on the paper and show them the paper each time they asks the question, similarly here, we hold the instance of the object once created, and return the instance each time when requested without creating object it again and again.

I hope hadn't made it too complex, let me know if you have any questions, I will try my best to clear your doubts, meanwhile you can follow me [ smit joshi ] for related articles.

To view or add a comment, sign in

More articles by smit joshi

  • Design Patterns - Overview

    Design patterns are the way to solve the common problem's that occur during the development of application, or…

  • Spring Into Action With GitHub Code Spaces

    Hello friends today we'll learn to use GitHub Code spaces. Prerequisites: A Blank GitHub Repo with README file.

    1 Comment

Explore content categories