design pattern

Singleton Design Pattern

Hi My friends, in this small tutorial we will discuss the Singleton design pattern and  I will show you an implementation in JAVA.

As you may already know  there are three types of design patterns: Creational, behavioral and structural. Singleton is among the creational patterns and it is the easiest one to learn.

Why Singleton? :

  • The need to have one and only instance of a class during the execution of the application.
  • The instance should not be accessible only from a method of the class.

Steps to implement the design :

  1. Make the constructor of the class that you want it to be singleton private.
  2. Create a public static method with return type of Class, this method will be responsible for the instantiation of the class.
  3. Add a private static attribute that represents the instance of the class .

The following code represents an implementation of this design pattern.