The Singleton Pattern Explained and Implemented in Java | Creational Design Patterns | Geekific

  Рет қаралды 93,493

Geekific

Geekific

Күн бұрын

Support us on Patreon: / geekific
Discord Community: / discord
GitHub Repository: github.com/geekific-official/
In the first video of this series we start with one of the simplest, yet one of the most used Design Patterns out there: the Singleton Design Pattern.
Timestamps:
00:00 Introduction
00:15 What is the Singleton Pattern?
01:54 Simple Singleton Implementation
03:37 Thread-Safe Singleton Implementation
06:53 Enhanced Singleton Implementation
07:48 Recap
08:18 Thanks for Watching!
If you found this video helpful, check other Geekific uploads:
- Introduction to Design Patterns: • What are Design Patter...
- What is a Thread? (Process, Program, Parallelism, Scheduler Explained): • What is a Thread? | Th...
- The Volatile and Synchronized Keywords in Java: • The Volatile and Synch...
- Object-Oriented Programming Fundamentals: • What is Object-Oriente...
- SOLID Principles and Best Practices: • SOLID Design Principle...
#Geekific #DesignPatterns #SingletonPattern #CreationalPatterns

Пікірлер: 78
@Salehalanazi-7
@Salehalanazi-7 2 жыл бұрын
Damn man. That's a unique teaching ability to explain every reason. Nice.
@geekific
@geekific 2 жыл бұрын
Thanks a lot :) Glad it helped!
@snarf45
@snarf45 Жыл бұрын
My left ear loved this
@geekific
@geekific Жыл бұрын
😂 Check our newest videos, we fixed that!
@Nitionful
@Nitionful Жыл бұрын
Very well constructed, step by step explanation. Thank you!
@jeromebranchetti6062
@jeromebranchetti6062 4 ай бұрын
Incredibly clear man THANKS
@user-fl1gt9bu9x
@user-fl1gt9bu9x Жыл бұрын
Awesome.....Haven't seen this indepth singleton implementation, mostly people just do a first level of implementation without thinking of the multithreaded environment.
@cool_huip_
@cool_huip_ Жыл бұрын
Hey man, your videos are super awesome. Thank you so much for the good work
@EpicGamer-ux1tu
@EpicGamer-ux1tu 2 ай бұрын
Thanks for the video, it was really useful.
@ajeethkumarkandasamy5305
@ajeethkumarkandasamy5305 Жыл бұрын
Thanks for clear explanation!
@oumaymaaouinti6016
@oumaymaaouinti6016 10 ай бұрын
Super clear 👏 Thank you !
@StratOnFire
@StratOnFire 7 ай бұрын
Thank you so much for making this video. Helped a lot!
@dragosudrea6533
@dragosudrea6533 Жыл бұрын
Nicely explained. Thank you!
@grzegorzmolin
@grzegorzmolin Жыл бұрын
Your videos are so so so good!! Thank You!!
@Sarah-zo2ys
@Sarah-zo2ys 2 жыл бұрын
Thank you so much for a well coordinated video!! Kudos to you
@geekific
@geekific 2 жыл бұрын
My pleasure :) Glad you liked it!
@SyncEdgeHandle
@SyncEdgeHandle 5 ай бұрын
The only thing I have to say is 'Perfectly Awesome..........'. Thank you!
@azizbekkhushvakov2407
@azizbekkhushvakov2407 2 жыл бұрын
Great explaining skills, I read many documentation but I completely understood from your video
@geekific
@geekific 2 жыл бұрын
Awesome, thank you! Glad it was helpful :)
@ukaszkiepas57
@ukaszkiepas57 Жыл бұрын
That was giga clear. Tanks buddy
@artyug4235
@artyug4235 Жыл бұрын
thank you so much ,very clear
@--36--
@--36-- 2 жыл бұрын
Good channel. I started reading Head First Design Pattern, but I think I'm gonna drop it and just learn from you.
@geekific
@geekific 2 жыл бұрын
Checking multiple references is not a bad idea, that's what I did when I try to put a video together! I am really glad you found our videos helpful :)
@--36--
@--36-- 2 жыл бұрын
@@geekific Gonna watch them all.
@savelokhosa6846
@savelokhosa6846 Жыл бұрын
That was a great explanation
@tanzir3678
@tanzir3678 Жыл бұрын
overwhelmed
@Ran_cnc_3d
@Ran_cnc_3d Жыл бұрын
Thank you!
@christiansailor2880
@christiansailor2880 Жыл бұрын
Excellent
@pointonous2611
@pointonous2611 4 ай бұрын
the best i've seen
@sovngarden
@sovngarden 21 күн бұрын
Absolutely love the video although the last third of the video took 90% of my brainpower to keep up
@hassansakr4774
@hassansakr4774 Жыл бұрын
You are amazing bro, you nailed it, I will definitely subscribe to such an amazing and valuable content channel, wish you growth and wealth :)
@geekific
@geekific Жыл бұрын
I appreciate that! Thanks for the support :)
@mikelevitskiy2535
@mikelevitskiy2535 2 жыл бұрын
amazing!
@geekific
@geekific 2 жыл бұрын
Thank you :)
@lkke7604
@lkke7604 2 жыл бұрын
sick explanation and video
@geekific
@geekific 2 жыл бұрын
Thanks! Glad you liked it :)
@murad357
@murad357 Жыл бұрын
Not sure why you don't have much more views for this video. I don't think anyone explained the multi thread issue in any of the singleton pattern video. Great job and thank you.
@eugenebespalov9471
@eugenebespalov9471 5 ай бұрын
Cool video! It would be dope if you could also explain an alternative approach of making a singleton using enums please. Oh, and numbering the videos would be really beginner-friendly and much appreciated by the viewers!
@geekific
@geekific 5 ай бұрын
Will add it to my list of upcoming videos! Stay Tuned!
@greglang5748
@greglang5748 8 ай бұрын
Great video. But how to we actually use it now? Should there also be a setter to update the variables?
@raghukgtl
@raghukgtl 11 күн бұрын
Good explanation , one question if some one calls clone method on the singleton instance , it will create another object right ? do we have to override clone method and return same instance , please explain ?
@ShakeYourNoobs
@ShakeYourNoobs Жыл бұрын
I just found out about this channel and subscribed to it. Great Job! Although I think your volatile explanation is a bit wrong. I think volatile tells the OS that this variable will always be written/read to/from the RAM instead of the CPU cache memory. So let's dive in the example where we don't have volatile: Thread A enters the synchronized block and checks if instance is null (still haven't started initialization), at the same time thread B checks if instance is null outside the synchronized block and sees the instance to be null. Therefore thread B also tries to enter the synchronized block but will have to wait for thread A. Thread A after a successful initialization will exit the synchronized block and eventually return. But thread B on the other hand, inside the synchronized block will again see the instance to be null (since the instance variable might only be stored in the cache of a different CPU core where thread A was operating, and where thread B can't see), and will initialize it again. This will not be happening if the "instance" variable is declared volatile: Thread A enters the synchronized block and checks if instance is null (still haven't started initialization), at the same time thread B checks if instance is null outside the synchronized block and sees the instance to be null. Therefore thread B also tries to enter the synchronized block but will have to wait for thread A. Thread A after a successful initialization (will write instance to RAM directly instead of the CPU cache) will exit the synchronized block and eventually return. Now thread B, when checking again if the instance is null inside the synchronized block, will see that it is already initialized (since it will read directly from RAM) and will return that value. Correct me if I am wrong
@geekific
@geekific Жыл бұрын
Thank you for the support! I am afraid you are, it is exactly like I explained in the video, I suggest you watch that part again and if you still have trouble please let me know how can I improve my explanation in the future :) Additionally, feel free to check these references, maybe their approach will be better than mine at explaining the subject: stackoverflow.com/questions/11639746/what-is-the-point-of-making-the-singleton-instance-volatile-while-using-double-l (first answer in this Stack Overflow thread) or en.wikipedia.org/wiki/Double-checked_locking#Usage_in_Java (The part where they explain the "Broken multithreaded version"). Hope this helps! Cheers!
@Fun9to5
@Fun9to5 2 ай бұрын
So with this pattern you can replace a single simple method that instanciate 10 different classes (implementing the same interface/abstract class) in a switch by 10 new creator classes to respect "Open to extension Close to modification" .... That's AMAZING ... What a great idea that's so much better now. LOL
@kiennguyencong5459
@kiennguyencong5459 Жыл бұрын
good
@sthitaprajnapriyadarshan1740
@sthitaprajnapriyadarshan1740 3 жыл бұрын
Good channel. Helped me a lot. However I would request you to put even more detail to compete with other channels.
@geekific
@geekific 3 жыл бұрын
Happy it helped! and thanks for the feedback will take it into consideration in future videos :)
@bhanuchirutha
@bhanuchirutha 2 ай бұрын
OMG wow man
@rohitmarwaha7363
@rohitmarwaha7363 8 ай бұрын
please explain eager and lazy instantion
@rburaksaritas
@rburaksaritas 3 ай бұрын
What is the use of "private String data" field here? If i call the getInstance() function with some data string, it will not update the data field of the instance. The first getInstance() call will set the data value and it will never change again. What am I missing?
@rburaksaritas
@rburaksaritas 3 ай бұрын
Nevermind, the question is already asked before and answered well. If anyone thought the same thing: A: Very nice! This video was mainly about the Singleton Pattern that is why I didn't want to delve other details. But what you could do is create a map and store several singletons based on their names, then the single instance would be tied to that name. So, in the most common example if you were using the singleton for DB instances and you were using two of them, you'll have two records in your map! Hope this helps :) Cheers!
@ExtinctEngineer
@ExtinctEngineer Жыл бұрын
i want a written notes of what ever he said can someone help me?
@TarekFaham
@TarekFaham Жыл бұрын
If the singleton needs to call a web service to fetch the singleton data such as access token, where do you place the logic to invoke the web service? Do you put it inside the singleton class or outside? Please clarify.
@geekific
@geekific Жыл бұрын
Depends on what your singleton is. If that's its whole purpose, like a token generator or something then it should go inside.
@17alienmd
@17alienmd 2 жыл бұрын
After instance was initialised, it doesn't matter anymore what String data we pass as parameter to getInstance, it will always return the instance with the data when the method was called the first time. It this really the correct way of doing that?
@geekific
@geekific 2 жыл бұрын
Very nice! This video was mainly about the Singleton Pattern that is why I didn't want to delve other details. But what you could do is create a map and store several singletons based on their names, then the single instance would be tied to that name. So, in the most common example if you were using the singleton for DB instances and you were using two of them, you'll have two records in your map! Hope this helps :) Cheers!
@Artoooooor
@Artoooooor 11 ай бұрын
That's exactly what I thought.
@tb7377
@tb7377 2 жыл бұрын
I think there is one mistake. An additional 'instance =' At the end it says instance = result = new Singleton(data);
@geekific
@geekific 2 жыл бұрын
Nope :) This is equivalent to result = new Singleton(data); instance = result; we need to assign the new Singleton created to the instance we have and not just the result being returned or else we will be fetching that result and creating a new instance every time! Cheers!
@tb7377
@tb7377 2 жыл бұрын
Oh, good to know. Thanks
@martingeorgiev999
@martingeorgiev999 Жыл бұрын
6:24 I don't get how volatile fixes the problem. It only makes thread B reread the variable from memory when returning it but the variable could still not be fully initialized by thread A.
@geekific
@geekific Жыл бұрын
It won't be there!
@martingeorgiev999
@martingeorgiev999 Жыл бұрын
@@geekific I am afraid I don't understand what you are referring to, what won't be where?
@geekific
@geekific Жыл бұрын
It will not be written to memory until fully initialized
@mgysmt
@mgysmt Жыл бұрын
Good explanation, but the second if condition is redundant, isn't it?
@geekific
@geekific Жыл бұрын
Thank you! Basically more than half of the video was dedicated into answering this question. Kindly watch between 3:25 to 7:00. Cheers!
@nirmalgurjar8181
@nirmalgurjar8181 Жыл бұрын
No, it's just to check again if an instance is already created while the current thread was waiting for lock to acquire after the first check, 2nd check will be rarely used as most of times first check will be not null and return from there, double check is just for initialisation if many threads are trying to initialise the instance at the same time.
@ecstasyofgold888
@ecstasyofgold888 Жыл бұрын
>country can't have two presidents San Marino has entered the chat
@FluteVJ
@FluteVJ Жыл бұрын
Very nice job. But we can still break this by serialize the object and deserialize it to create a duplicate object. I guess you should have covered that particular scenario on how to overcome that.
@geekific
@geekific Жыл бұрын
Thanks for the feedback! Will keep that in mind for future videos! Stay Tuned :)
@nevokrien95
@nevokrien95 11 ай бұрын
There is so much stuff there. This feels like it should be solved by just making the whole thing a global atribute.
@ErikS-
@ErikS- Жыл бұрын
"most used pattern" - As far as I know, many programmers call it an anti-pattern...
@geekific
@geekific Жыл бұрын
Being an anti-pattern isn't keeping people from using it ;P
@baibula
@baibula 6 ай бұрын
Cool, but you should use enum for that.
@hasnatsakib7223
@hasnatsakib7223 Жыл бұрын
kichui bujhlam na
@geekific
@geekific Жыл бұрын
Please, let us know which parts exactly were not clear so we may try and help in the comments section :)
@ukaszkiepas57
@ukaszkiepas57 Жыл бұрын
That was giga clear. Tanks buddy
@jeseniaroberts3574
@jeseniaroberts3574 Жыл бұрын
Thank you!
КАК ДУМАЕТЕ КТО ВЫЙГРАЕТ😂
00:29
МЯТНАЯ ФАНТА
Рет қаралды 9 МЛН
A clash of kindness and indifference #shorts
00:17
Fabiosa Best Lifehacks
Рет қаралды 125 МЛН
Sigma girl and soap bubbles by Secret Vlog
00:37
Secret Vlog
Рет қаралды 12 МЛН
5 Design Patterns That Are ACTUALLY Used By Developers
9:27
Alex Hyett
Рет қаралды 227 М.
Singleton Pattern - Design Patterns (ep 6)
20:09
Christopher Okhravi
Рет қаралды 247 М.
Multithreading in Java Explained in 10 Minutes
10:01
Coding with John
Рет қаралды 898 М.
Strategy Pattern - Design Patterns (ep 1)
35:11
Christopher Okhravi
Рет қаралды 1,4 МЛН
“We Have Been LIED TO...” The Dr Banned For Speaking Out | Dr Aseem Malhotra
21:41