How to Break Singleton Design pattern in Java || Java Interview Questions and Answers | Code Decode

preview_player
Показать описание
In this video of How to Break Singleton Design pattern of Java Interview Questions and Answers
from Code Decode we will demonstrate how to break Singleton Design Pattern.

Udemy Course of Code Decode on Microservice k8s AWS CICD link:

Course Description Video :
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Promotional Links
Coupon Code: CODE10

---------------------------------------------------------------------------------------------------------------------------------------------------------------

Reflection: Reflection can be caused to destroy singleton property of singleton class

After running this class, you will see that hashCodes are different that means, 2 objects of same class are created and singleton pattern has been destroyed.

Serialization:- Serialization can also cause breakage of singleton property of singleton classes. Serialization is used to convert an object of byte stream and save in a file or send over a network. Suppose you serialize an object of a singleton class. Then if you de-serialize that object it will create a new instance and hence break the singleton pattern.

As you can see, hashCode of both instances is different, hence there are 2 objects of a singleton class. Thus, the class is no more singleton.

Above both hashcodes are same hence no other instance is created.

Cloning: Cloning is a concept to create duplicate objects. Using clone we can create copy of object. Suppose, we create clone of a singleton object, then it wil create a copy that is there are two instances of a singleton class, hence the class is no more singleton.

Two different hashCode means there are 2 different objects of singleton class.
Code Decode Playlists

Subscriber and Follow Code Decode

#codedecode #breaksingletondesignpattern #javainterviewquestionanswers
Рекомендации по теме
Комментарии
Автор

So finally by posting this video you have proved that you are a human being and not alien or robotic machine... your voice indicates that either your health is not good or you got very tired for that day. Pl. do take very good care of your health first.
And yes needless to say that video is as usual excellent ! up to the mark
Thanks for the video

gokulaher
Автор

A most common question in an interview when it comes to design patterns. Thanks for sharing your knowledge with us 👌👍

ratunawartejas
Автор

I followed your couple of video and it's very helpfull. Currently I m in client location nd don't have any plan to switch and all but knowledge perspective, I m grabbing it. Please continue 👏👏👏

bablookumar
Автор

To avoid clone - In singleton class in clone method return instance.

To avoid reflection - Check in constructor if instance not null then throw exception.

To avoid Deserialize problem - Add method readResolve method and return instance.

To avoid in concurrency problem - add synchronized block while creating instance.

Finally if possible use Enum instead of singleton class.

navnathjadhav
Автор

Well explained. Useful too. Refreshing from regular software development work.

vengateshm
Автор

This question was asked in Infor interview recently.Thanks for sharing

phanimadhuri
Автор

Informative and clear explanation 👍. Although we know about these concepts, very few have patience to explain with such granularity. If possible, please start series on HLD and LLD using design patterns.

anandpurohit
Автор

Thank you so much for the video, please do continue video for this.

jyothsnagangarapu
Автор

I was asked in the inteview that how to break singleton pattern. But before answering this question I was asked why it's required to break singleton pattern. They told me to answer only if you know why to break singleton pattern.

I didn't knew the answer. Later I researched and found that we need to break singleton to update the corrupted instance or change instance properties over runtime.

lalitdashora
Автор

Everytime same hashcode will be returned for same object?

When u run 1st time its 6533.. form next it is 1130...

Please explain

rajyalakshmi
Автор

Thanks for sharing, Its really helpful. Please make the video on breaking singleton using thread executor service.

rajashrirachha
Автор

Hey, I was grilled on this in my interviews and could not answer. Thanks for sharing this. Really appreciate your work. I have already subscribed this gem channel. Can you share some insights on how do you approach learning any concept/technology in depth and may be selecting ideal resources for them?

AyushGupta-ibqk
Автор

Nice video. Can you please continue Microservices series and complete it ? Tha k you for all the videos

karthikeyanrm
Автор

Hi mam great explanation, can you please share a video on how this can be avoided in singleton

niteeshchandanshire
Автор

Thank you for the video and take care of your health too. your voice seems to be not well. Take care.

nagalakshmikesavan
Автор

Very explained, Thank you very much and please cover other approaches of breaking the singleton like thread executive services as you said

rakeshk
Автор

very helpful content....Please make video on executor framework

sumedhkulkarni
Автор

Can you share some examples where we used singleton in projects. Ot would be helpful

_ravi_kumar_gupta
Автор

Your videos are helpful, helped me to crack a java interview. Thank you and please continue making them. I would also like to follow you on LinkedIn (if you are present).

One request, can you make a detailed video on the builder design pattern.

twit_reactions
Автор

Awesome.... but can you also explain the reason of other methods???Like using reflection we are setting accessibility to true but what about others???
I know for serialization....
when you serialize Objects they will only serialize non static variables that belongs to instance of class and set static member variable as null....So getInstance Method would check and allow for another creation of instance....
Correct me if wrong and also please share the reasons for other methods and new ways
Thanks

husnainajmal
welcome to shbcf.ru