Builder Design Pattern Tutorial

Builder Design Pattern TutorialWelcome 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

Sharing is nice

Code from the Video

RobotPlan.java

// This is the interface that will be returned from the builder

public interface RobotPlan{
	
	public void setRobotHead(String head);
	
	public void setRobotTorso(String torso);
	
	public void setRobotArms(String arms);
	
	public void setRobotLegs(String legs);
	
}

Robot.java

// The concrete Robot class based on the RobotPlan interface

public class Robot implements RobotPlan{

	private String robotHead;
	private String robotTorso;
	private String robotArms;
	private String robotLegs;
	
	public void setRobotHead(String head) {
		
		robotHead = head;
		
	}
	
	public String getRobotHead(){ return robotHead; }

	
	public void setRobotTorso(String torso) {
		
		robotTorso = torso;
		
	}
	
	public String getRobotTorso(){ return robotTorso; }

	
	public void setRobotArms(String arms) {
		
		robotArms = arms;
		
	}
	
	public String getRobotArms(){ return robotArms; }

	
	public void setRobotLegs(String legs) {
		
		robotLegs = legs;
		
	}
	
	public String getRobotLegs(){ return robotLegs; }
	
	
	
}

RobotBuilder.java

// Defines the methods needed for creating parts 
// for the robot

public interface RobotBuilder {
	
	public void buildRobotHead();
	
	public void buildRobotTorso();
	
	public void buildRobotArms();
	
	public void buildRobotLegs();
	
	public Robot getRobot();
	
}

OldRobotBuilder.java

// The concrete builder class that assembles the parts 
// of the finished Robot object

public class OldRobotBuilder implements RobotBuilder {

	private Robot robot;
	
	public OldRobotBuilder() {
		
		this.robot = new Robot();
		
	}
	
	public void buildRobotHead() {
		
		robot.setRobotHead("Tin Head");
		
	}

	public void buildRobotTorso() {
		
		robot.setRobotTorso("Tin Torso");
		
	}

	public void buildRobotArms() {
		
		robot.setRobotArms("Blowtorch Arms");
		
	}

	public void buildRobotLegs() {
		
		robot.setRobotLegs("Rollar Skates");
		
	}

	public Robot getRobot() {
		
		return this.robot;
	}
	
	
	
}

RobotEngineer.java

// The director / engineer class creates a Robot using the
// builder interface that is defined (OldRobotBuilder)

public class RobotEngineer {
	
	private RobotBuilder robotBuilder;
	
	// OldRobotBuilder specification is sent to the engineer
	
	public RobotEngineer(RobotBuilder robotBuilder){
		
		this.robotBuilder = robotBuilder;
		
	}
	
	// Return the Robot made from the OldRobotBuilder spec
	
	public Robot getRobot(){
		
		return this.robotBuilder.getRobot();
		
	}
	
	// Execute the methods specific to the RobotBuilder 
	// that implements RobotBuilder (OldRobotBuilder)
	
	public void makeRobot() {
		
		this.robotBuilder.buildRobotHead();
		this.robotBuilder.buildRobotTorso();
		this.robotBuilder.buildRobotArms();
		this.robotBuilder.buildRobotLegs();
		
	}
	
}

TestRobotBuilder.java

public class TestRobotBuilder {
	
	public static void main(String[] args){
		
		// Get a RobotBuilder of type OldRobotBuilder
		
		RobotBuilder oldStyleRobot = new OldRobotBuilder();
		
		// Pass the OldRobotBuilder specification to the engineer
		
		RobotEngineer robotEngineer = new RobotEngineer(oldStyleRobot);
		
		// Tell the engineer to make the Robot using the specifications
		// of the OldRobotBuilder class
		
		robotEngineer.makeRobot();
		
		// The engineer returns the right robot based off of the spec
		// sent to it on line 11
		
		Robot firstRobot = robotEngineer.getRobot();
		
		System.out.println("Robot Built");
		
		System.out.println("Robot Head Type: " + firstRobot.getRobotHead());
		
		System.out.println("Robot Torso Type: " + firstRobot.getRobotTorso());
		
		System.out.println("Robot Arm Type: " + firstRobot.getRobotArms());
		
		System.out.println("Robot Leg Type: " + firstRobot.getRobotLegs());
		
	}
	
}

22 Responses to “Builder Design Pattern Tutorial”

  1. 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?

  2. Venkata Naveen Alapati says:

    Thanks Derek for your wonderful tutorials.
    newthinktank is now one of my favorite websites.

    Thanks again.

  3. Amir says:

    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.

  4. proXMan says:

    I like these tutorials, it makes me go deep into design pattern

  5. bigO says:

    Thanks Derek for the awesome tutorial. Could you please explain how this is different from the factory pattern?

  6. Ash says:

    thanku……….

  7. khoa says:

    that great tutorial. I think the method getRobot() must return RobotPlan interface, is it right?

    Thanks for your help

  8. Karim Masoud says:

    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 .

    • Derek Banas says:

      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.

  9. rling says:

    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.

    • rling says:

      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).

  10. Jack says:

    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

Leave a Reply

Your email address will not be published.

Google+