Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
How to implement Builder Pattern in Node.js
https://ozenero.com/implement-builder-pattern-nodejs-example
How to implement Builder Pattern in Node.js
When we want to create complicated object which has multiple parts, we can use Builder Pattern that separates the building of these parts. This creational process does not care about how these parts are assembled.
In this tutorial, we're gonna look at 2 ways to implement Builder Pattern in Node.js:
- using Builder function
- using Builder class
Node.js Builder Pattern example using Builder function
Our Builder function works as a Builder object that can build a Person object.
A Person object has 4 fields: name
, age
, location
, languages
.
PersonBuilder.js
function PersonBuilder() {
this.person = {};
this.setName = name => {
this.person.name = name;
return this;
}
this.setAge = age => {
this.person.age = age;
return this;
}
this.setLocation = location => {
this.person.location = location;
return this;
}
this.setLanguages = languages => {
this.person.languages = languages;
return this;
}
this.buildInfo = () => this.person;
}
module.exports = PersonBuilder;
Now look at the code in app.js:
More at:
https://ozenero.com/implement-builder-pattern-nodejs-example
How to implement Builder Pattern in Node.js





