JavaCodingNerd

Java Coding Nerd. This Blog will feature content-rich articles, tutorials and own opinions on variuos programming related topcis.

Design Patterns

Java Design Patterns: Builder Pattern

BuilderPattern

BuilderPattern

The Builder Pattern

The Builder Pattern is  a so called creational pattern. It is used to build objects with a lot of parameters. Lets start with an example that does not use the builder Pattern. You should always use this pattern if you constructer would have more than 4-5 parameters.


public class Person {

private String name;
private String lastName;
private String title;
private int age;
private float weight;

// different constructors
public Person(String name, String lastName) {
this.name = name;
this.lastName = lastName;

}

public Person(String name, String lastName, String title) {
this.name = name;
this.lastName = lastName;
this.title = title;

}

public Person(String name, String lastName, String title, int age) {
this.name = name;
this.lastName = lastName;
this.title = title;
this.age = age;
}

public Person(String name, String lastName, String title, int age, float weight) {
this.name = name;
this.lastName = lastName;
this.title = title;
this.age = age;
this.weight = weight;

}

// Getters and Setters
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public float getWeight() {
return weight;
}

public void setWeight(float weight) {
this.weight = weight;
}

}

I know this class is long. But there is nothing too complex. Lets focus on why this code is bad.
First it is hard for other developers who use this class (and yourself) to create an object of Person because there are so many different constructers. You have to be careful to remeber which parameter is which value. It becomes even harder because there is more paramater than one type (String). It is easy to accidently write the lastName in the name parameter.
Furthermore this code is incredibly hard to maintain. Just imagine how big the effort ist to add an aditional value, or to delete one. Or you decide you want to intialize a Person with just it´s age. Correct you would have to write yet another constructor. Of course you could have also used the setters but in case you want an immutable Class you cannot have any.

Using the Builder Pattern

To overome all those downsides you can use the Builder Pattern.
It is a very readable and maintable way to create Objects. Look at the nested class called Builder


package BuilderPattern;

public class Person {

private String name;
private String lastName;
private String title;
private int age;
private float weight;

// different constructors
public Person(String name, String lastName) {
this.name = name;
this.lastName = lastName;

}

public static class Builder {
private String name;
private String lastName;
private String title;
private int age;
private float weight;

public Builder name(String name) {
this.name = name;
return this;
}

public Builder lastName(String lastName) {
this.lastName = lastName;
return this;
}

public Builder title(String title) {
this.title = title;
return this;
}

public Builder age(int age) {
this.age = age;
return this;
}

public Builder weight(float weight) {
this.weight = weight;
return this;
}

public Builder build() {
return this;
}

}

// Getters and Setters
public String getName() {
return name;
}

public String getLastName() {
return lastName;
}

public String getTitle() {
return title;
}

public int getAge() {
return age;
}

public float getWeight() {
return weight;
}

}

Instead of having dozens of different constructors, you can easily create Persons now with different attributes by using the Builder Pattern


public static void main(String[] args) {

Person buildedPerson = new Person(new Builder().age(12).lastName("Norris").name("Chuck").weight(120).build());
Person buildedPerson2 = new Person(new Builder().age(12).build());

}

If another attribute has to be added to you Person later you can just rewrite your Persons class without having to change all code that uses the Person class. This would indeed be the case when you change the constructer and the old constructor is no longer valid.

 

 

Spread the love

Leave a Reply

Theme by Anders Norén