Skip to content

Instantly share code, notes, and snippets.

@trikitrok
Last active March 21, 2025 00:19
Show Gist options
  • Save trikitrok/183036ea5f63312128fdff00e561fa1f to your computer and use it in GitHub Desktop.
Save trikitrok/183036ea5f63312128fdff00e561fa1f to your computer and use it in GitHub Desktop.
Extract and Override Call example
class Account {
private balance: number;
private readonly logger: AcmeLogger;
constructor(balance: number) {
this.balance = balance;
this.logger = new AcmeLogger();
}
public withdraw(value: number): void {
this.balance += value;
this.logger.logTransaction(new Date(), -value); // <- the call to the awkward dependency
}
}
//////////////////////////////////////////////
// After extracting the call to the awkward dependency to a new method
class Account {
private balance: number;
private readonly logger: AcmeLogger;
constructor(balance: number) {
this.balance = balance;
this.logger = new AcmeLogger();
}
public withdraw(value: number): void {
this.balance += value;
this.logWithdraw(value);
}
private logWithdraw(value: number): void {
this.logger.logTransaction(new Date(), -value);
}
}
// Which is the starting point of Subclass & Override Method
// See https://gist.github.com/trikitrok/2909137a57a2d0bb2c2a906bfe657e87
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment