How to use the Builder Pattern in Java - 035

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

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

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

    one of the best java tutorials ever happens on youtube. I do respect on the way he explained with a real-world example.

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

    Think I finally get it because of the explanation on previous videos. Proper teaching.

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

    I see some comments claiming code doesn't work...slight mistake..blah blah..but what more important here is the way how the things are explained....this channel needs more attention..you got a new subscriber

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

    ok. That is an ASAP subscribe. This is EXACTLY the next level I was looking after reading all the beginners tutorials in programming and feeling completely stable. Thumbs UP!

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

      Glad it helps! That's what I'm shooting for. Intro that goes beyond "type this".

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

    Interesting, i read an article on DigitalOcean that suggested making the Game class have a private constructor and to make GameBuilder an inner static class. This way, the only way you can instantiate Game is to use the builder. Good video!

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

    Thank you sir im so stupid so im glad i have people like you to help me

  • @neeravarora530
    @neeravarora530 4 ปีที่แล้ว

    crisp and to the point thank u

  • @danielchoi4202
    @danielchoi4202 9 ปีที่แล้ว +8

    Amazing tutorial. Why aren't you more well-known?

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

      +Daniel Choi Thank you! Not sure? Probably because I've only been doing tutorials since April?

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

    Subscribed! This was by far the most concise explanation I have seen.

  • @marcoscaldaferri5663
    @marcoscaldaferri5663 3 ปีที่แล้ว

    This video is a blessing, thanks (from Italy 🍕)!

    • @marcoscaldaferri5663
      @marcoscaldaferri5663 3 ปีที่แล้ว

      I really hope you are having (or you have had) the succes you are looking for (on TH-cam, and/or shool/university as a teacher); you deserve that.

  • @sagaravhad5198
    @sagaravhad5198 6 ปีที่แล้ว

    Thank you so much. You always explain the concept clearly. please never stop making videos.

  • @sergioangel8362
    @sergioangel8362 9 ปีที่แล้ว

    Really enjoyed this, easy to understand and follow. This really helped me understand the builder patterns used by the Android Pay API. Thank you!!

    • @deegeu
      @deegeu  9 ปีที่แล้ว

      +Sergio Angel Glad I could help! Thanks for watching.

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

    best one on builders! Thank you!

  • @sankalparora9374
    @sankalparora9374 3 ปีที่แล้ว

    Amazing tutorial!

  • @kumarsantoshdash
    @kumarsantoshdash 8 ปีที่แล้ว

    Awesome tutorial! Focused on particular problem and it's solution.

    • @deegeu
      @deegeu  8 ปีที่แล้ว

      +Santosh kumar Dash Thank you!

  • @raviranger52
    @raviranger52 3 ปีที่แล้ว

    once a great explanation always a great explanation .. even after 5 years :)

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

    very good explanation.

  • @mouradbarakat123
    @mouradbarakat123 7 ปีที่แล้ว

    Thanks for the video. One thing to note here is that we create an Instance or Object of a class, not creating a class.

  • @ersenosman4699
    @ersenosman4699 9 ปีที่แล้ว

    Nice video, thank you. I have question. With the builder pattern, how would we go about changing or updating a value. For example. Let's assume I made a Game object that did not have weather but later I want to add to the existing object later. How would I go about this with the pattern? Please also assume that the build method ignores the null check for weather. Let me know if the question is clear or not. Thanks for reading.

    • @deegeu
      @deegeu  9 ปีที่แล้ว

      +Ersen Osman If I understand your question, I think you'd just add a setter method to change the weather. You can have a value set in the builder, and also set later as and attribute setter.

  • @LucasLopesdoAmaral
    @LucasLopesdoAmaral 4 ปีที่แล้ว

    Is there any problem if I have more constructors? I mean... a Full constructor with all parameters, an empty Constructor and a Contructor with the builder approach?

  • @mdfarooq7145
    @mdfarooq7145 7 ปีที่แล้ว

    Thanks for the awesome explanation.

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

    Awesome! Loved it, just subscribed and like the video. Thanks.

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

    Thanks for that nice explanation!

  • @luisfvm9381
    @luisfvm9381 9 ปีที่แล้ว

    great tutorial!! greetings from Mexico.

    • @deegeu
      @deegeu  9 ปีที่แล้ว

      +lluuii fre Hola! Thanks and thank you for watching!

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

    Thank you

  • @javadoctor101
    @javadoctor101 7 ปีที่แล้ว

    Hey Deege, great video. Is it a good practice to have the builder class as "STATIC" inner class rather then it being "Non-Static"? What are the advantages to make the builder class static?

  • @alexisbrunet2707
    @alexisbrunet2707 3 ปีที่แล้ว

    nice thanks

  • @IsayArenasSoto
    @IsayArenasSoto 9 ปีที่แล้ว

    Thank you, very clear.

    • @deegeu
      @deegeu  9 ปีที่แล้ว

      +Isay Arenas Soto Thanks for watching!

  • @derrick3534
    @derrick3534 6 ปีที่แล้ว

    You are awesome. Subscribed.

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

    Awesome, thanx!

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

    good video, tx

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

    Awesome Explanation ! just subscribed and like the video. Thanks.
    Would be great help , if you could provide similar video tutorial on other patterns and principles as well .

    • @deegeu
      @deegeu  7 ปีที่แล้ว

      I had planned on doing pattern videos, but haven't got around to it yet. Hopefully some time this year?

  • @georgievvladimir
    @georgievvladimir 6 ปีที่แล้ว

    Great video. BTW can you provide us with a tutorial how to use builder pattern extending a class, please ?

  • @egri_ervin
    @egri_ervin 9 ปีที่แล้ว

    cool tutorial!

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

      +ibanezsindrome Thank you!

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

    great video.... :)

    • @deegeu
      @deegeu  8 ปีที่แล้ว

      +Jeet Parmar thanks for watching!

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

    Hi the video is awesome, thank you for your help. I can't find the source code from this episode on Github. Is there any chance you could send me the source code?

    • @deegeu
      @deegeu  8 ปีที่แล้ว

      +Bartosz Prażmo I've fallen a bit behind on the source code. I'll try to get that shored up this week. Sorry about that.

  • @adamytkowski1764
    @adamytkowski1764 8 ปีที่แล้ว

    That doesn't quite work for me. In the method Game build() an error pops up saying that the constructor for Game is undefined

  • @premraj.m
    @premraj.m 8 ปีที่แล้ว +1

    It really awesome..:)

    • @deegeu
      @deegeu  8 ปีที่แล้ว

      +prem raj Thank you!

  • @ripperx444
    @ripperx444 8 ปีที่แล้ว

    I have been creating a nested public static class Builder and inside and right away I make an instance of the parent class. Then my in my Builder I just set the instance fields and in the Build I just return that instance. I find it easier than having my constructor take a Builder builder. I am new to Java coming from OO Python and I gotta say this language really sucks. I find everything to be a huge pain to work with to do even basic things.

  • @robertczaja7151
    @robertczaja7151 7 ปีที่แล้ว

    awsome !

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

    Maybe I discovered a slight mistake here.
    4:16 All the properties of the GameBuilder class are private
    4:44
    awayTeam = builder.awayTeam;
    homeTeam = builder.homeTeam;
    etc...
    Wut?

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

      Actually no, that's correct. You usually want your attributes to be private, and only accessible through getters and setters. The code sets the variables in the constructor, where they are accessible. You might check out episodes 27 (th-cam.com/video/JK7ltJLCgqM/w-d-xo.html) and 28 (th-cam.com/video/eQCXrNBD0aQ/w-d-xo.html) where I talk about that a bit. If that doesn't help, let me know and I can create another video on it. Thanks!

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

      Hello there, Deege U. Thanks for the reaction, but that is not quite what I meant.
      What I mean here is that all the properties of the GameBuilder class have been set on private, that is indeed a good move. Although... In the Game class, all the properties are used as though they were public properties. I could be wrong, but wouldn't this be correct?
      awayTeam = builder.getAwayTeam();
      homeTeam = builder.getHomeTeam();
      etc...

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

      Ah I think I see what you mean. The GameBuilder attributes are private, and would not be accessible in the Game class. Yes, that's an error. :) Doh! Good catch!

    • @mustafasygn
      @mustafasygn 7 ปีที่แล้ว

      So do we need to put it inside the game class or what?

    • @Keyakina
      @Keyakina 6 ปีที่แล้ว

      No use getters instead of directly referring to variables

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

    I really wish I had the source code for this. I'd like to get in the habit of using the builder pattern than chaining constructors.

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

      So I haven't gotten around to filling in the source code for the Java tutorials, mostly because the code would just be toy examples. But I do have some code using the builder pattern for an upcoming tutorial. There's other code surrounding it, but you can find the builder pattern here. github.com/deege/deegeu-quizzer/blob/master/src/main/java/com/deegeu/trivia/model/TriviaQuestionBuilder.java

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

      Thanks!

  • @1오징오
    @1오징오 5 ปีที่แล้ว

    Starts at 1:10

  • @alexm4121
    @alexm4121 7 ปีที่แล้ว

    I realize that the train left the station a while ago.
    Nevertheless.... On appox. 4:27 the following is painted
    public Game build(){
    if (homeTeam ==null){
    throw new IllegalStateException("No HomeTeam");
    }
    if (awayTeam ==null){
    throw new IllegalStateException("No AwayTeam");
    }
    ............................................................................................
    ............................................................................................
    ...........................................................................................
    return new Game (this);
    }
    The method build of type Game belongs to the GameBuilder class, correct?
    And, as name suggests, i have expected to see here the actual build, i.e.
    Game game = new Game();
    game.homeTeam(ht);
    game.awayTeam(at);
    ...................................
    ...................................
    return Game
    But the method does not build anything, just provides some validations
    What am i missing? Probably everything :-)))

  • @risteardob2095
    @risteardob2095 6 ปีที่แล้ว

    You forgot to implement the Director class. Without the Director class the Client is going to be tightly coupled to the builder.

  • @AndersRapp
    @AndersRapp 8 ปีที่แล้ว

    You claim that the builder pattern should only be used for large complex classes. I see your point but the builder pattern is supposed to help you in case your class grows very large and you keep adding more variables to the constructor. The way to avoid future problems for people accessing your code is to use the builder pattern from the start. :)

    • @ripperx444
      @ripperx444 8 ปีที่แล้ว

      Anders Rapp I started in Java recently and after my first time trying to remember the order of a 10 arg constructor I decided builder is the only way to go here.

  • @Maxi-ym8du
    @Maxi-ym8du 4 หลายเดือนก่อน

    fucking legend!

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

    I dont think you ever showed a GameBuilder.build method. You showed a Game.build method but not a GameBuilder.build method. But after calling all the build methods you recommended calling .build last. So im just going to a assume GameBuilder.build looks something like the Game.build method

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

    interesting. thanks