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

preview_player
Показать описание

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:

#Geekific #DesignPatterns #SingletonPattern #CreationalPatterns
Рекомендации по теме
Комментарии
Автор

Damn man. That's a unique teaching ability to explain every reason. Nice.

Salehalanazi-
Автор

Very well constructed, step by step explanation. Thank you!

Nitionful
Автор

Thanks for the video, it was really useful.

EpicGamer-uxtu
Автор

seen this indepth singleton implementation, mostly people just do a first level of implementation without thinking of the multithreaded environment.

flgtbux
Автор

Hey man, your videos are super awesome. Thank you so much for the good work

cool_huip_
Автор

Thank you so much for making this video. Helped a lot!

StratOnFire
Автор

Your videos are so so so good!! Thank You!!

grzegorzmolin
Автор

The only thing I have to say is 'Perfectly Thank you!

SyncEdgeHandle
Автор

Thank you so much for a well coordinated video!! Kudos to you

Sarah-zoys
Автор

Great explaining skills, I read many documentation but I completely understood from your video

azizbekkhushvakov
Автор

Good channel. I started reading Head First Design Pattern, but I think I'm gonna drop it and just learn from you.

----
Автор

You are amazing bro, you nailed it, I will definitely subscribe to such an amazing and valuable content channel, wish you growth and wealth :)

hassansakr
Автор

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!

eugenebespalov
Автор

Absolutely love the video although the last third of the video took 90% of my brainpower to keep up

sovngarden
Автор

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 ?

raghukgtl
Автор

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

ShakeYourNoobs
Автор

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.

murad
Автор

Great video. But how to we actually use it now? Should there also be a setter to update the variables?

greglang
Автор

Good channel. Helped me a lot. However I would request you to put even more detail to compete with other channels.

sthitaprajnapriyadarshan
Автор

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

Funto