Created
March 22, 2025 12:24
-
-
Save trikitrok/31aeecfd652e21ba254cdce8a0631fb8 to your computer and use it in GitHub Desktop.
Wrap Method version 2 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
// We also want to keep the original behavior (paying without logging) | |
////////////////////////////////////////////////// | |
// Step 1: Identify the change point | |
export class Employee { | |
public timeCards: TimeCard[]; | |
public payPeriod: Date[]; | |
public date: Date; | |
public payRate: number; | |
public payDispatcher: PayDispatcher; | |
public pay(): void { | |
const amount = new Money(); | |
for (const card of this.timeCards) { | |
if (this.payPeriod.includes(this.date)) { | |
amount.add(card.hours * this.payRate); | |
} | |
} | |
this.payDispatcher.pay(this, this.date, amount); | |
// <-- this is the change point | |
} | |
} | |
////////////////////////////////////////////////// | |
// Step 2: Develop a method for the new behaviour with tests, (logPayment in this case) | |
export class Employee { | |
// rest of the code omitted for the sake of brevity... | |
public logPayment(logger: Logger): void { | |
// code for log payment | |
} | |
} | |
////////////////////////////////////////////////// | |
// Step 3: Create a new pubic method for the cpmposed behaviour, (makeLoggedPayment in this case) | |
export class Employee { | |
// rest of the code omitted for the sake of brevity... | |
public makeLoggedPayment(): void { // <- new method composing old and new behaviors | |
this.pay(); | |
this.logPayment(logger); | |
} | |
public pay(): void { | |
const amount = new Money(); | |
for (const card of this.timeCards) { | |
if (this.payPeriod.includes(this.date)) { | |
amount.add(card.hours * this.payRate); | |
} | |
} | |
this.payDispatcher.pay(this, this.date, amount); | |
} | |
public logPayment(logger: Logger): void { | |
// log employee data | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment