Subscribe to Java/SCJP        RSS Feed
-----

Singleton Pattern

Icon 1 Comments
The Singleton pattern is used for classes which should have only one instance in the application. The other classes can't invoke the constructor directly because it is declared private and some public static method is provided which returns the single instance. This pattern makes the instance available globaly and thus should be used carefully.

e.g.
class InstanceOne{
InstanceOne one;
private InstanceOne(){}
public static InstanceOne getInstance(){
	if(one != null) {
		return one;
	} else {
		one = new InstanceOne();
	}
}
}

1 Comments On This Entry

Page 1 of 1

amitbhandari Icon

28 February 2008 - 11:48 PM
I guess, you are missing description of thread-safe singleton pattern here wherein the execution block of constructor should be made critical until either the new object has been created or the old object has been returned.
0
Page 1 of 1

December 2014

S M T W T F S
 123456
78910111213
14151617 18 1920
21222324252627
28293031   

Recent Entries

Recent Comments

Search My Blog

1 user(s) viewing

1 Guests
0 member(s)
0 anonymous member(s)