Created
October 3, 2022 13:56
-
-
Save semlinker/7fa005fad45290b8c4baf8de840af9bc to your computer and use it in GitHub Desktop.
Design Patterns: Builder Pattern in TypeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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