Singleton Design Pattern | Implementation with details & code ✌🏻

แชร์
ฝัง
  • เผยแพร่เมื่อ 27 ก.ย. 2024

ความคิดเห็น • 145

  • @arjungupta250
    @arjungupta250 2 ปีที่แล้ว +11

    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

    • @KeertiPurswani
      @KeertiPurswani  2 ปีที่แล้ว +1

      Thank you so much. Sorry for releasing the rest of the series late but finishing it soon ❤️😇

  • @swatiacharya9073
    @swatiacharya9073 10 หลายเดือนก่อน

    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...😍

  • @whynesspower
    @whynesspower 11 หลายเดือนก่อน +3

    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

  • @aryanyadav3926
    @aryanyadav3926 ปีที่แล้ว +5

    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?

    • @Aman-pd4bg
      @Aman-pd4bg 28 วันที่ผ่านมา

      Baat me dum hai

    • @RohitKumar-sb4qc
      @RohitKumar-sb4qc 3 วันที่ผ่านมา

      No, that would miss the case when both the threads have gone passed by the first if-condition, they both will eventually access the constructor, though one after the other. That's why the second if-condition is a must too

  • @AnupTiwari-n3v
    @AnupTiwari-n3v 10 วันที่ผ่านมา

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

  • @insofcury
    @insofcury 4 หลายเดือนก่อน

    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.

  • @pratikwadekar4981
    @pratikwadekar4981 2 ปีที่แล้ว +21

    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.

    • @KeertiPurswani
      @KeertiPurswani  2 ปีที่แล้ว +6

      Thanks Pratik. Will definitely increase more from next time😇
      Feedback like yours helps me improve in every video❤️😇

  • @SugamMaheshwari
    @SugamMaheshwari 4 หลายเดือนก่อน

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

  • @venkatkrishna3774
    @venkatkrishna3774 2 ปีที่แล้ว

    Please increase the font size when you write code or zoom in a bit. Difficult to read from distance. Thanks

  • @roushanraj8530
    @roushanraj8530 2 ปีที่แล้ว

    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 both..... 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 TH-cam for LLD and system design 💯💯

  • @aloklakhotia7251
    @aloklakhotia7251 2 ปีที่แล้ว +4

    Great video. Thanks for this. :D
    One doubt: Isn't the inner if condition redundant as the lock is already captured?

    • @KeertiPurswani
      @KeertiPurswani  2 ปีที่แล้ว +1

      Thanks Suraj. For explaining so well! ❤️😇

    • @geeksentertainmentmedia4827
      @geeksentertainmentmedia4827 2 ปีที่แล้ว +1

      And "upper if" to just stop "locking" once you have created the instance.

  • @PkSam07
    @PkSam07 2 ปีที่แล้ว

    Awesome explanation! but I think you missed to explain why we should go for singleton pattern? I think you mentioned in order to avoid multiple logs in different places? I didn't get that?

  • @RaisShaikh-bs7dh
    @RaisShaikh-bs7dh ปีที่แล้ว

    Thanks Keerti, Such a great video including each smaller things in it. One request if you can give one session on only mutex and semaphore will help us a lot. Thank you so much again for this wonderful session.

  • @osamayounus4867
    @osamayounus4867 2 ปีที่แล้ว

    Thanks for these videos. One thing which would help with visibility is if you can show all open code files side by side

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว

      But you won’t be able to see the code clearly on screen. I am going to upload all to github pretty soon!

  • @VY-zt3ph
    @VY-zt3ph 2 ปีที่แล้ว

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

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว

      Woah! How awesome is that! ❤️😇

    • @VY-zt3ph
      @VY-zt3ph ปีที่แล้ว

      @@KeertiPurswani Thank you Di

  • @sasageyo9571
    @sasageyo9571 2 ปีที่แล้ว +1

    Are freshers (university grads ) expected to face these questions ?
    Great video btw !!!

  • @47hriday
    @47hriday 2 ปีที่แล้ว +1

    Its best explanation about singleton.Thank You.

  • @svarajdhanulkar1791
    @svarajdhanulkar1791 11 หลายเดือนก่อน

    Yar jo wo problem tha like k if Multithreading is in picture then singleton fails or tabhi muje realise hua uska bhi jugad hota hai par ye interview me pucha tha or sch me tabhi realise hua k hmm such a diff pov

  • @krishnasummer5603
    @krishnasummer5603 ปีที่แล้ว

    Hi sis, your explanation is very clean & good, if u can add more design patterns .It'll be a great help. Thanks.

  • @mrCetus
    @mrCetus 2 ปีที่แล้ว

    One Request: Please give the direct link to the playlist of design patterns you are covering to check other patterns easily.

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว

      There’s a playlist on the channel - Design Patterns
      th-cam.com/play/PLliXPok7ZonlZJuAN0hvUnf5ovFepjxU0.html
      😇😇

  • @Kaushikraj9845
    @Kaushikraj9845 2 ปีที่แล้ว +1

    Assuming if we write the code in java ..Can we create object during initialization like below
    public class Singleton {

    private static Singleton singleton=new Singleton();;
    private Singleton()
    {
    super();
    }

    public static Singleton getSingleton()
    {
    return singleton;
    }
    }

  • @anonymoushacker9094
    @anonymoushacker9094 2 ปีที่แล้ว

    Isn't it better to use static class for logger rather than singleton pattern. Some of the useful example can be parking lot

  • @SourabhDesaiBrief
    @SourabhDesaiBrief 2 ปีที่แล้ว

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

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว

      Thank you! A lot of effort goes behind every single video! ❤️

  • @mayurbadwaik6401
    @mayurbadwaik6401 ปีที่แล้ว

    wonderfull explantion keerti, one doubt -> as explained to use mutex, can we follow this approach
    -> run 1st thread and after that remaining threads -> anyways we are just blocking 2 threads to make instances for the first time and after the instance is created it would not matter how many threads access it

  • @sivakumardugana3272
    @sivakumardugana3272 ปีที่แล้ว

    Very clear explanation with examples. Thanks Kreeti

  • @sohelsheikh1934
    @sohelsheikh1934 2 ปีที่แล้ว

    For Java People, to handle multithreading do below changes in Logger class -
    public static Logger getLogger() {

    if(logger == null) {
    logger = getInstance();
    }
    return logger;
    }
    synchronized public static Logger getInstance(){
    if(logger == null)
    return new Logger();
    else
    return logger;
    }

  • @paraskaushik2639
    @paraskaushik2639 ปีที่แล้ว

    Why have you made 3 files ? logger.hpp , logger.cpp and user.cpp . why cant we directly used logger.hpp in user.cpp ? is there some pattern , folder structuring you have followed ?

  • @Luckygangff
    @Luckygangff 4 หลายเดือนก่อน

    When we delete the instance and created new instance 2nd time in main we are unable to delete the instance which we created 2nd time getting double free error

  • @srilakshmiuvpakala3267
    @srilakshmiuvpakala3267 ปีที่แล้ว

    Hi Keerthi, thanks a million for the wonderful videos. I have one question in implementation of singleton design pattern.
    "Why do we need to make assignment operator in private. Assignment operator gets called when we try to assign existing object to another already existing object. In other words this gets called only when we have two instances already created which we already prevented from happening by making default and copy constructors private.

  • @Ww-ul8oe
    @Ww-ul8oe 2 ปีที่แล้ว +1

    Thank you Keerti. Appreciate implementing in c++

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว +1

      Thank you! Please do subscribe and share the video with your friends❤️😇

  • @mayankrai7938
    @mayankrai7938 2 ปีที่แล้ว +2

    Finally back ☺️

    • @KeertiPurswani
      @KeertiPurswani  2 ปีที่แล้ว +1

      With many many videos this time❤️😇

  • @bheemsenkulkarni2471
    @bheemsenkulkarni2471 ปีที่แล้ว

    Thanks Keerthi, this was a very good explanation about singleton

  • @AshishIkhar-k7b
    @AshishIkhar-k7b ปีที่แล้ว

    Thanks Keerti, can you please record the session on "Smart Pointer"

  • @vinamrajha1571
    @vinamrajha1571 12 วันที่ผ่านมา

    You forgot to return as reference when you overloaded the assignment operator.

  • @kingshahzadaamir
    @kingshahzadaamir 2 ปีที่แล้ว

    Hey Keerti, I tried this on gcc linux terminal. without mutex also it gave single instance all the time.
    spent 15 minutes to figure out what wrong I wrote. but then tried it on online gdb compiler; there it varied then.

  • @nileshpatil4111
    @nileshpatil4111 2 หลายเดือนก่อน

    Nicely explained. Understood it completely

  • @ArchanaBiradar-l5h
    @ArchanaBiradar-l5h 10 หลายเดือนก่อน

    How about TH-cam adding search option in comments section.

  • @arkosnatoneogy2366
    @arkosnatoneogy2366 8 หลายเดือนก่อน

    after you implement the double checked lock is the inner if condition needed any more? isnt it bound to be TRUE?

  • @rohitsankarpal7866
    @rohitsankarpal7866 ปีที่แล้ว

    Can someone help me with VS Code as the thread is not supporting !! Any idea how to resolve that ?

  • @whynesspower
    @whynesspower 11 หลายเดือนก่อน

    20:05 can you give me the code for that?
    Will it be something like
    delete Logger operator=(const Logger &)?
    Correct me if I am wrong

  • @sheshitkarthikeya1528
    @sheshitkarthikeya1528 2 ปีที่แล้ว

    Your effort to keep things simple is amazing...Also don't worry no one will steal your logger constructor.😂

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว

      Hehe thank you so much! ❤️❤️

  • @virendramane7818
    @virendramane7818 8 หลายเดือนก่อน

    Thank you Keerti! Really appreciate your work.

  • @guruprasad_sannenahalli
    @guruprasad_sannenahalli 2 หลายเดือนก่อน

    Thanks Keerti. Very useful.

  • @durgeshrai04
    @durgeshrai04 ปีที่แล้ว

    Thanks a lot Kreerti! for the wonderful explantion of singleton design pattern.

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว +1

      Thank you! Hope you like other videos on the channel as well! 😇

  • @athena2986
    @athena2986 2 ปีที่แล้ว

    Thank you for explaining in detail.can you please create GitHub links for the codes specially the design pattern ones

  • @2008suryan
    @2008suryan 2 ปีที่แล้ว

    Very good clear explanation. Expecting more videos on other design patterns.

  • @j.r.krishna1122
    @j.r.krishna1122 ปีที่แล้ว +1

    You made my day

  • @tarunkumar7656
    @tarunkumar7656 3 หลายเดือนก่อน

    AWESOME, Great work!

  • @AnkitaSingh-vv5rc
    @AnkitaSingh-vv5rc 3 หลายเดือนก่อน

    Thankyou so much, Keerti Mam

  • @kartheekgoud9009
    @kartheekgoud9009 2 ปีที่แล้ว

    Thanks Keerthi.. Really great explanation 👏.

  • @skandan6266
    @skandan6266 2 ปีที่แล้ว

    Please make the code view bigger. It becomes difficult to read while watching on a phone

  • @meme_engineering4521
    @meme_engineering4521 2 ปีที่แล้ว

    excellent!!, can someone please tell me if this playlist is enough for sde1 interviews?

  • @nkjat123
    @nkjat123 ปีที่แล้ว

    Hi Guys.. awesome videos

  • @AnkitSingh-lb9fj
    @AnkitSingh-lb9fj 2 ปีที่แล้ว

    great stuff as always, waiting for next video on design pattern

  • @vivekshivhare8820
    @vivekshivhare8820 ปีที่แล้ว

    Can we delete the second time nullptr checking as we are already checking it for mtx locking, or that can also cause some problem? just curious, please do reply..

  • @bsratuoh
    @bsratuoh ปีที่แล้ว

    It is really a great session and helpful. Thanks for this. If possible pls increase the font size while explaining codes.

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว

      Thanks for the feedback! Hoping it’s better in the present videos! ❤️😇

  • @RishabhRD
    @RishabhRD 2 ปีที่แล้ว

    If you are writing singleton in C++... Meyers singleton is the standard way of doing this. I guess covering meyers singleton was super important not done.... BTW singletons are anti pattern avoid it

  • @rohit_kapdi
    @rohit_kapdi ปีที่แล้ว

    Superb explanation. ❤

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว

      Thank you! Hope you like other videos as well! 😇

  • @varshakancham5944
    @varshakancham5944 2 ปีที่แล้ว

    Why are locks expensive

  • @hrudayd4217
    @hrudayd4217 2 ปีที่แล้ว +1

    Normally front end developer uses js, learning dsa using js is it useful or not ?

    • @KeertiPurswani
      @KeertiPurswani  2 ปีที่แล้ว +1

      Dsa is useful for sure (language doesn’t matter that much - as long as you can solve questions)
      But I do know some front end developers who are doing really well and don’t know dsa much.

  • @niksgupta36
    @niksgupta36 2 ปีที่แล้ว

    In Double-checked Locking, instead of adding another nullpointer check, could we have just added mtx.lock() and unlock() inside the initial nullpointercheck? Should work right. Let me know.

    • @geeksentertainmentmedia4827
      @geeksentertainmentmedia4827 2 ปีที่แล้ว +4

      You mean putting lock inside if? It would have same problem . Two threads would have entered inside the "if " condition , and then lock would have happened. So one thread would create instance , unlock , then another would have created instance.

    • @niksgupta36
      @niksgupta36 2 ปีที่แล้ว

      @@geeksentertainmentmedia4827 Makes sense now. Thanks.

  • @PROTECHRAHUL
    @PROTECHRAHUL 2 ปีที่แล้ว +1

    Hey diii i didn't get what are you teaching can you please tell? So i can learn it.

    • @KeertiPurswani
      @KeertiPurswani  2 ปีที่แล้ว

      Hi Rahul, start watching the design pattern playlist. The first video is introduction of what design patterns are. It should help. Let me know if you have any doubts😇

    • @PROTECHRAHUL
      @PROTECHRAHUL 2 ปีที่แล้ว +1

      @@KeertiPurswani Thanks dii. 🤗

  • @integrateeverything
    @integrateeverything 2 ปีที่แล้ว

    Hi didi ,your videos are just awesome keep making videos like this and motivating us

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว

      Thank you! Please keep sharing videos and motivating me! ❤️

  • @jenishadsouza907
    @jenishadsouza907 2 ปีที่แล้ว

    Really great explanation!!!

  • @vishalsethi4024
    @vishalsethi4024 2 ปีที่แล้ว

    Great Teacher YOU are

    • @KeertiPurswani
      @KeertiPurswani  2 ปีที่แล้ว +1

      Thank you! Means a lot❤️😇

  • @Archak77
    @Archak77 ปีที่แล้ว

    Excellent video ...simple explaination 👍

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว

      Thank you so much! Please do share with your friends?

  • @tech_wizard9315
    @tech_wizard9315 2 ปีที่แล้ว +1

    Please provide best resume templates for freshers to apply for tech giant's. It's need of many students

  • @nikhillaad941
    @nikhillaad941 2 ปีที่แล้ว

    awsome explaination

  • @mobin1457
    @mobin1457 ปีที่แล้ว

    Perfect work

  • @fakecake7608
    @fakecake7608 2 ปีที่แล้ว

    Great complete Video :)

  • @JimitRupani
    @JimitRupani 2 ปีที่แล้ว

    It was really helpful.

  • @svarajdhanulkar1791
    @svarajdhanulkar1791 11 หลายเดือนก่อน

    Thanks mam

  • @kushagra4401
    @kushagra4401 2 ปีที่แล้ว

    di do you work in cpp in your company too?

  • @SaiPrakashdsp
    @SaiPrakashdsp 2 ปีที่แล้ว

    Can you please write code in python from next videos if possible

  • @faizanbhat5902
    @faizanbhat5902 8 หลายเดือนก่อน

    damn easy explanation @keeti_Purswani

  • @PRANAVMAPPOLI
    @PRANAVMAPPOLI ปีที่แล้ว +1

    Hey, thanks for the awesome explanation. Why we need the 2 if check, cant we remove the inner if check?

    • @akshayyadav6854
      @akshayyadav6854 9 หลายเดือนก่อน

      Have the same question...

  • @MilindKTechnology
    @MilindKTechnology ปีที่แล้ว +1

    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

  • @mohanganesh1055
    @mohanganesh1055 2 ปีที่แล้ว +1

    Mam, please complete the design pattern playlist as fast as possible.

  • @RohitRaj-qv5nq
    @RohitRaj-qv5nq 2 ปีที่แล้ว +2

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

    • @KeertiPurswani
      @KeertiPurswani  ปีที่แล้ว +1

      Thank you so much! Means a lot! Please do share with your friends! ❤️

  • @sammetajhansi8063
    @sammetajhansi8063 20 วันที่ผ่านมา

    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.

  • @vaibhavjain8672
    @vaibhavjain8672 5 หลายเดือนก่อน +1

    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.

  • @aryankumar87771
    @aryankumar87771 2 ปีที่แล้ว +1

    wait wait what was that at the last? making constructor private and operator overloading private... as a non C++ coder that hit me like a train

  • @theSDE2
    @theSDE2 2 ปีที่แล้ว +1

    keerti please do a parallel code for java as well.

  • @potassium_cyanide_boy8515
    @potassium_cyanide_boy8515 ปีที่แล้ว

    What if exception is thrown Inside a mutex lock ? It will never ever unlock the mutex and no other thread will be able to enter the critical section or they will keep waiting to mutex to get unlocked that will never happened. So we should use unique_lock or lock_guard i guess 😒😒

  • @amithtripst9851
    @amithtripst9851 3 หลายเดือนก่อน

    Could you please let us know what is the IDE you are using for practice?

  • @ravirayala2168
    @ravirayala2168 ปีที่แล้ว

    Explanation is good but screen color is very bad unable to see clearly

  • @siddeo85
    @siddeo85 ปีที่แล้ว

    nice video but could you make videos using java?

  • @matheens6689
    @matheens6689 ปีที่แล้ว

    You can use semaphore ? Why mutex

  • @palakjain2505
    @palakjain2505 2 ปีที่แล้ว

    why do we even need a constructor here?

  • @hari8222
    @hari8222 3 หลายเดือนก่อน

    nice explanation

  • @powprashant
    @powprashant 2 ปีที่แล้ว

    Hello Keerti,
    Thanks for the video, but this is still not complete. Particularly the ownership of the instance, how the instance will be released/destroyed.
    You can use smart pointer (since C++11)
    But the best would be using Meyer's Singleton that works with C++98 and C++O3 as well

  • @shyamchauhan520
    @shyamchauhan520 2 ปีที่แล้ว

    Nice explanation :)
    One doubt: when are we going to delete the resource(loggerInstance) which we have created dynamically using new keyword.

  • @mayankprabhakar5336
    @mayankprabhakar5336 ปีที่แล้ว

    Thanks for such a simple explanation!!
    I have one Q. Should I also delete "move constructor" & "move assignment operator" in singleton class?
    If YES or No, explanation would be very helpful.

  • @amarnathsama
    @amarnathsama 2 ปีที่แล้ว

    Can someone explain why we need to have objects of the class at all? why can't getLogger function be static also?

  • @shankarBinnar-xl8en
    @shankarBinnar-xl8en ปีที่แล้ว

    Thanks Keerti for this video..learn singlton design pattern in detailed . can I get your codes on any website ??

  • @AdityaNakhare
    @AdityaNakhare 2 ปีที่แล้ว

    In a main method, you can create a singleton class instance before creating a thread. There won't be any contention issue, locking is also not needed.

  • @sanjaygatne1424
    @sanjaygatne1424 หลายเดือนก่อน

    बहोत अच्छेसे और गहराईसे समझाया.
    फाॅंट का आकार बडा होता पढने मे आसानी होती.
    नलपीटिआर् दो बार चेक हो गया है. जगह और वेग के संबंध मे.

  • @nehakale9427
    @nehakale9427 ปีที่แล้ว

    Excellent explaination Keerti!

  • @JRKrishna-d1e
    @JRKrishna-d1e ปีที่แล้ว

    Thanks Keerti! Very good way of explanation. God bless you.

  • @gauti_gaming
    @gauti_gaming 2 ปีที่แล้ว

    Please release videos fast