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

Why is String immutable?

594


what is meant by Garbage collection?

602


What is a ternary operator in java? What is an interface?

523


Can finally block be used without a catch?

521


Why stringbuilder is not thread safe in java?

541






What is the loop in java?

551


How can we create objects if we make the constructor private ?

517


What is integer valueof?

619


What is the purpose of the finally clause of a try-catch-finally statement in java programming?

484


What methods are used in Servlet?Applet communication?

1633


How do you override a private method in java?

473


What are the differences between path and classpath variables?

466


What's the difference between an abstract class and interface in java?

529


Can we split string with in java?

509


Does java support multiple inheritance or not?

577