Quick start Singleton – Walk through

This being my first existence on the network and I just want to make sure that I would come back to this blog page again sometime and keep on writing. For now this is a quick 5 min walk through of getting your hands dirty on the Singleton Pattern. As any ones first pattern Singleton always seems to be the easiest to adapt and ironically always the mistress of your pains; when you realize the act wasn’t right in the first place.
More details on that later.

This post should help you to get your hands right on the Singleton Pattern and find the kind there off.

Like any other pattern Singleton also has an objective behind it. What is that?

Motivation:

A Singleton ensures that a class has only one instance, and provides a global point of access to that class.

Benefits
The very simple benefits of a singleton can be:

* Controlled access
* Permits a variable number of instances
* Reduced name space

When to use:
There must be exactly once instance of a class

How to use: Walk Through

1. Create a class

  1. public class SimplySingleton{}

2. Declare a member variable. This variable will be used for keeping the singleton instance.

It has to be private so that it is not accessible from anywhere else. It has to be static so that it holds only one instance in all entirety.

  1. private static SimplySingleton simplySingleton = null;

3. Declare a private constructor.

Creating a private constructor would mean no one else can instantiate this class.

  1. private SimplySingleton(){}

4. So now everything seems private how do we access it. Create a global access point.

  1. public static SimplySingleton getInstance(){}

How would I access it from outside SimplySingleton.getInstance();

This method should return a SimplySingleton instance.

So here comes the logic to create the one and only instance.

  1. // 4a. is the variable null?
  2. if(simplySingleton != null)
  3. return simplySingleton;
  4. // 4b. if not assign it an instance.
  5. else return simplySingleton = new SimplySingleton();

Following is the complete code listing for writing a Singleton.

  1. // Declaring the class
  2. public class SimplySingleton {
  3.  
  4. // 1. a private and a static member variable
  5.  
  6. private static SimplySingleton simplySingleton = null;
  7.  
  8. // 2. a private constructor
  9.  
  10. private SimplySingleton(){}
  11.  
  12. // 3. a global access point
  13.  
  14. public static SimplySingleton getInstance(){
  15. // 4a. is the variable null?
  16. if(simplySingleton != null)
  17. return simplySingleton;
  18. // 4b. if not assign it an instance.
  19. else return simplySingleton = new SimplySingleton();
  20. }
  21.  
  22. }

Following are some good resources for in depth peek into the Singleton Pattern.

More Resources:

http://en.wikipedia.org/wiki/Singleton_pattern

http://radio.weblogs.com/0122027/stories/2003/10/20/implementingTheSingletonPatternInJava.html

http://www.oodesign.com/singleton-pattern.html

http://www.oaklib.org/docs/oak/singleton.html

2 thoughts on “Quick start Singleton – Walk through

Leave a Reply

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