Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Created March 21, 2025 18:31
Show Gist options
  • Save trikitrok/23ae8f61221c7cc5f8cf605247f67569 to your computer and use it in GitHub Desktop.
Save trikitrok/23ae8f61221c7cc5f8cf605247f67569 to your computer and use it in GitHub Desktop.
// a utility class :(
export class BankingServices {
public static updateAccountBalance(userId: number, amount: Money): void {
// some code to update the account balance
}
// more methods...
}
export class User {
private id: number;
constructor(id: number) {
this.id = id;
}
// more code...
public updateBalance(amount: Money): void {
// using the utility class :(
BankingServices.updateAccountBalance(this.id, amount);
}
// more code...
}
//////////////////////////////////////////////
// Introduce instance delegator in the utility class
export class BankingServices {
public static updateAccountBalance(userId: number, amount: Money): void {
// some code to update the account balance
}
//!! the instance delegator <- now we can Subclass & Override Method here!
public updateBalance(userId: number, amount: Money): void {
// it calls the static method
BankingServices.updateAccountBalance(userId, amount);
}
// more methods...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment