Besides already described ways there are two other ways I just wanted to mention:
- Static factory methods:
export class Lifter {
id: string;
name: string;
weightclass: number;
bestSnatch: number;
bestCJ: number;
bestTotal: number;
bio: string = "Default biography text";
imageURL: string = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Arh-avatar.jpg";
private constructor(id: string, name: string, weightclass:number, bestSnatch:number, bestCJ:number, bestTotal:number) {
this.id = id;
this.name = name;
this.weightclass = weightclass;
this.bestSnatch = bestSnatch;
this.bestCJ = bestCJ;
this.bestTotal = bestTotal;
}
static create(id: string, name: string, weightclass:number, bestSnatch:number, bestCJ:number, bestTotal:number, bio: string, imageURL: string): Lifter {
return new Lifter(id, name, weightclass, bestSnatch, bestCJ, bestTotal);
}
static createWithDefaultBioAndImageURL(id: string, name: string, weightclass:number, bestSnatch:number, bestCJ:number, bestTotal:number): Lifter {
return new Lifter(id, name, weightclass, bestSnatch, bestCJ, bestTotal);
}
static createWithDefaultBio(id: string, name: string, weightclass:number, bestSnatch:number, bestCJ:number, bestTotal:number, imageURL: string): Lifter {
const lifter = new Lifter(id, name, weightclass, bestSnatch, bestCJ, bestTotal);
lifter.imageURL = imageURL;
return lifter;
}
static createWithDefaultImageURL(id: string, name: string, weightclass:number, bestSnatch:number, bestCJ:number, bestTotal:number, bio: string): Lifter {
const lifter = new Lifter(id, name, weightclass, bestSnatch, bestCJ, bestTotal);
lifter.bio = bio;
return lifter;
}
}
const lifter1 = Lifter.create("3","Manfred",73,107,125,220,"sampleBio", "https://www.w3schools.com/howto/img_avatar2.png");
const lifter2 = Lifter.createWithDefaultBio("3","Manfred",73,107,125,220,"https://www.w3schools.com/howto/img_avatar2.png");
const lifter3 = Lifter.createWithDefaultImageURL("3","Manfred",73,107,125,220,"sampleBio");
const lifter4 = Lifter.createWithDefaultBioAndImageURL("3","Manfred",73,107,125,220);
Example here
- Builder pattern
It can be implemented in several different ways, below is just sample one but you can also modify it in some ways, e.g. Lifter class could accept only LifterBuilder in constructor as a parameter and grab parameters from it - this way you could prevent initialization of this class without builder.
The exact implementation depends on your needs and preferences.
export class Lifter {
id: string;
name: string;
weightclass: number;
bestSnatch: number;
bestCJ: number;
bestTotal: number;
bio: string = "Default biography text";
imageURL: string = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Arh-avatar.jpg";
constructor(id: string, name: string, weightclass:number, bestSnatch:number, bestCJ:number, bestTotal:number, bio: string, imageURL: string) {
this.id = id;
this.name = name;
this.weightclass = weightclass;
this.bestSnatch = bestSnatch;
this.bestCJ = bestCJ;
this.bestTotal = bestTotal;
}
}
export class LifterBuilder {
id: string;
name: string;
weightclass: number;
bestSnatch: number;
bestCJ: number;
bestTotal: number;
bio: string = "Default biography text";
imageURL: string = "https://upload.wikimedia.org/wikipedia/commons/a/a0/Arh-avatar.jpg";
constructor(id: string, name: string, weightclass:number, bestSnatch:number, bestCJ:number, bestTotal:number) {
this.id = id;
this.name = name;
this.weightclass = weightclass;
this.bestSnatch = bestSnatch;
this.bestCJ = bestCJ;
this.bestTotal = bestTotal;
}
withBio(bio: string): LifterBuilder {
this.bio = bio;
return this;
}
withImageURL(imageURL: string): LifterBuilder {
this.imageURL = imageURL;
return this;
}
build(): Lifter {
return new Lifter(this.id, this.name, this.weightclass, this.bestSnatch, this.bestCJ, this.bestTotal, this.bio, this.imageURL);
}
}
const lifter = new LifterBuilder(...).withBio(...).withImageURL(...).build(); // just call withBio or withImageURL when you need them and don't call any of them if you don't want to change default values