This link has been bookmarked by 22 people . It was first bookmarked on 15 Jul 2006, by someone privately.
-
21 Sep 12
-
So to prevent direct instantiation, we create a private default constructor, so that other classes can't create a new instance.
-
But unless we add some further code, there'll be absolutely no way to use the class. We want to prevent direct instantiation, but we still need to allow a way to get a reference to an instance of the singleton object.
-
We need to make sure that threads calling the getSingletonObject() method don't cause problems
-
This prevents two threads from calling the getSingletonObject() method at the same time.
-
So, to be absolutely positively 100% certain that a singleton really is a singleton, we must add a
clone()method of our own, and throw aCloneNotSupportedExceptionif anyone dares try! -
public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); // that'll teach 'em }
-
-
17 Sep 12
-
21 May 12
-
05 Nov 11
-
30 Nov 10
-
29 Nov 10
Rinav GA singleton is an object that cannot be instantiated. At first, that might seem counterintuitive - after all, we need an instance of an object before we can use it. Well yes a singleton can be created, but it can't be instantiated by developers - meaning that the singleton class has control over how it is created. The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM) - by prevent direct instantiation we can ensure that developers don't create a second copy.
-
a design pattern is a template for software development.
-
The purpose of the template is to define a particular behavior or technique that can be used as a building block for the construction of software - to solve universal problems that commonly face developers.
-
you're probably wondering what a singleton is
-
A singleton is an object that cannot be instantiated. At first, that might seem counterintuitive - after all, we need an instance of an object before we can use it.
-
but it can't be instantiated by developers
-
The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM) - by prevent direct instantiation we can ensure that developers don't create a second copy.
-
So why would this be useful?
-
prevent others (ourselves included) from making copies of it or creating new instances.
-
For example, a central configuration object that stores setup information should have one and one only instance - a global copy accessible from any part of the application, including any threads that are running.
-
we provide a default constructor that is marked as private.
-
private SingletonObject()
-
So far so good. But unless we add some further code, there'll be absolutely no way to use the class. We want to prevent direct instantiation, but we still need to allow a way to get a reference to an instance of the singleton object.
-
We need to provide an accessor method, that returns an instance of the SingletonObject class but doesn't allow more than one copy to be accessed.
-
SingletonObject
-
static
-
if (ref == null) // it's ok, we can call this constructor ref = new SingletonObject(); return ref;
-
static SingletonObject ref;
-
Preventing thread problems with your singleton
-
synchronized
-
they forget about cloning.
-
SingletonObject clone = (SingletonObject) obj.clone();
-
clone()method -
java.lang.Object
-
By default, the
clone()method is marked as protected, -
we must add a
clone()method of our own, and throw aCloneNotSupportedExceptionif anyone dares try! -
public Object clone() throws CloneNotSupportedException { throw new CloneNotSupportedException(); // that'll teach 'em }
-
-
18 Feb 10
-
06 Aug 09
-
27 Nov 08
-
11 Mar 08
-
13 Nov 07
-
18 Jun 07
-
20 Nov 06
-
02 May 06
Would you like to comment?
Join Diigo for a free account, or sign in if you are already a member.