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 are the restrictions imposed by a Security Manager on Applets?.

2071


What does \ mean in regex?

626


Does chrome use java?

523


Which sorting algorithm is best in java?

539


Why do we use threads in java?

571






How thread scheduler schedule the task?

587


What is private public protected in java?

572


What is difference between array and vector?

541


What is the relationship between clipping and repainting under awt?

619


Define how does a try statement determine which catch clause should be used to handle an exception?

588


What is the collections api in java programming?

557


What is the primary benefit of encapsulation?

562


What is a void method?

521


Why pass by reference is not possible in java?

502


Is age discrete or continuous?

692