Welcome to my Builder design pattern tutorial. You use the builder design pattern when you want to have many classes help in the creation of an object. By having different classes build the object you can then easily create many different types of objects without being forced to rewrite code.
The Builder pattern provides a different way to make complex objects like you’d make using the Abstract Factory design pattern. All of the code follows the video to help you learn.
If you like videos like this, it helps if you tell Google [googleplusone]
Sharing is nice [nttfblike]
Code from the Video
RobotPlan.java
Robot.java
RobotBuilder.java
OldRobotBuilder.java
RobotEngineer.java
TestRobotBuilder.java
Nice explanation of the Builder Design pattern.
And what can you say about situation when we haven’t a ‘director’ at the pattern scheme? Instead of that we can put static builder class in the domain model and declare there methods which will set needed field of the domain model and returns the domain model until we call ‘build()’ method?
Thank you 🙂 I guess you could use an inner class as a builder. I prefer to separate them, but that is just an opinion
Thanks Derek for your wonderful tutorials.
newthinktank is now one of my favorite websites.
Thanks again.
You’re very welcome 🙂 Thank you for visiting my little website
Thanks for your awesome tutorials. One thing to consider though is that sometimes you start writing a bad code first and make it “good” using a pattern. I learned them quicker because I immediately saw the point of using the pattern. But sometime like on this one, You define the pattern and you just start writing the code. So it took me a while to find out why this is a good approach practically after reviewing your codes and some other tutorials. Just my two cents.
Thank you very much for the tip 🙂 I wish I would have done that now in hindsight
I like these tutorials, it makes me go deep into design pattern
Thank you 🙂 I’m glad you have enjoyed them.
Thanks Derek for the awesome tutorial. Could you please explain how this is different from the factory pattern?
Thank you 🙂 Probably the best thing I can do is to point you to my Factory tutorials because I have a couple. Here is the first Factory DP tutorial. Here is one on an abstract factory design pattern. Here is a factory method tutorial. And, another example using the abstract factory pattern. I hope that helps.
Awesome! Thanks! 🙂
You’re very welcome 🙂
thanku……….
You’re very welcome 🙂
that great tutorial. I think the method getRobot() must return RobotPlan interface, is it right?
Thanks for your help
Thank you 🙂 Yes it returns a Robot instance
excuse me , why did you used the RobotPlan interface for the Robot class to implement the getters of head –> legs .
also why were you have to use RobotBuilder interface for the builder ?
i mean can’t i write the methods of the interfaces directly to my classes ( Robot , OldRobotBuilder ) ?
thanks in advance .
I basically did everything this way to demonstrate the builder pattern. I kept the example simple to make sure the pattern was easier to understand. Yes you are correct that these results could be gained without the pattern.
Great videos 🙂 I have leaned a ton! Thanks.
I’m relatively new to this stuff. So maybe I have overlooked something here, but RobotPlan seems like it’s not part of the pattern. It seems to provide some flexibility to the code, but doesn’t.
I has thinking that if “getRobot” method in class RobotEngineer returned a RobotPlan (instead of Robot),then the engineer could be used on other implementations of RobotPlan (if we also make other implementation of the RobotBuilder and added a setBuilder method to the RobotEngineer class).
Example: If we want to make androids. Then we could implement the RobotPlan interface to an Android class and implement the RobotBuilder interface to an AndroidBuilder class and then after changing the robotBuilder (and adding a setBuilder method) in the RobotEngineer class, call the makeRobot and the getRobot methods to make an android.
Instead of making a setBuilder method. We could make a new instance of RobotEngineer in the testRobotBuilder (in any case an instance of the AndroidBuilder must be created first and either past to the RobotEngineer through the AndroidBuilder constructor or the added setBuilder method).
Hi Derek,
Thank you so much for the awesome tutorials. I started at JavaLessonOne.java and now I’m here at lesson 71, Patterns :D. I graduated years ago when procedural programming was the thing, but your videos have taught me so much more than my time there.
I’ve been supplimenting your design patterns with additional design patterns I found on Github after watching your git tutorials. Here is the link:
https://github.com/iluwatar/java-design-patterns
You’re very welcome 🙂 Yes I remember the procedural programming days. Thank you for posting the link.