JAVA How to JAVA Development tutorials and how to

2May/100

JAVA Singleton Pattern

Singleton Pattern - JAVA How to

Hello again, after 3 weeks of posts absense, i'm back 8-) . Back with JAVA design patterns stuff, this time about How to apply Singleton Pattern, basically a pattern that allows you to restrict to instantiate more than one instance of a class in your application. The requirements that you satisfy with Singleton Pattern are:

  • Only one instance of a class per application
  • If you try to instantiate a second object from singleton class, you'll get the original instance

I decided to write about it because i had a requirement in a project where i'm working right now that involves to handle an object to mantain the user state and other data in a single object, without possibility of instantiate another one and access easily to current object, so i decided to implement the pattern. First, you need to restrict the constructor of the class to avoid to instantiate object in the usual way so we have to declare the constructor as private. next you have to declare a method that, if an object isn't created yet, instantiate a new object and returns it, or, if object is already created, return a reference to that object...

To do that you have to mantain the state (object's instantiated or not) and an object's reference in the class. Let's code. Suppose that you have my requirement about the user data, so create a class named UserData like this:

public class UserData
 {
 private String codUsr;
 private String status;
 private UserData userData;

 private UserData() {}

 static UserData getUserData() {
 if(userData == null)
 userData = new UserData();

 return userData;
 }

 public void setCodUsr(String codUsr) {
 this.codUsr = codUsr;
 }

 public void setStatus(String status) {
 this.status = status;
 }

 public void getCodUsr() {
 return this.codUsr;
 }

 public void getStatus() {
 return this.status;
 }
 }

As you can see, we have the basics:

  • Properties
  • Accessors and Mutators following JavaBeans conventions

But, you can see something different:

  • private property the same type as class: Is used to mantain a reference to the single object that is instantiated.
  • private constructor: To avoid external code to instantiate objects in the normal way
  • static method with return type as the class: This method is the heart of singleton class, because if instance isn't created, instantiates a new object and assigns the reference to class property, and finally returns the class field.

This is a simple singleton pattern implementation because could be more complex implementation that involves singleton subclasses or something like that, but this is a good beginning. I also recommend that you read the book of patterns desing of Gang of fours. Enjoy it, and if you want the source code of UserData class, you can download it >>HERE<<.

Share and Enjoy

  • Twitter
  • Facebook
  • Digg
  • del.icio.us
  • Technorati
  • MySpace
  • Sphinn
  • Print
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Identi.ca
  • LinkedIn
  • Meneame
  • Netvibes
  • Yigg
  • Reddit
  • RSS
  • Tumblr
Comments (0) Trackbacks (1)

Leave a comment