Last active
January 29, 2022 05:34
-
-
Save hallojoe/b5503d88853a88564a87ae227ff1fb6e to your computer and use it in GitHub Desktop.
Fluent builder pattern in TypeScript
This file contains hidden or 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
interface IBuilder<T> { | |
build(): T | |
} | |
interface IValueModel { | |
name: string | |
date: Date | |
} | |
class FluentValueModelBuilder implements IBuilder<IValueModel> { | |
public _valueModel: IValueModel = { | |
date: new Date(), | |
name: "Undfined" | |
} | |
public build(): IValueModel { | |
return this._valueModel | |
} | |
public setName(name: string): this { | |
this._valueModel.name = name | |
return this | |
} | |
public setDate(date: Date): this { | |
this._valueModel.date = date | |
return this | |
} | |
} | |
const result = new FluentValueModelBuilder() | |
.setDate(new Date()) | |
.setName("Gilfoyle") | |
.build() | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment