Singleton Design Pattern and Singleton Class in Java - How you can write your singleton class
What is a singleton design pattern?

Singleton design pattern is a design pattern in which only one instance (object) of a class can be created. One of the main advantages of this is its efficient memory management.

What is a singleton class?

A singleton class is a class where only one instance (object) can be created from this class. The whole Java/J2EE application shares the same object of singleton class.

What are the uses of singleton classes?

1. When you need your application to share some common data.

To meet the above requirement,you can make a singleton class which holds your data in its member variables.  You can access this singleton class anywhere from your application and hence you can achieve the common data sharing for your application.

2. Different threads can share data using a singleton class. All of your threads will be working on the same object in this case (You need to take care of thread safety in this case)

3. You can have a singleton class which holds your application configuration. Now any part of your application can access this configuration by accessing this singleton class.

How to make your class singleton

1. Define a class, its member variables and methods as you do normally.

2. Just make the constructor of this class as “private”. Once you do this, objects of this class cannot be created from anywhere outside the scope of this class.

3. Define a static reference variable to store the reference to your singleton object.

4. Define a static method in your singleton class.

(a) If object of our singleton class is not at all created, this method will create a new object, update the reference variable mentioned above (point 3), and return the created object.

(b) If an object of our singleton class is already created (if the reference variable mentioned above (point 3) is already pointing to a singleton object), this method will return the already created object.

Example

Let’s take an example. Assume we have a class “ApplicationConfiguration” which has a single variable applicationRootFolder. In this example, this class is made as a singleton class.

Code

/*
 * FileName : ApplicationConfiguration.java
 */

public class ApplicationConfiguration
{
	/*properties of your singleton class. You can define the variables
	 * as per your business requirements.
	 */
	private String applicationRootFolder ;
	/*This is important. You need to define a static reference variable
	 * to store the reference to your singleton object
	 */

	private static ApplicationConfiguration appConfig ;

	/*Getter and setter methods for the properties you have (as per your
	 * business requirements
	 */
	public String getApplicationRootFolder()
	{
		return applicationRootFolder ;
	}

	public void setApplicationRootFolder(String applicationRootFolder)
	{
		this . applicationRootFolder = applicationRootFolder;
	} 		 /*Define the private constructor
	*
	*This is one important point, make the constructor as private. Once
	*you define this private constructor, the normal implicit default
	*public constructor will not be available. Since the only available
	*constructor is private, an object of this class cannot be created
	*from outside this class.
	*/
	private ApplicationConfiguration()
	{
	}

	/*Define a method which returns the single object of this class.
	 *
	 * First time when this method is called, reference variable
	 *"appConfig"   will be null. So it will create the object of our
	 *singleton class "ApplicationConfiguration" and return.
	 *
	 * From next time onwards, whenever this method is called,
	 * the already created object of ApplicationConfiguration"
	 * will be returned.
	 *
	 * In this way, we can make sure only one instance (object) of
	 * our class can be created.
	 */

	public static ApplicationConfiguration getApplicationConfiguration()
	{
		if ( appConfig == null )
		{
			appConfig = new ApplicationConfiguration();
		}
		return appConfig ;
	}
}

The above given class “ApplicationConfiguration” is a singleton class and only one object can be created from this.Please find the code below which explains how you can access your singleton class.

 /*
 * File Name : SingletonDemo.java
 */
public class SingletonDemo
{
	public static void main(String  args[])
	{
    	//Getting my singleton object
       	ApplicationConfiguration  appConfig = ApplicationConfiguration.getApplicationConfiguration ();

       	/*This is the only way to get the object of
       	*ApplicationConfiguration (Our singleton  class).
      	*
       	*Even if we try to create a object as
       	*ApplicationConfiguration  applicationConfiguration = new
       	*ApplicationConfiguration();
       	*it will fail as only available constructor is private.
       	*/

       //Setting some properties for our singleton class
       appConfig.setApplicationRootFolder("test");

	/*Now any object of your application can access the  singleton
       	*class and its properties as shown below
       	*/
       	ApplicationConfiguration  appConfigRefere =  ApplicationConfiguration.getApplicationConfiguration ();
       	System.out.println(appConfigRefere.getApplicationRootFolder());

       }
  }
 
 
 
 
Comments(0) Sign in (optional)
 
Add a new comment...  (Use Discussion Board for posting new aptitude questions.)

Name:
Email: (optional)
10 + 7 = (please answer the simple math question)

Post Your Comment
X  
View & Edit Profile Sign out
X
Sign in
Google
Facebook
Twitter
Yahoo
LinkedIn
X