What is singleton class?

Answer Posted / test

In computer science the singleton design pattern is designed
to restrict instantiation of a class to one (or a few)
objects. This is useful when exactly one object is needed to
coordinate actions across the system. Sometimes it is
generalized to systems that operate more efficiently when
only one or a few objects exist.

The singleton pattern is implemented by creating a class
with a method that creates a new instance of the object if
one does not exist. If one does exist it returns a reference
to the object that already exists. To make sure that the
object cannot be instantiated any other way the constructor
is made either private or protected.

The singleton pattern must be carefully constructed in
multi-threaded applications. If two threads are to execute
the creation method at the same time when a singletondoes
not yet exist they both must check for an instance of the
singleton and then only one should create the new one.

The classic solution to this problem is to use mutual
exclusion on the class that indicates that the object is
being instantiated.

A Java programming language solution is as follows. It is
based on the Q&A link found below modified for
multi-threading however it is still vulnerable to the
double-checked locking anti-pattern also found below:

public class Singleton {
private static Singleton INSTANCE null;

// Private constructor suppresses
// default public constructor
private Singleton() {}

//synchronized creator to defend against multi-threading issues
//another if check here to avoid multiple instantiation
private synchronized static void createInstance() {
if (INSTANCE null) {
INSTANCE new Singleton();
}
}

public static Singleton getInstance() {
if (INSTANCE null) createInstance();
return INSTANCE;
}
}

Is This Answer Correct ?    25 Yes 3 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is there any way to find whether software installed in the system is registered by just providing the .exe file? I have tried the following code but its just displaying the directory structure in the registry. Here the code : package com.msi.intaller; import java.util.Iterator; import ca.beq.util.win32.registry.RegistryKey; import ca.beq.util.win32.registry.RootKey; public class RegistryFinder { public static void main(String... args) throws Exception { RegistryKey.initialize(RegistryFinder.class.getResource("jRe gistryKey.dll").getFile()); RegistryKey key = new RegistryKey(RootKey.HKLM, "Software\\ODBC"); for (Iterator subkeys = key.subkeys(); subkeys.hasNext();) { RegistryKey subkey = subkeys.next(); System.out.println(subkey.getName()); // You need to check here if there's anything which matches "Mozilla FireFox". } } }

1339


What is string builder?

455


What is the use of list in java?

512


Which collection is ordered in java?

522


What is object in java?

521






When do we use synchronized blocks and advantages of using synchronized blocks?

655


How do you check if two given string are anagrams?

543


Name some classes present in java.util.regex package.

596


What is return type in java?

624


Can we have multiple catch block for a try block?

582


Do I need java for windows 10?

538


What are jee technologies?

527


What state does a thread enter when it terminates its processing in java programming?

570


What is the difference between procedural and object-oriented programs?

517


What is JFC?

698