Singleton & Properties
Description : Every complex object oriented program contains at least one Singleton object. It's given. It's a simple, yet complex way to access a single object that can control methods that can be accessed from any program. For example, you might have a verbose variable that can be turned on and off from a single change in a class that can be globally accessed by all program. This allows a easy way to debug your code. It also will enhance your application speed as you turn off unnecessary data. Simple as that. I created two supporting java files and a properties file to show you an example. Check it.

Supporting files : hold down shift and click on the link to download the files.

  1. JwSingleton.java
  2. ReadProperties.java
  3. test.properties

Code Explaination

Let's go over parts of the code to let you understand what's going on.

1. JwSingleton.java

private JwSingleton(){} 
/* The most import part is to cut off the main access to the singleton object's constructor. Why? Because one would never want to risk others to call the Singleton objects constructor to create a new object of itself. */
public static JwSingleton getInstance(){
		if (jwSing==null){
			jwSing = new JwSingleton();
			COUNT++;
		}
		//always returns COUNT=1
		System.out.println(" :: Number of JwSingleton Instance is >> "+ COUNT);
		return jwSing;
	}
/* The only way the object can be accessed is through getInstance() method. This method makes sure that you can only create one instance of JwSingleton object. The value jwSing keeps the first instance and returns it to the requesting object everytime. */

1. ReadProperties.java

jwSingMe = JwSingleton.getInstance();	
/* Notice how we created JwSingleton object. We never call new because we have no access to private constructor and it won't get you anywhere. the getInstance() in the singleton class takes care of the actual creation of the object. That's all for Singleton example. */

	if(jwProp == null){
		try{
	 		jwProp = new Properties();
			jwProp.load(getClass().getResourceAsStream("test.properties"));
		}catch(IOException e){
		   	System.out.println("   ## ERROR [ Failed to Read test ]" + e.getMessage());

		}catch(NullPointerException n){
			System.out.println("   ## ERROR [ Failed to Read Properties File ]" + n.getMessage());	
		}				
	}	

/* Here's a interesting one for you. How to read a property file? Take a look at getClass().getResourceAsStream("test.properties"). This takes care of actual path information. As long as the test.properties is sitting in the same directory, this call will find the exact location and load up the property file specified. */

if(jwProp.getProperty("verbose").trim().equals("false")){
		verbose=false;
		System.out.println(" :: Verbose Mode is Set to FALSE");
	}
/* Once jwProp is set you can get any variables set in the properties file by calling getProperty("Variable"). Pretty straight forward. Please take a look at the test.properties to see what it returns. */


close this window to go back to the main menu