Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits
Singleton Print E-mail
User Rating: / 1
PoorBest 
Thursday, 27 January 2005 19:55
Ensure a class only has one instance, and provide a global point of access to it.

Statically Initialized
The singleton class is implemented by defining a static field that is statically initialized (that is, the field is initialized when the class is initialized). This has the advantage that invocations of the method used to access the singleton do not incur the overhead of checking whether or not the instance has been created.

Dynamically Initialized
The singleton class is implemented by defining a static field that is dynamically initialized (that is, the field is initialized the first time its value is requested). This has the advantage that the singleton is not allocated unless it is needed, potentially reducing memory usage.



Links

Source Code

/**
 * Represents a singleton.
 */
public class Singleton {
	/**
	 * Holds the singleton instance.
	 */
	private static Singleton instance;

	/**
	 * constructor must be private to avoid automatic creation of default
	 * constructor by the compiler
	 *  
	 */
	private Singleton() {
		super();
	}

	/**
	 * Returns the singleton instance.
	 * 
	 * synchronized to prevent race problem
	 * 
	 * @return the singleton instance
	 */
	public static synchronized Singleton getInstance() {
		if (instance == null) {
			instance = new Singleton();
		}
		return instance;
	}
}
Singleto

Tags See All Tags Add New Tag...

Please Enter New Tags Separated By Comma's
  Or Close

designpattern  java 
Powered By Joomla Tags

Comments
Add New Search RSS
Write comment
Name:
Email:
 
Title:
UBBCode:
[b] [i] [u] [url] [quote] [code] [img] 
 
:):grin;)8):p:roll:eek:upset:zzz:sigh:?:cry
:(:x
Please input the anti-spam code that you can read in the image.

3.20 Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved."

Last Updated ( Saturday, 18 June 2005 00:08 )
 


Another articles:


Content View Hits : 2926634

Enter Amount: