I am developing a design pattern, and I want to make sure that here is just one instance of a class in Java Virtual Machine, to funnel all requests for some resource through a single point, but I don't know if it is possible.
I can only think of a way to count instances of a class and destroy all instance after first is created.
Is this a right approach? If not, is there any other way?
In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time. After the first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created.
The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type. It returns either true or false.
The best way of accomplishing this is using a named mutex. Create the mutex using code such as: bool firstInstance; Mutex mutex = new Mutex(false, "Local\\" + someUniqueName, out firstInstance); // If firstInstance is now true, we're the first instance of the application; // otherwise another instance is running.
Use the singleton pattern. The easiest implementation consists of a private constructor
and a field
to hold its result, and a static
accessor method with a name like getInstance()
.
The private field can be assigned from within a static initializer block or, more simply, using an initializer. The getInstance()
method (which must be public) then simply returns this instance,
public class Singleton { private static Singleton instance; /** * A private Constructor prevents any other class from * instantiating. */ private Singleton() { // nothing to do this time } /** * The Static initializer constructs the instance at class * loading time; this is to simulate a more involved * construction process (it it were really simple, you'd just * use an initializer) */ static { instance = new Singleton(); } /** Static 'instance' method */ public static Singleton getInstance() { return instance; } // other methods protected by singleton-ness would be here... /** A simple demo method */ public String demoMethod() { return "demo"; } }
Note that the method of using “lazy evaluation” in the getInstance()
method (which is advocated in Design Patterns), is not necessary in Java because Java already uses “lazy loading.” Your singleton class will probably not get loaded unless its getInstance()
is called, so there is no point in trying to defer the singleton construction until it’s needed by having getInstance()
test the singleton variable for null
and creating the singleton there.
Using this class is equally simple: simply get and retain the reference, and invoke methods on it:
public class SingletonDemo { public static void main(String[] args) { Singleton tmp = Singleton.getInstance(); tmp.demoMethod(); } }
Some commentators believe that a singleton should also provide a public final clone()
method that just throws an exception, to avoid subclasses that “cheat” and clone()
the singleton. However, it is clear that a class with only a private constructor cannot be subclassed, so this paranoia does not appear to be necessary.
That's the well known Singleton pattern: you can implement this as follows:
public class SingletonClass { //this field contains the single instance every initialized. private static final instance = new SingletonClass(); //constructor *must* be private, otherwise other classes can make an instance as well private SingletonClass () { //initialize } //this is the method to obtain the single instance public static SingletonClass getInstance () { return instance; } }
You then call for the instance (like you would constructing a non-singleton) with:
SingletonClass.getInstance();
But in literature, a Singleton is in general considered to be a bad design idea. Of course this always somewhat depends on the situation, but most programmers advice against it. Only saying it, don't shoot on the messenger...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With