Builder Design Pattern

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
whoa Internet and welcome to part 8 of my design patterns video tutorial today we're going to talk about the Builder pattern and like most of the other tutorials this is hundred percent self-contained so if you're just here to see the Builder pattern you're going to learn everything about it in this one video however if you want to see all the other patterns refer to the link up above and you can check those out as well as brushing up on basic Opie principles so what is the Builder pattern well this is a pattern that's used to create objects made from a bunch of other objects and when you want the creation of these parts to be independent of the main object and also to hide the creation of the parts from the client so both are not dependent upon each other whenever you create a builder pattern the Builder part of the Builder pattern knows all the specifics and every other class involved knows nothing about what's going on in regards to the specifics of the final object you're going to create sounds way more complicated than it is and now you're going to see how simple it is here is the Builder pattern basically I'm using my old alien here and in this situation he is going to request a robot to be built for now here is the interface which we're going to call robot builder and it's going to find what every robot has which means a method that builds a robot head torso arms and legs as well as receives the robot then of course you're going to implement this interface over here with the old robot builder is what I'm going to call it and then you just need to implement all those methods and what this guy does is it's going to set all the specifics for making a very specific robot type and here is the final robot it's going to have a whole bunch of fields this guy up here is going to set the values for those fields whenever build robot heads called and build robot torso and so forth and so on and of course get robots going to return this robot to whoever calls it mainly this little alien over here then we're going to have the robot engineer or what is often called the director and what it does is it creates a robot with the field set by the Builder this guy up here and then finally we have our interface which is going to have all our set methods to set all the values for a robot and then the final robot itself so now we're going to go from this basic little diagram here to build all of the code so on with the code so here we are inside of Eclipse and the first thing we're going to do is come to robot plan which is going to be the interface for creating all of our robots this is real simple we're just going to just like always to find all the methods that need to be set up so we're going to say that we want our robot to have a head and it's going to be passed a string and we're just going to call that head so real simple and then we're also going to define our torso our arms and our legs and then this is going to be past torso arms and legs so here you're going to define all the qualities that you want all of your robots to have and then after that you're going to go to robot Jabba and you're going to implement the robot plan which you just created and if we come up here we're going to just put our mouse over robot and it's going to say add unimplemented methods we're going to do that and there we go and this is going to act as the concrete robot class based off of the interface that we just saw right there so what are we going to need to do well we're going to need to actually create these fields for ourselves so they're all going to be private of course during robot edie robot torso arms and legs and then what we need to do is just come in here and go robot head is equal to whatever the value of head is robot torso and make sure that we what this is upper case of course is equal to whatever torso was when it was Sun over robot arms is equal to whatever the value of arms was and robot legs is going to be whatever the value of legs was and there you go got that all set up now the only other thing we're going to want to do here is put some get methods inside most of the time whenever you have set method you're also going to have get method so we're going to go public string and all of the code that is here is available underneath the video you should get it if you really want to learn this stuff it's free why not okay return robot head real simple and then we're just going to also do the get methods for the torso torso arms and arms and legs and there you go that is all we're going to do with robot Java so now we need to come in here - robot builder Java which is going to be the interface for building all of the different types of robots we want to use so what sort of things we don't want this guy to do public voice well we're going to want to build our robot head of course using interfaces just makes everything so simple don't have these long classes everything is going to be really easy to implement and you don't have to think that much I'm going to make that uppercase and this is going to be torso arms legs and there you go that's all it took to create the basic interface or robot builder da Java now we're going to create old robot builder and this is where you're going to see all the changes because this is going to be the concrete builder class that's going to assemble all the parts for the finished robot object so we're going to have to go private robot because this is going to be building a robot so we're going to need access to an actual robot object and then there's no use typing all that out because we can just put our mouse over this guy of course and go add unimplemented methods and there you go got our stuff automatically thrown inside of there and now since we have robot defined up inside of here in a compositional type of way where the field is actually an object Allah the other strategy pattern sort of kinda we're going to come in here and define this robot which is a reference to this robot up here right there is equal to new robot so now we have that guy all set up and this is the constructor of course so we know that this robot is going to be created whenever this guy is called that's good and then what we need to do is just go robot dot set robot head and since this is an old robot let's say his head is made out of tin just to do something and you say all I'm doing is calling all the methods that were defined inside of the robot class so set robot or so and this is going to be tin torso arms what would be an old-school type of arm for this guy to have about a blowtorch blowtorch arms and then we come down to robot legs and what would be an old-school type of robot leg how about rollerskates so this is a roller-skate robot with blowtorch arms made out of ten and there you go there's the old robot builder Java and every single time you want to define a new type of robot you're just going to create a new builder object that's based off of the main robot builder interface which we just in it and everything is just going to work so now we come to the robot engineer which is what I call it a lot of people call it a director but I just have always called it an engineer so that's what I call it and what this guy is going to do is create a robot using the Builder interface that is defined right here with old robot builder in this situation so what are we going to do we're going to go pro if it's robot builder robot builder is going to be its name and then we're going to call robot engineer which is going to be the constructor and it's going to be passed a robot builder specific object which is going to be our old-style robot type here and then once again we're just going to go this robot builder you can think of it as kind of like a factory type right like that so we know now we have a robot builder or we have a factory for building route robots and this guy back here has robots inside of it so you can think of it this way that this guy defines this factory type don't call it a factory but you know if it makes you understand this better you can think of it that way so we're defining the robot builder factory area and over here we're defining the robot itself inside of old robot builder so that's how we're able to backtrack and actually create these guys so what do we need to do with this well we're going to have to provide the option for get robot so that they'll be able to get the robot that is created inside of here by the engineer and to do that we're just going to go return this robot builder get robot and to do that we're gonna have to go over here to robot builder Java and just go public robot get robot so that it'll be able to return that and then bounce over to the sky right here which is the old robot builder Java which implements the robot builder itself and then of course come in here and implement get robot there you go and what's it going to return it's going to return this robot which this builder knows how to create so pretty simple bounce back over in a robot engineer and then what the robot engineer is going to do is execute the methods that are going to be specific to the robot builder so to do that we're just going to go public avoid make a robot like that then we're going to say this robot builder we want to call the build robot head method I like that which of course is right here build robot head and what's it going to do it's going to set the value to tin head for head inside of robot which is back over here and you can see right there there's robot head so that's exactly what it's doing so bounce back over here so what do we need to do now we just need to set all the other values for the torso and the arms and the legs and that's it so if I'll save that and then we're going to go into test robot builder and we're going to check to make sure we did all this right so what are we going to need to do if we want to build ourselves a robot well we're going to need to get ourselves a robot builder object obviously I'm just going to call it old-style robot and then it's going to be of type old robot builder so you could think of it same as if you're saying okay I want to get myself all robot builder to build me a robot and this is the class blueprint that I want you to use to create that robot and then we're going to need to do is pass the old robot builder specification to the engineer so if we want to pass something to the engineer well we're going to have to actually create the engineer and I need to spell engineer right and there we are so we define that we want to get ourselves a robot builder we want it to use the blueprint to find an old robot builder and then we need to get ourselves an engineer to build it and this is going to be equal to new robot engineer and we need to pass over the specs that we created right here or the blueprint for what type of robot to create and there we are we did that and then we need to tell the engineer that make the robot using the specifications of the old robot builder class so we're going to go robot engine here put a dot there and then what's it going to say as our options well one of our options is make Oh bot well that's definitely not going to be the right thing we'll click on it what did that tell us I told us that we need to change that to robot make robot and then jump over here in a robot engineer and change this to make robot now we are nice way to catch our little errors we've made there and then the engineer is going to return the right robot based off of the spec that we passed it right here now we got ourselves all robot so we're going to go robot first robot is what it's going to be called and it's going to be equal to the object that is going to be returned whenever get robot is sent to the engineer so just to read go over all this stuff basically we're defining a robot builder right here and this can be thought of as a blueprint for creating a very specific type of robot and we want a robot to be built for us what do we need we need an engineer to build that robot for us and to be able to build that robot the engineer is going to need the blueprint which we defined up here passed over to it once it gets the blueprint the engineer being smart like he is can make ourselves a nice little robot and then we're going to be actually able to define the actual robot and just call robot get robot from the robot engineer and it's going to pass it over and the robot will be defined and ready for us to work with so pretty cool especially because nothing knows what's going on except for this guy here the robot builder so let's check to make sure that we actually did this right and we can say proudly that our robot was built and then we need to see if our robot was built right so we could say something like robot head type like that and then we just need to go first robot the object we created and say hey what is your robot head look like and it's gonna tell us and we just copied I pasted that inside of there and we just change this the torso get robot torso arm type first robot get robot arms if you use nice naming conventions you don't even have to think you can just like do this out of your head which is great and get robot legs and if we file/save that we can see what type of awesome robot we created there in a very structured manner and there you go robot built robot head type 10 head 10 torso blow torch arms rollerskates able to put all that together with really only one part of this whole entire Builder pattern knowing any specifics about the total robots is gonna be created everybody else is left in the dark and us does their jobs leave any questions or comments below the last till next time
Info
Channel: Derek Banas
Views: 282,083
Rating: 4.6779261 out of 5
Keywords: Builder Design Pattern, Design Patterns, Design Patterns Tutorial, Design Pattern Tutorial
Id: 9XnsOpjclUg
Channel Id: undefined
Length: 13min 4sec (784 seconds)
Published: Thu Sep 13 2012
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.