Skip to content

Instantly share code, notes, and snippets.

@semlinker
Created October 3, 2022 13:56
Show Gist options
  • Save semlinker/7fa005fad45290b8c4baf8de840af9bc to your computer and use it in GitHub Desktop.
Save semlinker/7fa005fad45290b8c4baf8de840af9bc to your computer and use it in GitHub Desktop.
Design Patterns: Builder Pattern in TypeScript
class UserBuilder {
public username!: string;
public sex!: string;
public age!: number;
public photo!: string;
public email!: string;
setUserName(name: string) {
this.username = name;
return this;
}
setSex(sex: string) {
this.sex = sex;
return this;
}
setAge(age: number) {
this.age = age;
return this;
}
setPhoto(photo: string) {
this.photo = photo;
return this;
}
setEmail(email: string) {
this.email = email;
return this;
}
build() {
return new User(this.username, this.sex, this.age, this.photo, this.email);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment