Builder Pattern trong Java/Javascript
Trong nhiều ngôn ngữ lâp trình thì Constructor giúp chúng ta truyền tham số để tạo 1 object mới. Nhưng vấn đề xảy ra là:
- Nếu object thay đổi liên tục (thêm hoặc xóa bớt thuộc tính) thì constructor ta cũng phải đổi lại
- object cần nhiều constructor có tham số đầu vào khác nhau
Việc này dẫn đến code của chúng ta dễ phải thay đổi liên tục. Để khắc phục nhược điểm này Builder Pattern ra đời
Với Builder Pattern thì việc tạo 1 instance object trở nên dễ dàng hơn, ngắn gọn hơn và tùy biến được nhiều hơn
Ví dụ ta có 1 class Employee như sau:
package com.entity;
public class Employee {
private Long id;
private String name;
private Long departmentId;
// Generate getter and setter
}
Ví dụ ta muốn tạo 2 Employee:
- id = 1, name = Nguyễn Văn A
- id = 2, name = Nguyễn Văn B, departmentId = 10
Employee employee1 = new Employee();
employee1.setId(1L);
employee1.setName("Nguyễn Văn A");
Employee employee2 = new Employee();
employee2.setId(2L);
employee2.setName("Nguyễn Văn B");
employee2.setDepartmentId(10L);
package com.entity;
public class Employee {
private Long id;
private String name;
private Long departmentId;
public Employee(Long id, String name) {
this.id = id;
this.name = name;
}
public Employee(Long id, String name, Long departmentId) {
this.id = id;
this.name = name;
this.departmentId = departmentId;
}
// Generate getter and setter
}
Employee employee1 = new Employee(1L, "Nguyễn Văn A");
Employee employee2 = new Employee(2L, "Nguyễn Văn B", 10L);
Thay vào đó ta có cách đơn giản hơn đó là dùng builder
Trong java thì tạo và sử dụng builder rất đơn giản và ít thao tác code vì đã có lombok với @Builder
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<scope>provided</scope>
</dependency>
package com.entity;
import lombok.Builder;
@Builder
public class Employee {
private Long id;
private String name;
private Long departmentId;
}
Employee employee1 = Employee.builder().id(1L).name("Nguyễn Văn A").build();
Employee employee2 = Employee.builder().id(2L).name("Nguyễn Văn B").departmentId(10L).build();
Rất ngắn gọn và hiệu quả. Kể cả khi ta thêm các thuộc tính vào Employee thì cũng có thể tạo 1 instance với các tham số đơn giản
function Employee() {
this.id;
this.name;
this.departmentId;
};
function EmployeeBuilder() {
this.setId = function(id){
this.id = id;
return this;
};
this.setName = function(name){
this.name = name;
return this;
};
this.setDepartmentId = function(departmentId){
this.departmentId = departmentId;
return this;
};
this.build = function(){
let e = new Employee();
e.id = this.id;
e.name = this.name;
e.departmentId = this.departmentId;
return e;
};
return this;
};
Ở đây ta phải thêm tiền tố set (tùy các bạn đặt miễn sao không trùng với tên property như id, name, departmentId) cho các hàm builder của property để tránh trùng với tên property trong function EmployeeBuilder
let builder = EmployeeBuilder();
let employee1 = builder.setId(1).setName('Nguyễn Văn A').build();
let employee2 = builder.setId(20).setName('Nguyễn Văn B').setDepartmentId(10).build();
console.log('Employee: ', {employee1, employee2});
Nhận xét
Đăng nhận xét