Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementation of builder pattern in typescript #2

Open
vijayswarnkar21 opened this issue Sep 25, 2022 · 1 comment
Open

implementation of builder pattern in typescript #2

vijayswarnkar21 opened this issue Sep 25, 2022 · 1 comment

Comments

@vijayswarnkar21
Copy link

vijayswarnkar21 commented Sep 25, 2022

Hi,
implementation of builder pattern in TS looks like -

export class Student {
name: String;
age: Number;

constructor(studentBuilder: StudentBuilder) {
    this.name = studentBuilder.name;
    this.age = studentBuilder.age;
}

}

export class StudentBuilder {
private _name: String = "";
private _age: Number = 0;

constructor(){
    
}

setName(name: String) {
    this._name = name
    return this
}

setAge(age: Number) {
    this._age = age
    return this
}

get name(): String {
    return this._name
}

get age(): Number {
    return this._age
}

build() {
    return new Student(this);
}

}

==================================================
client code:
const student = new StudentBuilder().setName("vijay").setAge(29).build()

======================================================

but in java it is some what different.

or there is better way to implementing it.

@vijayswarnkar21
Copy link
Author

Here object is not immutable

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant