Grokking The Java Developer Interview: More Than 200 Questions To Crack The Java, Spring, SpringBoot & Hibernate Interview


Question 65: What is Singleton Design Pattern and



Download 7,31 Mb.
Pdf ko'rish
bet44/89
Sana21.04.2022
Hajmi7,31 Mb.
#571320
1   ...   40   41   42   43   44   45   46   47   ...   89
Bog'liq
Grokking-The-Java-Developer-Interview-More-Than-200-Questions-To-Crack-The-Java -Spring -SpringBoot-

Question 65: What is Singleton Design Pattern and
how it can be implemented?
Answer: This is a famous interview question, as it involves many
follow-up questions as well. I will cover all of them here:
What is Singleton Design Pattern?
Singleton design pattern comes under Creational Design Patterns
category and this pattern ensures that only one instance of class
exists in the JVM.
Singleton pattern is used in:
logging, caching, thread pool etc.
other design patterns like Builder, Prototype, Abstract
Factory etc.
core java classes like java.lang.Runtime etc.
How to implement Singleton Pattern
To implement a Singleton pattern, we have different approaches but
all of them have the below common concepts:
private constructor to restrict instantiation of the class
from other classes.
private static variable of the same class that is the only
instance of the class.
public static method that returns the instance of the
class, this is the global access point for outer world to
get the instance of the singleton class.
The approaches to implement singleton pattern are:
Eager Initialization : In eager initialization, the instance of Singleton
Class is created at the time of class loading, this is the easiest
method to create a singleton class but it has a drawback that


instance is created even though client application might not even use
it.
Example:
A.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
65\Eager Initialization\color1.png


Test.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
65\Eager Initialization\color2.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
65\Eager Initialization\output.png


We can also access the instance by using the classname like
A.instance because of static keyword but we will have to change its
access modifier to ‘public’, so that it is visible outside the singleton
class.
Let’s suppose you are creating a large object by using a lot of
resources, there may be a chance that object creation may throw an
exception but the above way of creating a singleton class does not
provide any option for exception handling as you can write try-catch
only inside a block of code. There is a solution to tackle this
Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   40   41   42   43   44   45   46   47   ...   89




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish