Java Design Patterns: Builder Patterns

Keywords: Java

First, why use Builder mode

Builder mode is designed to make up for the shortcomings of Java language design. Let's look at a chestnut together.

public class User{

    String name;
    int age;
    String email;
    String address;

    public User(){
    }
    
    //Constructors that want names and mailboxes
    public User(String name, String email){
        this.name = name;
        this.email = email;
    }

    //Constructors that want names and addresses
    public User(String name, String address){
        this.name = name;
        this.address = address;
    }
}

In the above code, it is easy to find that, under our normal requirements, Java constructor writing will be a problem, because the number of parameters and type can not constitute overload, so it is not possible to write, so the Builder pattern is to solve this situation.

2. Realization of Builder Model

Let's take a look at how the Builder pattern is written:

public class User {

	String name;
	int age;
	String phone;
	String email;
	String address;

    //Note that parametric constructors are private, and avoid external use of constructors to create User objects
	private User() {
	}

	@Override
	public String toString() {
		return "User [name=" + name + ", age=" + age + ", phone=" + phone + ",                 
                email=" + email + ", address=" + address
				+ "]";
	}

	public static class Builder {

		private String name;
		private int age;
		private String phone;
		private String email;
		private String address;

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

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

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

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

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

		public User build() {
			User user = new User();
			user.name = name;
			user.age = age;
			user.email = email;
			user.phone = phone;
			user.address = address;
			return user;
		}
	}
}

From the above code, we can see that it is to create an internal class within User, and have the same fields (attributes) as User, and provide SET methods. The most important thing is to provide a method (build) that can return User objects, so that User objects can be created through Builder.

Another advantage of the Builder design pattern is that we can combine input parameters at will, not only to avoid overloading errors, but also to write too many constructors.

Let's look at how to call after we have written the Builder schema class:

public class UserTest {
	
	public static void main(String[] args) {
		User u = new User.Builder().setName("bob").setAge(22).build();
		System.out.println(u);
	}
	
}

It is not difficult to find that we are the object of the Builder class under a new User, and after setting various parameters, we call the method that returns the User object and finally realize the creation of the User method. This is the Java Builder design pattern, is it simple? get up quickly!!

 

 

This blog is a note of my experience in learning Java. If there are any mistakes, please point them out in the comments. Thank you very much!! ___________

Posted by MockY on Tue, 14 May 2019 09:14:28 -0700