Singleton Design Pattern | Implementation with details & code ✌🏻

preview_player
Показать описание
Really really wanted my first YouTube video this year to be a Tutorial! It’s a symbol that this year is going to be filled with a LOT of tutorials - starting with finishing 𝐃𝐞𝐬𝐢𝐠𝐧 𝐏𝐚𝐭𝐭𝐞𝐫𝐧 𝐒𝐞𝐫𝐢𝐞𝐬.

Today’s video is on 𝐒𝐢𝐧𝐠𝐥𝐞𝐭𝐨𝐧 𝐃𝐞𝐬𝐢𝐠𝐧 𝐏𝐚𝐭𝐭𝐞𝐫𝐧. I know most people think they know this design pattern as it’s a basic and common one, but there are details to it that we end up missing. Making the code thread safe and efficient is important when it comes to implementing the code.

So, obviously. I have covered everything with code in detail. Link is in comments. Please keep sharing your feedback.

If you guys really want educators to be motivated and create a lot of tutorials, please do support us - just a like, share or subscribe can mean a lot to us. Trust me - a LOT of effort goes behind these videos!

𝐂𝐡𝐞𝐜𝐤 𝐨𝐮𝐭 𝐄𝐝𝐮𝐜𝐚𝐭𝐢𝐯𝐞 𝐡𝐞𝐫𝐞-

Educative Unlimited-

You can get 𝐃𝐈𝐒𝐂𝐎𝐔𝐍𝐓𝐒 using code "KEERTI" -

You can also connect with me on-

𝐂𝐡𝐞𝐜𝐤 𝐨𝐮𝐭 𝐭𝐡𝐞𝐬𝐞 𝐩𝐥𝐚𝐲𝐥𝐢𝐬𝐭𝐬-
𝐃𝐞𝐬𝐢𝐠𝐧 𝐏𝐚𝐭𝐭𝐞𝐫𝐧𝐬 -
𝐇𝐨𝐧𝐞𝐬𝐭 𝐂𝐨𝐧𝐯𝐞𝐫𝐬𝐚𝐭𝐢𝐨𝐧𝐬 -

The video contains following parts-
0:00-1:45 - Intro of Singleton Design Pattern
1:45-2:08 - Educative Promotion
2:08-4:45 - Simple Logger Class Code
4:45-6:33 - Count instances of Logger
6:33-7:25 - Restrict Logger Instantiation
7:25-11:52 - Simple Singleton Implementation
11:52-15:32 - Code fails in MultiThreading Case
15:32-17:20 - Thread Safe Singleton
17:20-19:03 - Double-checked Locking
19:03-20:15 - Points to keep in mind!

#designpatterns #singleton #systemdesign #softwareengineering
Рекомендации по теме
Комментарии
Автор

Thanks a ton Keerti! Such an awesome video including the multithreading handling part which was unexpected, and while learning I enjoyed it throughly. Paused the video in between to like it because this video and this design pattern series deserves it. Looking forward for next videos in the series :D

arjungupta
Автор

Hey Keerti, This was really great explanation. Most videos would stop showing the implementation about singleton design pattern. But the fact that you showed how to make them thread safe was a cherry on top. One request though, could you increase the font size or zoom in into your code editor in upcoming videos as it becomes difficult when viewing on phone.

pratikwadekar
Автор

Thanks a lot, Keerti. I am fresher and was super scared when I first check what it is online, but you made it super simple that even a fresher who barely knows these concepts in detail can understand it. Super happy learning this in such a simple way.

vaibhavjain
Автор

Keerti this is by far the best video I have watched. The way you simplify everything and explain things with so much calm, simplicity is just amazing. Waiting for this entire series. It's great learning this with you. Thankyou so much :)

ishajindal
Автор

I use to always skip the design pattern concepts..but with your videos...its so easy to understand...i look forward to other videos... great work thanks for sharing the knowledge wirh us...😍

swatiacharya
Автор

Thank you so much for explaining Singleton Design pattern, making it so easy for me to implement in my code.

Small request: Can you explain the concept first diagrammatically like you did for vtable and vptr.

SouravChakraborty-yw
Автор

Wonderful explanation Keerti, It is hard to find Oops tutorial in C++ and you have handled it easily. Thanks a lot again.

RohitRaj-qvnq
Автор

Thank you ma'am. I have a question. If you null pointer check for loggerInstance on Ln. 21 then do you really think null check required on Ln.24 as well

MilindKTechnology
Автор

To restrict access to the operator=, you can declare it as private in the class definition, as you've already done. However, you should also provide an empty definition for it to make it inaccessible and avoid compilation errors. Here's how you can do it:

cpp code

private:
Logger& operator=(const Logger&) = delete;


Adding = delete explicitly specifies that the copy assignment operator is deleted and cannot be used. This way, any attempt to use the copy assignment operator will result in a compilation error

whynesspower
Автор

Thanks a lot Keerti. This is the most complete video I have seen so far. I like the way you explain "WHY" we need to do it. Really appreciate your work.

kedarsadhu
Автор

Thanks for the video Keerti. Simple and detailed. Good work!!

AnupTiwari-nv
Автор

Lovely explanation of double checking before locking for thread-safe single ton logging🔥

SugamMaheshwari
Автор

Keerti awesome work on the video. I do have a suggestion though. It would be great to if you have have those multiple files in the split window so that it is easily to reference and connect while watching the video itself. This is one way to do it, but the idea is to have visibility or maybe use lesser files. I totally understand that this way it is much more modular but I think for watching everything on a single screen helps better.

insofcury
Автор

Thank you very much Di, got this from end to end like thread safe and all....
One doubt why we use .hpp and .cpp files we can't do in .cpp file only, its little bit confusing to me, i haven't code like this in cpp

And please add more videos.... this Playlist is going to be best on YouTube for LLD and system design 💯💯

roushanraj
Автор

Once we have checked that loggerInstance==nullptr, why is there a need to check it again, can't we just apply the lock and create a new loggerInstance as we know that it is null?

aryanyadav
Автор

Thanks Keerthi! Your explanation was very excellent.But I had a doubt.Instead of using header files can't we write all the code in a single file?.Will there be any issues in doing so? Because not everyone can create those header files that easily.I am a fresher basically So I wanted to know if this is the actual convention to be followed or if it is also fine to code everything in a single file.

sammetajhansi
Автор

Thanks Keerti for such crisp & nice video capturing all the necessary points related to singleton design pattern.
Really appreciate for posting such videos which helps the entire developer community.

shashankcool
Автор

Clearly Visible, Thank You :)
It complied on first time, on staging code - Truly magical!

SourabhDesaiBrief
Автор

Very clear explanation with examples. Thanks Kreeti

sivakumardugana
Автор

Fantastic video. I came here after hearing your interview experience. I learned MULTITHREADING just to understand this video.

VY-ztph
welcome to shbcf.ru