How to BREAK and FIX Singleton Design Pattern | Interview Question

แชร์
ฝัง
  • เผยแพร่เมื่อ 23 ต.ค. 2022
  • Head to cutt.ly/spring_micro and use Coupon Code SUPER70 to get 70% off the course.
    The singleton pattern is implemented by creating a class with a method that creates a new class instance if one does not exist. An instance already exists simply returns a reference to that object.
    To ensure that the object cannot be instantiated any other way, the constructor is made private.
    GitHub Repo: github.com/shabbirdwd53/desig...
    🎟 Join this channel to get access to perks:
    / @dailycodebuffer
    RECOMMENDED COURSES
    🎬 Spring Boot + React Full Stack Web Application with Tailwind CSS - • Spring Boot + React Fu...
    🎬 Spring Security Full Course | From Registration to Login - • Spring Security Tutori...
    🎬 Microservices using SpringBoot | Full Example - • Microservices using Sp...
    🎬 Event-Driven Microservices with CQRS Design Pattern - • Implement CQRS Design ...
    🎬 Implement SAGA Design Pattern using Spring boot and Axon Framework - • Implement SAGA Design ...
    🎬 Spring Data JPA Tutorial | Full In-depth Course - • Spring Data JPA Tutori...
    🎬 Deploy Springboot Microservices to Kubernetes Cluster - • Deploy Springboot Micr...
    🎬 Automate Microservices deployment with Github Actions - • GitHub Actions Tutoria...
    🎬 Go / Golang Full Course for Beginners - • Golang Tutorial for Be...
    🎬 Node JS Full Course for Beginners: Learn in 2 Hours - • Node JS Full Course fo...
    🎬 Express.js Tutorial - Beginner | 2021 - • Express.js Tutorial - ...
    🎬 Redis CLI Tutorial | Complete Course - • Redis CLI Tutorial | C...
    BE MY FRIEND:
    👨‍💻. Website: www.dailycodebuffer.com/
    👨‍🏫 Facebook: / dailycodebuffer
    🐦 Twitter: / dailycodebuffer
    📸 Instagram: / dailycodebuffer
    🎮 GitHub: github.com/dailycodebuffer
    MY FAVOURITE GEAR:
    👨‍💻 My Macbook Air M1: amzn.to/3eFXlID
    💻 ASUS ROG Zephyrus G14, 14": amzn.to/32xsXhf
    💻 ASUS ROG Zephyrus G14, 14" (US): amzn.to/3xa6X6m
    🎤 My Main Mic: amzn.to/3qDl0z0
    🎤 My Main Mic (US): amzn.to/3awzUj7
    🎙 My Secondary Mic: amzn.to/3sOZCJW
    🖥 My LG Monitor: amzn.to/3zhLY3e
    🖱 My Mouse: amzn.to/3eDpRKT
    ⌨️ My Keyboard: amzn.to/32xthMZ
    RECOMMENDED BOOKS:
    📙 Mastering Spring Boot 2.0 (Kindle): amzn.to/3HsOiHu
    📙 Mastering Spring Boot 2.0 (US): amzn.to/3tHNUy5
    📙 Building Microservices(Kindle): amzn.to/3gsA1jL
    📙 Building Microservices(US): amzn.to/3dDnfNn
    📙 Spring Boot in Action: amzn.to/32Nf3qW
    📙 Spring Boot in Action (US): amzn.to/32DkeX9
    📙 Spring Microservices in Action: amzn.to/3mQ4sCH
    📙 Spring Microservices in Action(US): amzn.to/32G3Hl7
    🎻 MUSIC:
    🎵 Hear the Noise (Sting): th-cam.com/users/audiolibrary...
    “Sound effects obtained from www.zapsplat.com“
    #singleton #softwaredesign #java #DailyCodeBuffer
  • วิทยาศาสตร์และเทคโนโลยี

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

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

    Bhai faad .....abhi tk sbse best video ..no bakwas no makhan malayi.. only main content in basic and easy language

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

    Thank you for listening to our suggestions and starting with design pattern. I hope this is a series and you cover some more patters, well the most common ones at least.

  • @akhiljuluru927
    @akhiljuluru927 ปีที่แล้ว +13

    please make all design patterns videos. Thank you for all your videos

  • @mihirshinde4268
    @mihirshinde4268 ปีที่แล้ว +3

    Best part of your videos is: Complete Information in short time. Keep it up!

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

    Shandar bhai! Maza aagya. Keep posting such content!

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

    in depth explaination , hats-off

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

    Great Content brother! Appreciate it!

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

    The question was asked in MakeMyTrip Interview. about the multiple threads waiting time, you explained the synchronized Block.
    I was not able to answer that scenario, But now I got it.
    Thanks for the proper explanation.

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

    Great, the enum one is something I came to know after long time.

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

    U explain the singleton dp well..thanks

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

    Thanks for sharing knowledge. Respect to you brother.

  • @Anilkumar-reddy
    @Anilkumar-reddy ปีที่แล้ว

    Nice Explanation bro, ur the life saver

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

    Wow nice one Shabbir 🔥🔥

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

    Very straightforward..

  • @DeepakGupta-pz4fx
    @DeepakGupta-pz4fx ปีที่แล้ว +1

    Thank you Bro for this series start ...please complete alll topic of design patterns....Happy Diwali

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

      Thanks 🙏
      Yes working on it
      Happy Diwali

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

    Thanks for the video, quick question how can you have the instance of a class on line 13 in the same class define?

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

    Very good explanation 😊

  • @AdityaRaj-ix5rg
    @AdityaRaj-ix5rg ปีที่แล้ว

    Thanks for the awesome video on design patterns. Kindly make video course of apache kafka as well.

  • @ipsadas4233
    @ipsadas4233 29 วันที่ผ่านมา

    Great video😊

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

    Hi bro, Great explain Can you also define program via enum as solution against reflection.

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

    Need one video on builder design pattern and where to use them. Please keep making informative videos.

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

    Design Patterns is a very complicated topic. Very good tutorials are not there on TH-cam. I hope you start the design patterns series.

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

    Excellent bro happy diwali

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

    Should have said though, great video, thanks.

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

    Bro can you also please make videos on multithreading in brief please 🙏🙏🙏🙏

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

    super explanation

  • @user-kf5hb4dm6x
    @user-kf5hb4dm6x ปีที่แล้ว

    Use volatile keywords while declaring refrence variable

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

    Awesome bro 👌👌👌

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

    I have seen the static instance declared as volatile. What does volatile keyword do?

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

    Superb

  • @abhishekchatterjee3750
    @abhishekchatterjee3750 9 วันที่ผ่านมา

    Once the first thread get a lock on synchonized block, the other thread will be waiting at that point , but the other threads has already passed the if condition, those are just waiting for their turn to execute the creation of the object again when the lock is released by the first thread. Is it like the if condition is going to be reevaluated again after the next thread get the lock. I am confused sinsed the synchronised block is inside the if condition. ?

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

    You can also add a Clone example to break and fix Singleton design pattern.

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

    so you mean from line 22 the sync method will be shared across all threads?

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

    Can u make java beginners to advanced video

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

    Shouldn't your readResolve method call the getInstance() method? Otherwise it could return null no?

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

    Good work brother

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

      Thanks 🙏🏻

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

      @@DailyCodeBuffer you should do live question and answer for interviews questions. Also live project it be fire where people can do supervhst and if they tipped you super chat you can give them personal help with the thing they having problem with the project

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

      Great Suggestion
      I will think about it. Thanks 🙏

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

      @@DailyCodeBuffer you can send notifications to your to subscribers. It can be big projext like fully functional full stack e-commerce website where every can pull and push the project in git and contribute to it. You can ask people if they can do something different and if they have trouble they can send certain fee and you can do personal one on one session to improve their application. While people on super chat who is having trouble you can show and what they may do wrong and answer the questions. I think it great idea it help lot of people. It will help people to see how you going about codding and why you use certain data structures over certain data structures. I will definitely join and I am pretty sure lot of people will join

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

    Really nice
    Still did not grasp enum example. Enum Example is too short. Not clear. How use enum to instantiate database connection ?

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

      public enum DatabaseConnection {
      INSTANCE;
      private Connection connection;
      DatabaseConnection() {
      try {
      // Load the JDBC driver
      Class.forName("com.mysql.cj.jdbc.Driver");
      // Create the database connection
      connection = DriverManager.getConnection(
      "jdbc:mysql://localhost:3306/myDatabase", "username", "password");
      } catch (ClassNotFoundException | SQLException e) {
      throw new RuntimeException("Database connection failed!", e);
      }
      }
      public Connection getConnection() {
      return connection;
      }
      }
      public class DatabaseUsageExample {
      public static void main(String[] args) {
      Connection conn = DatabaseConnection.INSTANCE.getConnection();
      // Use the connection for database operations
      // Remember to close any Statements and ResultSets, but you might not want to close the connection itself
      // as it is managed by the Singleton and should stay open for the lifetime of your application.
      }
      }
      we can use this way to create db connection using Enum.

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

    Hi, what about using clone method? using clone method also we can break the singleton rule..

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

      Just throw the clone exception if the object clone is carried out

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

    Holly crap i learned a lot. ty so much.

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

    I have a query.
    Why only one DB object?
    If created more DB connection then it will be easy to service the concurrent request coming at same time.. right?

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

      Yes, Absolutely 👍
      This is just an example to explain singleton

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

    8:20 The instance variable has to be volatile else the logic won't work.

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

    Bhai hindi ma video bno...

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

    Cloning the object will also break singleton right?

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

      Yes, Implement the Cloneable Interface and Override the clone method and throw an exception. It will fix that loop hole.

  • @user-fr9jb3sm4v
    @user-fr9jb3sm4v ปีที่แล้ว

    Good! New cap👍