Builder
Builder pattern builds a complex object using simple objects and using a step by step approach. This builder is independent of other objects.
If we want to create the User by only providing name and address, It will become difficult to dodge the unwanted arguments like age and phone.
class Address {
constructor(zip, street) {
this.zip = zip;
this.street = street;
}
}
class User {
constructor(name, age, phone, address) {
this.name = name;
this.age = age;
this.phone = phone;
this.address = address;
}
}
const user = new User(
"name",
undefined,
undefined,
new Address("340", "Laplasa")
);
console.log(user);
// User {
// name: 'name',
// age: undefined,
// phone: undefined,
// address: Address { zip: '340', street: 'Laplasa' }
// }
So all this undefined makes the code junky. To make it clean we can use Builder Pattern and only add the things which we need for the user.
General Builder Design Pattern
class Address {
constructor(zip, street) {
this.zip = zip;
this.street = street;
}
}
class User {
constructor(name) {
this.name = name;
}
}
class UserBuilder {
constructor(name) {
this.user = new User(name);
}
setAge(age) {
this.user.age = age;
return this;
}
setPhone(phone) {
this.user.phone = phone;
return this;
}
setAddress(address) {
this.user.address = address;
return this;
}
build() {
return this.user;
}
}
const user = new UserBuilder("Bob")
.setPhone(100)
.setAddress(new Address(360, "Laplasa"))
.build();
console.log(user);
// User {
// name: 'Bob',
// phone: 100,
// address: Address { zip: 360, street: 'Laplasa' }
// }
JavaScript Focused Builder Design Pattern
class Address {
constructor(zip, street) {
this.zip = zip;
this.street = street;
}
}
class User {
constructor(name, { age = 10, phone, address } = {}) {
this.name = name;
this.age = age;
this.phone = phone;
this.address = address;
}
}
const user = new User("bob", {
phone: 100,
address: new Address(360, "Laplasa"),
});
console.log(user);
// User {
// name: 'bob',
// age: 10,
// phone: 100,
// address: Address { zip: 360, street: 'Laplasa' }
// }