Using Builder to create object sample code

  • 2021-11-01 02:47:48
  • OfStack

Preface

When creating an object requires initialization data, data parameters are not easy to distinguish, and can be transmitted or not transmitted, you can consider using Builder construction method to create it. Every time I see someone else writing to call in Bulder way, I think so cool, so I will create objects in Builder way myself.

The following words are not much to say, let's take a look at the detailed introduction

Now you want to enter an example of the basic information of a Series 1 person:

Create an Person class


public class Person {
private String name;
private int age;
private float height;
private float weight;

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

public String getName() {
return name;
}

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

public int getAge() {
return age;
}

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

public float getHeight() {
return height;
}

public void setHeight(float height) {
this.height = height;
}

public float getWeight() {
return weight;
}

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

Then this is the case with creating objects, and the following parameters are not easy to see what they represent and are not readable


new Person(" Von Timo ",18,150,43);
new Person(" Wen Wan ",17,164,48);

Modification with Builder mode

Create an Builder class, attribute and Person class, plus SetXxx () method


static class Builder{
private String name;
private int age;
private float height;
private float weight;

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

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

public void setHeight(float height) {
this.height = height;
}

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

The construction method of Person is changed to pass in Builder object, and the attribute values of Builder object are assigned to Person object


public Person(Builder builder){
this.name = builder.name;
this.age = builder.age;
this.height = builder.height;
this.weight = builder.weight;
}

To transform the Builder class, the key step is to return each set method to the Builder class object, so that you can continue to call the set method happily and continuously, and finally call the build () creation method to return the Person object.


static class Builder{
private String name;
private int age;
private float height;
private float weight;

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

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

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

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

public Person build(){
return new Person(this);
}
}

Change the return value type of each set method to Builder, and return an builder object each time, so that the set method can be called in a continuous chain.

The build () method creates an person object and calls the parametric constructor of Person, assigning the properties of the builder object to the person object in turn. The attribute values in person are the values of the set method that are called in a chain.

Creating an Person object using the Builder schema makes it clear that the code is much more readable. Note: The Builder creation method is usually used when there are few objects created.


Person person = new Person.Builder()
.setName(" Brother Li ")
.setAge(20)
.setHeight(162)
.setWeight(45)
.build();

Summarize


Related articles: