What is singleton class?

Answer Posted / venu

The Singleton is a useful Design Pattern for allowing only
one instance of your class, but common mistakes can
inadvertently allow more than one instance to be created. In
this article, I'll show you how that can happen and how to
avoid it.

The Singleton's purpose is to control object creation,
limiting the number to one but allowing the flexibility to
create more objects if the situation changes. Since there is
only one Singleton instance, any instance fields of a
Singleton will occur only once per class, just like static
fields.

Singletons often control access to resources such as
database connections or sockets. For example, if you have a
license for only one connection for your database or your
JDBC driver has trouble with multithreading, the Singleton
makes sure that only one connection is made or that only one
thread can access the connection at a time. If you add
database connections or use a JDBC driver that allows
multithreading, the Singleton can be easily adjusted to
allow more connections.

Moreover, Singletons can be stateful; in this case, their
role is to serve as a unique repository of state. If you are
implementing a counter that needs to give out sequential and
unique numbers (such as the machine that gives out numbers
in the deli), the counter needs to be globally unique. The
Singleton can hold the number and synchronize access; if
later you want to hold counters in a database for
persistence, you can change the private implementation of
the Singleton without changing the interface.

On the other hand, Singletons can also be stateless,
providing utility functions that need no more information
than their parameters. In that case, there is no need to
instantiate multiple objects that have no reason for their
existence, and so a Singleton is appropriate.

The Singleton should not be seen as way to implement global
variables in the Java programming language; rather, along
the lines of the factory design patterns, the Singleton lets
you encapsulate and control the creation process by making
sure that certain prerequisites are fulfilled or by creating
the object lazily on demand.

However, in certain situations, two or more Singletons can
mysteriously materialize, disrupting the very guarantees
that the Singleton is meant to provide. For example, if your
Singleton Frame is meant as a global user interface for your
application and two are created, your application will have
two Frames on the screen -- quite confusing for the user.
Further, if two counters are created where one was intended,
then clients requesting numbers will not get the desired
sequence 1, 2, 3... but rather a multiple sequence such as
1, 1, 2, 2, 3, 3, 3.... Additionally, if several instances
of a database-connection Singleton are created, you might
start receiving SQLExceptions complaining about "too many
database connections."

In this article, I'll describe those phenomena and how to
avoid them. After discussing how to implement the Singleton,
I'll go over the sometimes surprising causes for the
phenomena one by one, showing you how they occur and how you
can avoid making those mistakes. I hope that in the process
you will learn about classloading, multithreading,
distributed systems, Design Patterns, and other interesting
topics, as I did.

Is This Answer Correct ?    8 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is difference between call by value and call by reference?

493


What are the new features in java 8?

556


What is runtime locatable code?

851


How does regex work?

515


List some important features of java 10 release?

505






Is a string literal?

512


Hi i am creating desktop application in that i want calling to mobile number. i have java telephone api (JTAPI) but i dont understand how it configure & use plese help me

1354


When can we say that threads are not lightweight process in java?

586


Can we create our own daemon thread?

528


What is java in detail?

550


How many techniques can be employed to create a string object?

547


Explain about java sdk?

574


What is difference between float and double?

492


What is java lang object?

521


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

517