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


Question 66: What are the different ways in which a



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

Question 66: What are the different ways in which a
Singleton Design pattern can break and how to
prevent that from happening?
Answer: There are 3 ways which can break Singleton property of a
class, they are discussed below with examples:
Reflection : Reflection API in java is used to change the runtime
behavior of a class. Hibernate, Spring’s Dependency injection also
uses Reflection. So, even though in the above singleton
implementations, we have defined the constructor as private, but
using Reflection, even private constructor can be accessed, so
Reflection can be used to break the singleton property of a class.
A.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
66\Reflection\Break Singleton\color1.png


Notice, I am using public access modifier with the only instance of
singleton class so that it can be accessed outside this class using
just the class name.
Now, let’s see how Reflection can break Singleton:
Test.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
66\Reflection\Break Singleton\color2.png


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
66\Reflection\Break Singleton\color3.png


Output:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
66\Reflection\Break Singleton\output.png


As we can see from the output, the 2 instances have different
hashcode, thus destroying Singleton.
Now to prevent Singleton from Reflection, one simple solution is to
throw an exception from the private constructor, so when Reflection
tries to invoke private constructor, there will be an error.


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
66\Reflection\Prevent Singleton_Solution 1\color.png


Now, if you run Test.java , you will get below output:



The other solution to prevent Singleton from Reflection is using
Enums , as its constructor cannot be accessed via Reflection, JVM
internally handles the creation and invocation of enum constructor
SingletonEnum.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
66\Reflection\Prevent Singleton_Solution 2\color1.png


TestSingletonEnum.java:


C:\Users\jjatin\Desktop\Different Versions\All_Photos\Question
66\Reflection\Prevent Singleton_Solution 2\color2.png


Output:
C:\Users\jjatin\Desktop\Different
Versions\All_Photos\Question
66\Reflection\Prevent
Singleton_Solution 2\output.png
Another way which can break Singleton property of a class is:

Download 7,31 Mb.

Do'stlaringiz bilan baham:
1   ...   43   44   45   46   47   48   49   50   ...   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