Java Design Pattern – Singleton Pattern

“In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This concept is also sometimes generalized to restrict the instance to more than one object, as for example, we can restrict the number of instances to five objects. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an anti-pattern by some people, who feel that it is overly used, introducing unnecessary limitations in situations where a sole instance of a class is not actually required.” [Source Wikipedia]

In the following demo, I have tried to explain via code, how Singleton Pattern can be implemented in Java. The comments are self explanatory.

package com.kushal.patternsexample;
/**
 * Main class for testing singleton pattern.
 *
 */
public class SingletonTestMain
{
	public static void main(String args [])
	{
		/**This creates a fresh object*
		 * Constructor will be called.
		 */
		JavaSingletonObject obj=JavaSingletonPattern.getJavaSingletonObject();
		obj.printMe();

		/**
		 * We already have object created.
		 * This method call wont call constructor.
		 */
		obj=JavaSingletonPattern.getJavaSingletonObject();
		obj.printMe();

		/**
		 * Let's destroy the method ourselves.
		 */
		JavaSingletonPattern.nullifyJavaSingletonObject();

		/**
		 * Now, we try to create object again.
		 * This time, construction will be called, as the object is null.
		 */
		obj=JavaSingletonPattern.getJavaSingletonObject();
		obj.printMe();

	}
}

/**
 * This is some kind of factory class, it creates
 * objects of different classes using singleton pattern.
 */
class JavaSingletonPattern {

	private static JavaSingletonObject javaSingletonObject;
	/**
	 * The constructor is private.
	 */
	private JavaSingletonPattern() {
		/* We can have optional code here*/
	}
	public static JavaSingletonObject getJavaSingletonObject() {
		if (javaSingletonObject == null) {
			javaSingletonObject = new JavaSingletonObject();
		}
		return javaSingletonObject;
	}

	/**Just a helper method to nullify the object ourselves.*/
	public static void nullifyJavaSingletonObject()
	{
		javaSingletonObject=null;
	}
}

/**
 * @author kushalp
 * This is a demo class, whose object we want to create
 * using singleton pattern.
 */
class JavaSingletonObject
{

	public JavaSingletonObject()
	{
		System.out.print("Constructor called. ");
	}

	public void printMe()
	{
		System.out.println("I Am JavaSingletonObject.");
	}
}

  1. How to extract plain Text from HTML Website easily in Java
  2. How To Find What Java Version You Are Using?
  3. Computing the total, free and usable disk space easily using JDK 1.6
  4. Creating java.util.date and java.util.sql objects from Calendar object
  5. Bar Code Generation In Java Using iText API
  6. Simple Tag Generator In Java For Music Blogs
  7. Tutorial on Converting an List of Strings or Numbers to a CSV file with optional sorting
  8. Calling URL Browser From Java Application
  9. Java Reflection Tutorial – List Methods Of A Class
  10. Cleaning Punctuation And White Spaces From A String
  11. Java Tutorial – Using JCIFS to copy files to shared network drive using username and password
  12. Prime Number Finder In Java
  13. Utility Class For Getting SQL Connection and Closing It
  14. Java Design Pattern – Singleton Pattern
  15. Reading / Writing File in Java and String Manipulation
  16. How to capture a screenshot using Java and save it to a file?
  17. Calculating The Difference Between Two Dates In Terms of Months
  18. Getting The Name Of Currently Logged In User In The System
  19. How To Set And Get System Clipboard Contents In Java
  20. RGB To Hex and Hex To RGB Conversion Using Java and Regular Expressions
  21. Generating Nepali Unicode Sequence For Characters
  22. Java Tool To Compare Two Lists and Spit Out The Differences – Working Example
  23. Replacing non-ASCII characters using Java Regular Expressions
  24. Calculating Folder Size In Java
Tagged , , , , , , , , . Bookmark the permalink.

3 Responses to Calculating Folder Size In Java

  1. Pingback: Sanjaal.com » Latest Updates

  2. Jamie says:

    This approach uses less memory:

    public static class SizeCounter implements FileFilter
    {
    private long total = 0;
    public SizeCounter(){};
    public boolean accept(File pathname) {
    if ( pathname.isFile()) {
    total+=pathname.length();
    } else {
    pathname.listFiles(this);
    }
    return false;
    }
    public long getTotal()
    {
    return total;
    }
    }

    private static long getFileOrDirectorySize(File file) {
    SizeCounter counter = new SizeCounter();
    file.listFiles(counter);
    return counter.getTotal();
    }

  3. kushalzone says:

    Thank you Jamie for your optimized solution.

Leave a Reply

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