Builder mode, similar to Viewholder construction, convenient for external class parameter setting

Source: Internet
Author: User
Tags constructor static class

What is Builder mode? By searching, you will find that most of the definitions on the web are separating the construction of a complex object from its representation, so that the same build process can create different representations.


But after reading this definition, there is no egg, and you still don't know what the builder design pattern is. In this person's attitude is to learn the design pattern of this thing, do not over-care about its definition, the definition is often more abstract, the best example of learning it is through the sample code.


We use an example to elicit the builder pattern. Suppose there is a person class, and we build a large number of people through that class, which has many attributes, most commonly such as name,age,weight,height and so on, and we allow these values not to be set, that is, to allow NULL, The definition of this class is as follows.


public class Person {
private String name;
private int age;
private double height;
private double weight;
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public double getheight () {
return height;
}
public void SetHeight (double height) {
This.height = height;
}
Public double getweight () {
return weight;
}
public void Setweight (double weight) {
This.weight = weight;
} }


Then we might define a construction method for convenience.


Public person (String name, Int. age, double height, double weight) {
THIS.name = name;
This.age = age;
This.height = height;
This.weight = weight; }


Perhaps for the convenience of the new object, you will also define an empty construction method:


Public person () {
    }


Even when you're lazy and just want to pass some of the parameters, you'll define a similar construction method as follows.


Public person (String name) {
this.name = name; }
Public person (String name, int age) {
THIS.name = name;
This.age = age; }
Public person (String name, Int. age, double height) {
THIS.name = name;
This.age = age;
this.height = height; }


So you can create all the objects you want.


Person P1=new person ();
Person P2=new person ("Zhang San");
Person P3=new person ("John Doe",);
Person P4=new person ("Harry", 21,180);
Person P5=new person ("Zhao Liu", 17,170,65.4);


Can imagine the disadvantage of such a creation, the most intuitive is the four parameters of the constructor of the last side of the two parameters exactly what meaning, not good readability, if you do not click to see the source, the ghost knows which is weight which is the height. Another problem is that when there are a lot of parameters, writing this constructor will be very troublesome, at this point, if you change the angle, try the builder mode, you will find that the readability of the code went up suddenly.


We add a static inner class builder class to person and modify the constructor for the person class, as shown in the code below.


public class Person {
private String name;
private int age;
private double height;
private double weight;
Privateperson (Builder builder) {
This.name=builder.name;
This.age=builder.age;
This.height=builder.height;
This.weight=builder.weight;
}
Public String GetName () {
return name;
}
public void SetName (String name) {
THIS.name = name;
}
public int getage () {
return age;
}
public void Setage (int.) {
This.age = age;
}
Public double getheight () {
return height;
}
public void SetHeight (double height) {
This.height = height;
}
Public double getweight () {
return weight;
}
public void Setweight (double weight) {
This.weight = weight;
}

Static Class builder{
private String name;
private int age;
private double height;
private double weight;
Public Builder name (String name) {
This.name=name;
return this;
}
Public Builder Age (int.) {
This.age=age;
return this;
}
Public Builder height (double height) {
This.height=height;
return this;
}
Public Builder weight (double weight) {
This.weight=weight;
return this;
}
Public person build () {
return new person (this);
}
} }


As we can see from the code above, we define a variable in the builder class that is exactly the same as the person class, set the property value through a series of member functions, but the return value is this, which is the builder object. Finally, a build function is provided to create the person object, which is returned by the person object, and the corresponding constructor is defined in the person class, that is, the constructor's entry is the Builder object, and then the member variable is assigned to its own value. The corresponding value is the value in the Builder object. In addition, the function of member functions in the Builder class to return the builder object itself is to allow it to support chained calls, which greatly enhances the readability of the code.


So we can create the person class like this.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.