Skip to content

implementation of builder pattern in typescript #2

@vijayswarnkar21

Description

@vijayswarnkar21

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions