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

August 2014

S M T W T F S
     12
3456789
10111213141516
1718192021 22 23
24252627282930
31      

Recent Entries

Recent Comments

Search My Blog

1 user(s) viewing

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