Created
March 22, 2025 11:59
-
-
Save trikitrok/277401189f67c69f3e5bcde016cda46a to your computer and use it in GitHub Desktop.
Wrap Method V1 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
////////////////////////////////////////////////// | |
// 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: Extract a method with the body of the current method | |
export class Employee { | |
public timeCards: TimeCard[]; | |
public payPeriod: Date[]; | |
public date: Date; | |
public payRate: number; | |
public payDispatcher: PayDispatcher; | |
public pay(): void { | |
this.dispatchPayment(); | |
// <-- this is the change point | |
} | |
private dispatchPayment(): 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); | |
} | |
} | |
////////////////////////////////////////////////// | |
// Step 3: Develop a method for the new behaviour with tests | |
export class Employee { | |
// rest of the code omitted for brevity... | |
public logPayment(logger: Logger): void { | |
// code for log payment | |
} | |
} | |
//////////////////////////////////////////////////// | |
// Step 4: Call it from the current method | |
export class Employee { | |
// rest of the code omitted for brevity... | |
public pay(): void { | |
this.dispatchPayment(); | |
this.logPayment(new AcmeLogger()); // <-- call to the new method | |
} | |
private dispatchPayment(): void { | |
// rest of the code omitted for brevity... | |
} | |
public logPayment(logger: Logger): void { | |
// code for log payment | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment