Subscribe

RSS Feed (xml)

Powered By

Skin Design:
Free Blogger Skins

Powered by Blogger

Wednesday, August 5, 2009

Example of Singleton

Hi friends,

Today I would write a note on an another example of singleton pattern as follows

Logic:

Defines an Instance operation that lets clients access
its unique instance. Instance is a class operation
(that is, a class method in Smalltalk and a static member
function is C++). May be responsible for creating its own
instance.

Code:

public class Singleton
{
private static Singleton instance = null;
public static Singleton getSingleton()
{
if( instance == null )
{
instance = new Singleton();
}
return instance;
}
}

public class Test
{
public static void main( String arg[] )
{
try
{
Singleton instance = Singleton.getSingleton();
}
catch( Exception e )
{
e.printStackTrace();
}
}
}

Hope this was useful to you. Please give me your valuable suggestions.

No comments: