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

  Рет қаралды 95,890

Geekific

Geekific

Күн бұрын

Пікірлер: 83
@NguyenTrang-je6zg
@NguyenTrang-je6zg Жыл бұрын
for those who got error at 2:00, each of the setter method should return this, so that you can call method chaining in main
@mahanirvaantantra
@mahanirvaantantra Жыл бұрын
Ideally it should NOT return this, instead the return type should be changed to void. As we are making changes to the object our purpose is not to return anything new. And in creating the car object in the builder class, these variables should be like this.id, this.brand, this.model, etc, because these are object methods...
@TheEmbustil
@TheEmbustil Жыл бұрын
or abstract
@masterflitzer
@masterflitzer 11 ай бұрын
@@mahanirvaantantra with void you can't do chaining...
@mahanirvaantantra
@mahanirvaantantra 11 ай бұрын
@@masterflitzer Yes. That's true. But for chaining he should have return 'this' or 'self'(in python). He didn't have it
@gopsLiverpool
@gopsLiverpool 2 жыл бұрын
This series is so clear and concise. There's great explanation, clear code and a relevant UML diagram which matches the code.
@gopsLiverpool
@gopsLiverpool 2 жыл бұрын
Just some add-on, it'll be awesome if you could add sequence diagram. Cheers
@geekific
@geekific 2 жыл бұрын
Thanks a lot! Glad you liked it :)
@VivekDudani
@VivekDudani Жыл бұрын
Glad I found this series. Such a clear, crisp and step by step process of explaining different concepts. Thank you ❤.
@1CheeseBurger
@1CheeseBurger Жыл бұрын
Delivered with such quality and clarity. Gold.
@tb7377
@tb7377 2 жыл бұрын
At 2:00 the signature of the methods inside CarBuilder does not match the code. For example method id does not return a CarBuilder
@geekific
@geekific 2 жыл бұрын
You are right! I think I missed them because they were pretty obvious in my head :D Anyway thanks for pointing that out! Cheers :)
@anushakandagal1727
@anushakandagal1727 2 ай бұрын
So far the best video for builder pattern, thanks!
@enishalilaj9309
@enishalilaj9309 2 жыл бұрын
One of the best videos I've seen regarding this topic so far! Great work!
@bjorn1409
@bjorn1409 Жыл бұрын
agreed
@Felipinho5
@Felipinho5 6 ай бұрын
Wonderful explanation!
@FarazAzadi
@FarazAzadi Жыл бұрын
Thanks a lot 🙏 If you want to use chain function calls, you should return "this" to return the object itself. ( It's forgotten in code. )
@thatwasamazing2791
@thatwasamazing2791 Жыл бұрын
Best explanation I’ve come across
@svalyavasvalyava9867
@svalyavasvalyava9867 7 ай бұрын
Wonderful explanation. Thank you! 😊
@SerhiiArtymovych
@SerhiiArtymovych 7 ай бұрын
Great series! Thank you!
@jpelegrinotube
@jpelegrinotube 2 жыл бұрын
These series are great, I have watched many videos and studied books about design patterns but these videos are engaging me. Excelente work!!!
@geekific
@geekific 2 жыл бұрын
Glad you like them! Thank you :)
@abdielortega9778
@abdielortega9778 9 ай бұрын
I loved the explanations 🥰
@royalzsoftware
@royalzsoftware Жыл бұрын
Great! Thanks for sharing. Really clear, keep up with these videos
@スターダスト-j5u
@スターダスト-j5u 2 жыл бұрын
Thanks, I'm Japanese. I can't understand English almost. But this video is perfect. I could understand builder pattern on this video than reading a textbook about gof in Japan. I admire you. thank you
@geekific
@geekific 2 жыл бұрын
Thanks a lot for the kind words :) Glad I could be of help!
@nirmalgurjar8181
@nirmalgurjar8181 2 жыл бұрын
If you can't understand English then how are you writing it.
@スターダスト-j5u
@スターダスト-j5u 2 жыл бұрын
@@nirmalgurjar8181 I know English a little
@nirmalgurjar8181
@nirmalgurjar8181 2 жыл бұрын
@@スターダスト-j5u ok .. you can write perfectly..
@スターダスト-j5u
@スターダスト-j5u 2 жыл бұрын
@@nirmalgurjar8181 thanks
@limitlesskode
@limitlesskode Жыл бұрын
2:48 if the Car constructor is private how does the CarBuilder build method construct the car class?
@geekific
@geekific Жыл бұрын
1/ Inner builders can still make use of it. 2/ In this video it is package-protected, and our builder sits in the same package (check this video: kzbin.info/www/bejne/Zl6XYoaDpLuSiqM).
@jayak3768
@jayak3768 6 ай бұрын
Here some problems I see: 1. 6:00 why Builder is an interface and not an abstract class. 2. The whole idea of Builder pattern is to allow clients to initialize the object with custom parameters as needed. The Director hides that complexity and provides a generic Build method. Meaning again the client is tied to the objects being initialized with specific params.
@ulfdellbrugge827
@ulfdellbrugge827 Жыл бұрын
Great. I don't like real world examples like Cars but I still understood the concept very well even with audio only.
@Captinofthemudslayer
@Captinofthemudslayer Жыл бұрын
Theses patterns seem overly complicated for no reason
@geekific
@geekific Жыл бұрын
When you apply them on simple examples they do!
@razorblade413
@razorblade413 Жыл бұрын
I'm starting to learn some patterns like state or memento. While those are useful, I honestly found this one overly complicated, because if the constructor has so many parameters why not just create an empty constructor and then populates the objects fields calling the setters methods like? Car car = new Car(); car.setBrand("Something"); // other setters fields that you want to initialize... car.setHeight(1.5); instead of using all those new classes?
@caballerosalas
@caballerosalas Жыл бұрын
Isolation
@youme1461
@youme1461 Жыл бұрын
@@razorblade413because of immutability, if you want to make a property immutable, builder is the way to do it. With setters it’s not possible
@tulasidamarla
@tulasidamarla 8 ай бұрын
@@razorblade413 This approach has a flaw. The object's are always mutable, whereas if you use builder, the objects returned from it are immutable. This is explained by joshua bloch in effective java.
@ivandrofly
@ivandrofly Жыл бұрын
Top video on builder pattern
@FarhanKhan
@FarhanKhan 2 жыл бұрын
superb explanation ... thanks for making the video
@joostvandam8624
@joostvandam8624 9 ай бұрын
great video!
@ryankao1983
@ryankao1983 2 жыл бұрын
crystal clear video thank you
@geekific
@geekific 2 жыл бұрын
Glad you like it :)
@Ellefsen97
@Ellefsen97 11 ай бұрын
I like the Builder Pattern in some usecases, but the usecase would need to be justified imo. I would rarely use it in place of a constructor, if ever. Let's say that the constructor has 10 properties and we want to create 1 million cars. Instead of performing ~11 operations (calling constructor, setting the 10 properties), we need to perform ~3 operations per property (calling the build method, setting the property and return the Builder). It won't matter in most scenarios, but in this hyperbolic example the CPU would do approximately 11 million operations in the constructor example and 33 million in the builder example. Having long constructors is also rarely an issue since we just use variables as arguments: new Car(id, brand, make, doors, engine, ...). Then we can just make the optional properties nullable. I'm only critiquing it's usecase for being a replacement for constructors. It can be very useful in certain usecases like the StringBuilder, DateTimeFormatter etc. It's also useful when you want to chain modify default/set properties, like the Locale: Locale locale = builder.setLanguage("en").setRegion("US").build(); I love using the Builder Pattern in this scenario. We can change all the properties that we want in one line, and we can ignore the Locale properties that we want to leave as default.
@nafiislam498
@nafiislam498 2 жыл бұрын
Awesome explaination
@clement1370
@clement1370 3 жыл бұрын
Thx for this Pattern ! 😀
@geekific
@geekific 3 жыл бұрын
You're welcome 😊
@Labandusette
@Labandusette 10 ай бұрын
very good
@khalidsaifullah374
@khalidsaifullah374 Ай бұрын
We can not use director if param values are different?
@trocandobytes
@trocandobytes 11 ай бұрын
I loved you explanations, but in the builder class is missing "return this" without thit you cannot link the methods with '.'
@cristianpalechor2011
@cristianpalechor2011 3 жыл бұрын
You are so awesome !!
@geekific
@geekific 3 жыл бұрын
And you rock
@michaelfulton1080
@michaelfulton1080 2 жыл бұрын
Thanks :)
@whm920
@whm920 5 ай бұрын
how is using the director class different from the Factory pattern?
@Sobioytccc
@Sobioytccc Жыл бұрын
If the builder is not a part of class representation then how can it call it's constructor. And if there is no getter method for the class, how the client will be able to access it's field.
@jhonatandariomarinjaramill9519
@jhonatandariomarinjaramill9519 2 жыл бұрын
thank you very much for the video, the build method should be put within the Builder interface, I thanks for the answer and again thank you very much for the video
@geekific
@geekific 2 жыл бұрын
You are welcome! Glad it helped :)
@jhonatandariomarinjaramill9519
@jhonatandariomarinjaramill9519 2 жыл бұрын
@@geekific excuse me for the bad writing, I asked you if the build method should be put within the Builder interface in the previous message but I forgot the question mark (?), could you respond me that?¿the build method has to be within the Builder interface or not?
@geekific
@geekific 2 жыл бұрын
No worries :) Up to you! The basic ideas of the implementation will not change. Cheers!
@haripriyachintalapati
@haripriyachintalapati Ай бұрын
in CarBuilder setters, return CarBuilder
@NhanNguyen-h6g
@NhanNguyen-h6g 6 ай бұрын
In the CarBuilder class, why don't you put directly the Car as this class property rather than cloning all properties of Car into the builder? I mean that if we need to modify some properties of the Car, we must update the CarBuilder also.
@Thori45
@Thori45 Жыл бұрын
I have a question, I read in the internet that you can use a public static inner class as a Builder in the object you want to create to implement the Builder Pattern. Is this basically the same? What are the advantages / disadvantages doing it either way?
@geekific
@geekific Жыл бұрын
Yes you can do that, and on the larger scheme of things it won't really matter which option you pick. The main advantage of the inner class is the encapsulation of the construction logic within the class you are working with, leading to a more clean and concise code. However, it may increase the complexity as the class will be harder to understand if you are not familiar with the pattern, and may introduce some limitation in accessing private fields. Hope this helps :)
@inkofthedragon
@inkofthedragon Жыл бұрын
Do you think the Builder Pattern could be an effective way to build different kinds of AiControllers with different behaviours/states and abilities, say for an Orc, Goblin, Troll, etc? Would the architecture flow look like? Thanks
@geekific
@geekific Жыл бұрын
I am not sure I understood 100%, but I will try to answer. If you have a common Character class let's say extended by several other classes (Orc, Goblin etc.) the builder pattern can indeed be used to initialize the objects based on these classes. Cheers!
@inkofthedragon
@inkofthedragon Жыл бұрын
@Geekific ok yes, so would i first have a Orc class and then an OrcBuilder class or a EnemyControllerBuilder class with methods like AddAbility, AddState, AddStat? And then inject in all the Abilities, Stats, States, etc? Then perhaps the EnemyControllerBuilder class would return a controller to the Orc class...and that would be the Orc? Thanks again
@geekific
@geekific Жыл бұрын
I don't really like creating builders for classes that do not exist. If you have only an Orc class then create one builder for it. If you have an Enemy class extended by the Orc class, then yeah you may have two builders and pass stuff from one to another.
@markos8383
@markos8383 Жыл бұрын
At 2:24 so basically we are just hiding the ugly part. We also need the constructor with those nulls.
@geekific
@geekific Жыл бұрын
But still it is a cleaner approach because in that constructor you always pass the variable, there is no mention of 'null' elements in your code.
@jorgegallego9672
@jorgegallego9672 3 ай бұрын
Shouldn´t you be returning "this" after every builder method? They all return CarBuilder but then the method is just a setter, this will give an error as "Expected CarBuilder but returned void"
@coolfyb
@coolfyb Жыл бұрын
CarBuilder instead of duplicating Car fields, can contain Car and set it's fields via setters (restricted)?
@geekific
@geekific Жыл бұрын
This will give you more flexibility and less coupling. Ex: If a Car's parameter is actually deduced from two other parameters that are not in the Car class, you can add these two params in the builder and the Car class doesn't need to know about them etc. Cheers!
@tshepotsotetsi1067
@tshepotsotetsi1067 3 ай бұрын
Was just having a conversation with someone about this
@JuanRamirez-fx3tf
@JuanRamirez-fx3tf Жыл бұрын
How can you implement a builder in a children class? I only want to set some parameters of the parent class, and the id in the children class.
@geekific
@geekific Жыл бұрын
By calling super() in the respective constructors.
@yatri6329
@yatri6329 2 жыл бұрын
What is the meaning of making carSchema is this for to make different types of car like one implementation will make one kind of car and other is for different type. But this we can do inside director only. Than what is the meaning of carSchema. Plz explain
@geekific
@geekific 2 жыл бұрын
In this video carSchema was used to explain one advantage of using a Director, and it is storing the common initialization of multiple objects which in turn have builders that implement the same interface. Hope this answers your question!
@buku69420
@buku69420 3 ай бұрын
i am crying
@MeraBaapHaiTu31
@MeraBaapHaiTu31 5 ай бұрын
Isn't it right that Director will also need the data or variable values which it will pass to Builder So How will we pass it to Director? Ans: We will create a Builder for that Director class 😂
«Жат бауыр» телехикаясы І 30 - бөлім | Соңғы бөлім
52:59
Qazaqstan TV / Қазақстан Ұлттық Арнасы
Рет қаралды 340 М.
The Lost World: Living Room Edition
0:46
Daniel LaBelle
Рет қаралды 27 МЛН
7 Design Patterns EVERY Developer Should Know
23:09
ForrestKnight
Рет қаралды 208 М.
Java Builder Pattern Explained in 3 Minutes
3:19
Jack Hodkinson
Рет қаралды 17 М.
This is the Only Right Way to Write React clean-code - SOLID
18:23
10 Design Patterns Explained in 10 Minutes
11:04
Fireship
Рет қаралды 2,4 МЛН
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 909 М.