Builder Design Patter

Ever found yourself dealing with a class that has numerous fields, but not all are mandatory during object creation? ?? Handling this scenario can be tricky.

Let’s see how Builder Design Pattern will help us to overcome this problem.

Here's the problem:?

  • Multiple constructors? Not a clean solution.?
  • One constructor with all fields, passing nulls? Hard to remember and maintain.

Create a nested static builder class with a constructor for mandatory fields and setters for non-mandatory ones. A build() method will return the main class object.

Code:

package designpatterns.builderpattern;

public class Person {

	/*

	 * mandatory fields

	 */

	private String firstName;

	private String lastName;

	private int age;

	private String email;

	/*

	 * non-mandatory fields

	 */

	private String middleName;

	private boolean student;

	public String getFirstName() {

		return firstName;

	}

	public String getLastName() {

		return lastName;

	}

	public int getAge() {

		return age;

	}

	public String getEmail() {

		return email;

	}

	public String getMiddleName() {

		return middleName;

	}

	public boolean isStudent() {

		return student;

	}

	private Person(PersonBuilder personBuilder) {

		this.firstName = personBuilder.firstName;

		this.lastName = personBuilder.lastName;

		this.middleName = personBuilder.middleName;

		this.age = personBuilder.age;

		this.email = personBuilder.email;

		this.student = personBuilder.student;

	}

	@Override

	public String toString() {

		return "Person [firstName=" + firstName + ", middleName=" + middleName + ", lastName=" + lastName + ", age="

				+ age + ", email=" + email + ", student=" + student + "]";

	}

	/**

	 * Builder class for Person

	 */

	public static class PersonBuilder {

		private String firstName;

		private String lastName;

		private int age;

		private String email;

		private String middleName;

		private boolean student;

		/*

		 * Builder constructor for mandatory fields

		 */

		public PersonBuilder(String firstName, String lastName, int age, String email) {

			super();

			this.firstName = firstName;

			this.lastName = lastName;

			this.age = age;

			this.email = email;

		}

		public PersonBuilder setMiddleName(String middleName) {

			this.middleName = middleName;

			return this;

		}

		public PersonBuilder setStudent(boolean student) {

			this.student = student;

			return this;

		}

		/*

		 * build() method to create the object of Person class.

		 */

		public Person build() {

			return new Person(this);

		}

	}

}        
package designpatterns.builderpattern;

public class Main {

	public static void main(String[] args) {

		Person person = new Person.PersonBuilder("Abc", "Pqr", 26, "[email protected]").setStudent(true).build();

		System.out.println(person.toString());

	}

}        

Output:

Person [firstName=Abc, middleName=null, lastName=Pqr, age=26, [email protected], student=true]


Note:?

  • Perfect for immutable objects – once values are set, they stay that way.?
  • The length of code will increase with the nested builder class.

要查看或添加评论,请登录

Nakul Mitra的更多文章

社区洞察

其他会员也浏览了