0% found this document useful (0 votes)
8 views4 pages

Singleton - DP

Uploaded by

fztzg8c9ty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Singleton - DP

Uploaded by

fztzg8c9ty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Singleton - DP

Thursday, 16 December 2021 10:46 PM

Singleton class is a class that controls the object creation. It means the singleton
class allows us to create a single object of the class, at a time. It is usually used to
control access to resources, such as database connections or sockets. It ensures
that only one connection is made and a thread can access the connection at a time.
Nevertheless, if we try to create the second object of the singleton class, it points
to the first instance of the singleton class.

Ensure that only one instance of the class exists.


Provide global access to that instance by:
1. Declaring all constructors of the class to be private.
2. Providing a static method that returns a reference to the instance. The lazy
initialization concept is used to write the static methods.
3. The instance is stored as a private static variable.

The main difference between these two classes is an instantiation. To create an


instance of a normal class, we use a constructor. On the other hand, to create an
instance of a singleton class, we use getInstance() method.

How to break a singleton -

1. Break by cloning :
java.lang.Cloneable interface then invoking clone() method on its single instance
creates a duplicate object.
2. Deserialization : When a class is deserialized, a fresh instance of the class is
created and its instance variables are then set to the values which were serialized.
3. Reflection API :using java reflection api, we can tweak into a class by getting details
like its fields, constructor, invoking its methods etc. Reflection can also be used to
create new instance of a class. Obviously, if the class is Singleton, then creating a
new instance again breaks its Singleton nature.

class Singleton ClassExample


{
// static variable s of type Singleton
private static Singleton s = null;
// variable of type String
public String str;
//private constructor of the Singleton class that restricted to this class itself
{
// static variable s of type Singleton
private static Singleton s = null;
// variable of type String
public String str;
//private constructor of the Singleton class that restricted to this class itself

private Singleton()
{
str = "it is an example of singleton class.";
}
//static method to create an instance of the Singleton class
// we can also create a method with the same name as the class name
public static Singleton getInstance()
{
//lazy initialization
if (s== null)
s = new SingletonClassExample();
return s;
}
}

You might also like