Last active
March 21, 2025 00:19
-
-
Save trikitrok/183036ea5f63312128fdff00e561fa1f to your computer and use it in GitHub Desktop.
Extract and Override Call example
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
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