Singleton Class in Object-Oriented Programming

 
Singleton Class
 
1
 
Singleton Class
 
In object-oriented programming, a singleton class
is a class that can have only one object (an
instance of the class) at a time. After the first time,
if we try to instantiate the Singleton class, the new
variable also points to the first instance created.
So whatever modifications we do to any variable
inside the class through any instance, affects the
variable of the single instance created and is
visible if we access that variable through any
variable of that class type defined.
 
P
u
r
p
o
s
e
 
o
f
 
S
i
n
g
l
e
t
o
n
 
C
l
a
s
s
 
The primary purpose of a Single class is to restrict the
limit of the number of object creation to only one. This
often ensures that there is access control to resources,
for example, socket or database connection.
The memory space wastage does not occur with the use
of the singleton class because it restricts the instance
creation. As the object creation will take place only once
instead of creating it each time a new request is made.
We can use this single object repeatedly as per the
requirements. This is the reason why the multi-threaded
and database applications mostly make use of the
Singleton pattern in Java for caching, logging, thread
pooling, configuration settings, and much more.
 
H
o
w
 
t
o
 
D
e
s
i
g
n
/
C
r
e
a
t
e
 
a
 
S
i
n
g
l
e
t
o
n
C
l
a
s
s
 
i
n
 
J
a
v
a
?
 
To create a singleton class, we must follow the
steps, given below:
1
.
 
E
n
s
u
r
e
 
t
h
a
t
 
o
n
l
y
 
o
n
e
 
i
n
s
t
a
n
c
e
 
o
f
 
t
h
e
 
c
l
a
s
s
 
e
x
i
s
t
s
.
2
.
 
P
r
o
v
i
d
e
 
g
l
o
b
a
l
 
a
c
c
e
s
s
 
t
o
 
t
h
a
t
 
i
n
s
t
a
n
c
e
 
b
y
Declaring all constructors of the class to be private.
Providing a static method that returns a reference
to the instance. The lazy initialization concept is
used to write the static methods.
The instance is stored as a private static variable.
 
H
o
w
 
t
o
 
d
e
s
i
g
n
 
a
 
s
i
n
g
l
e
t
o
n
 
c
l
a
s
s
?
 
Firstly, declare the constructor of the Singleton
class with the private keyword. We declare it as
private so that no other classes can instantiate
or make objects from it.
A private static variable of the same class that
is the only instance of the class.
Declare a static factory method with the return
type as an object of this singleton class.
 
N
o
r
m
a
l
 
c
l
a
s
s
 
V
s
 
S
i
n
g
l
e
t
o
n
 
c
l
a
s
s
 
We can distinguish a Singleton class from the
usual classes with respect to the process of
instantiating the object of the class. To
instantiate a normal class, we use a java
constructor. On the other hand, to instantiate
a singleton class, we use the getInstance()
method.
The other difference is that a normal class
vanishes at the end of the lifecycle of the
application while the singleton class does not
destroy with the completion of an application.
 
F
o
r
m
s
 
o
f
 
S
i
n
g
l
e
t
o
n
 
D
e
s
i
g
n
 
p
a
t
t
e
r
n
 
E
a
r
l
y
(
E
a
g
e
r
)
 
I
n
s
t
a
n
t
i
a
t
i
o
n
:
 
T
h
e
 
o
b
j
e
c
t
 
c
r
e
a
t
i
o
n
t
a
k
e
s
 
p
l
a
c
e
 
a
t
 
t
h
e
 
l
o
a
d
 
t
i
m
e
.
L
a
z
y
 
I
n
s
t
a
n
t
i
a
t
i
o
n
:
 
T
h
e
 
o
b
j
e
c
t
 
c
r
e
a
t
i
o
n
 
i
s
 
d
o
n
e
a
c
c
o
r
d
i
n
g
 
t
o
 
t
h
e
 
r
e
q
u
i
r
e
m
e
n
t
.
 
R
e
f
e
r
e
n
c
e
 
V
a
r
i
a
b
l
e
 
g
e
t
I
n
s
t
a
n
c
e
(
)
 
I
n
 
a
 
s
i
n
g
l
e
t
o
n
 
c
l
a
s
s
,
 
w
h
e
n
 
w
e
 
f
i
r
s
t
-
t
i
m
e
 
c
a
l
l
 
t
h
e
g
e
t
I
n
s
t
a
n
c
e
(
)
 
m
e
t
h
o
d
,
 
i
t
 
c
r
e
a
t
e
s
 
a
n
 
o
b
j
e
c
t
 
o
f
t
h
e
 
c
l
a
s
s
 
w
i
t
h
 
t
h
e
 
n
a
m
e
 
s
i
n
g
l
e
_
i
n
s
t
a
n
c
e
a
n
d
 
r
e
t
u
r
n
s
 
i
t
 
t
o
 
t
h
e
 
v
a
r
i
a
b
l
e
.
 
S
i
n
c
e
s
i
n
g
l
e
_
i
n
s
t
a
n
c
e
 
i
s
 
s
t
a
t
i
c
,
 
i
t
 
i
s
 
c
h
a
n
g
e
d
 
f
r
o
m
 
n
u
l
l
t
o
 
s
o
m
e
 
o
b
j
e
c
t
.
 
Example
 
class database {
   private static database dbobject;
   private database() { }
   public static database getInstance() {
   // create object if it is not created
    if (dbobject==null) {
       dbobject =new database();
      }
  return dbobject;
}
 
10
 
Example
 
public void  getconnection() {
   System.out.println(“You are now connected to database”);
   }
}
Class Main  {
         public static void main(String args[])
         {
              database db1;
              db1= database.getInstance();
              db1.getconnection();
         }
}
 
11
Slide Note
Embed
Share

Exploring the concept of Singleton class, which allows only one instance of a class to be created at a time. Learn the purpose, design, and distinction of Singleton classes from normal classes in Java. Discover how Singleton classes help in resource management and common application scenarios.

  • Singleton Class
  • Object-Oriented Programming
  • Java
  • Design Pattern
  • Resource Management

Uploaded on Jul 26, 2024 | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Amity School of Engineering & Technology Singleton Class 1

  2. Amity School of Engineering & Technology Singleton Class In object-oriented programming, a singleton class is a class that can have only one object (an instance of the class) at a time. After the first time, if we try to instantiate the Singleton class, the new variable also points to the first instance created. So whatever modifications we do to any variable inside the class through any instance, affects the variable of the single instance created and is visible if we access that variable through any variable of that class type defined.

  3. Amity School of Engineering & Technology Purpose of Singleton Class The primary purpose of a Single class is to restrict the limit of the number of object creation to only one. This often ensures that there is access control to resources, for example, socket or database connection. The memory space wastage does not occur with the use of the singleton class because it restricts the instance creation. As the object creation will take place only once instead of creating it each time a new request is made. We can use this single object repeatedly as per the requirements. This is the reason why the multi-threaded and database applications mostly make use of the Singleton pattern in Java for caching, logging, thread pooling, configuration settings, and much more.

  4. Amity School of Engineering & Technology How to Design/Create a Singleton Class in Java? To create a singleton class, we must follow the steps, given below: 1. Ensure that only one instance of the class exists. 2. Provide global access to that instance by Declaring all constructors of the class to be private. Providing a static method that returns a reference to the instance. The lazy initialization concept is used to write the static methods. The instance is stored as a private static variable.

  5. Amity School of Engineering & Technology How to design a singleton class? Firstly, declare the constructor of the Singleton class with the private keyword. We declare it as private so that no other classes can instantiate or make objects from it. A private static variable of the same class that is the only instance of the class. Declare a static factory method with the return type as an object of this singleton class.

  6. Amity School of Engineering & Technology Normal class Vs Singleton class We can distinguish a Singleton class from the usual classes with respect to the process of instantiating the object of the class. To instantiate a normal class, we use a java constructor. On the other hand, to instantiate a singleton class, we use the getInstance() method. The other difference is that a normal class vanishes at the end of the lifecycle of the application while the singleton class does not destroy with the completion of an application.

  7. Amity School of Engineering & Technology Forms of Singleton Design pattern Early(Eager) Instantiation: The object creation takes place at the load time. Lazy Instantiation: The object creation is done according to the requirement.

  8. Amity School of Engineering & Technology Reference Variable

  9. Amity School of Engineering & Technology getInstance() In a singleton class, when we first-time call the getInstance() method, it creates an object of the class with the name single_instance and returns it to the variable. Since single_instance is static, it is changed from null to some object.

  10. Amity School of Engineering & Technology Example class database { private static database dbobject; private database() { } public static database getInstance() { // create object if it is not created if (dbobject==null) { dbobject =new database(); } return dbobject; } 10

  11. Amity School of Engineering & Technology Example public void getconnection() { System.out.println( You are now connected to database ); } } Class Main { public static void main(String args[]) { database db1; db1= database.getInstance(); db1.getconnection(); } } 11

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#