Singleton implementation in Maven Java sample project

In this post I will create an implementation of the Singleton design pattern in a Maven Java project, my sample has  passed the sonar quality check   (it’s the case of all take4make sample ).

This the structure of the maven java project :

basically I create Singleton class  and Caller class which calls Singleton

So to create the Java  Singleton class I need to answer these 2 questions.

How  to instantiate the singleton class only one time during execution life (in other words  how to  make impossible to instantiate more than one instance  )?

How to get access on the alone singleton instance from every where?

So we need to create singleton class with a private constructor so that it will be impossible to instantiate it by other classes. Also we need to create a static attribute of type Singleton myInstance which will hold the unique instance reference. last thing we will create static method getInstance() that check if myInstance null and then create it using the private constructor. So getInstance() will  take in charge checking that we have only an alone instance of Singleton class, and return it. Also it will create if it’s not already created. This will guarantee instantiation in the first call.

Tt’s useful to use synchronized  modifier for getInstance() method to avoid problem in case of simultaneous use by many thread.

Singleton.java

/*
* created in scope of www.take4make.com samples
* @author khalil CHARA
*/

package com.take4make.singleton;

/**
* Singleton class created in scope of www.take4make.com samples
* @author khalil CHARA
*/
public final class Singleton
{
/**
* single instance of Singleton class , it's private to avoid access directly by other classes
*/
private static Singleton myInstance;

/**
* double number to check unicity of singleton instance
*/

private double randomNumber;
/** private constructor to avoid use by other classes
* the constructor chose random number to fill randomNumber variable,
* this will show us in case of many call of singleton that it's about the same instance.
*/
private Singleton()
{
//generate random number to prove for caller that there is only one instance created
this.randomNumber=Math.random();
}

/**
* get local and single instance myInstance
* it has static modifier to be the point of access
* it has synchronized modifier to avoid use by many thread at the same time.
* this method create the single instance in the first call. see the Caller class to complete the vision
* @return
*/
public static synchronized Singleton getInstance()
{
if (myInstance == null)
{
myInstance = new Singleton();
}
return myInstance;
}
/**
* get Random Number
* @return double
*/
public double getRandomNumber()
{
return randomNumber;
}
}

 

NB: The random attribute is for test purpose in Caller class to show that only one instance of singleton is created

Caller.java

/*
* created in scope of www.take4make.com samples
* @author khalil CHARA
*/

package com.take4make.singleton;
import org.apache.log4j.Logger;

/**
* Caller Class that use the Singleton class, created in scope of www.take4make.com samples
* @author khalil CHARA
*/
public class Caller
{
/**
* Logger for logging information in the consol
*/
private static final Logger LOGGER = Logger.getLogger(Singleton.class);
/**
* main methode to execute singleton sample
* @param args
*/
public static void main(String[] args)
{
//this first call of getInstance will create the single instance of Singleton class and return it.
Singleton sngltnT1=Singleton.getInstance();
//this second call of getInstance will just return the single instance already created before.
Singleton sngltnT2=Singleton.getInstance();
LOGGER.debug("sngltnT1 number is : "+sngltnT1.getRandomNumber());
LOGGER.debug("sngltnT2 number is : "+sngltnT2.getRandomNumber());
}
}

Result of the execution :

2019-01-06 20:38:21 DEBUG Singleton:16 – sngltnT1 number is : 0.48172858244605954
2019-01-06 20:38:21 DEBUG Singleton:17 – sngltnT2 number is : 0.48172858244605954

You can see that it’s about the same instance (for sngltnT1 and sngltnT2) because the random number is the same. If it was about different instances the random number will be different.

Down load the full project : T4MJavaSingleton

Leave a Reply

Your email address will not be published. Required fields are marked *