Created
March 22, 2025 11:50
-
-
Save trikitrok/f017f370a97bc284a3ccba317a527c32 to your computer and use it in GitHub Desktop.
Sprout Class 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 and determine the sprouted class interface | |
export class QuarterlyReportGenerator { | |
public readonly beginDate: Date; | |
public readonly endDate: Date; | |
public readonly database: Database; | |
constructor() { | |
// constructor with multiple side effects | |
} | |
public generate(): string { | |
const results = this.database.queryResults(this.beginDate, this.endDate); | |
let pageText = "<html><head><title>" + | |
"Quarterly Report" + | |
"</title></head><body><table>"; | |
// <-- this is the change point | |
if (results.length !== 0) { | |
for (const result of results) { | |
pageText += "<tr>"; | |
pageText += `<td>${result.department}</td>`; | |
pageText += `<td>${result.manager}</td>`; | |
pageText += `<td>${result.netProfit}</td>`; | |
pageText += `<td>${result.operatingExpense}</td>`; | |
pageText += "</tr>"; | |
} | |
} else { | |
pageText += "No results for this period"; | |
} | |
pageText += "</table>"; | |
pageText += "</body>"; | |
pageText += "</html>"; | |
return pageText; | |
} | |
} | |
////////////////////////////////////////////////// | |
// Step 2: Develop the sprouted class with tests | |
export class QuarterlyReportTableHeaderProducer { | |
public makeHeader(): string { | |
return "<tr><td>Department</td><td>Manager</td>" + | |
"<tr><td>Profit</td><td>Expenses</td>"; | |
} | |
} | |
//////////////////////////////////////////////////// | |
// Step 3: Instance the sprout class and call its method from the source method | |
export class QuarterlyReportGenerator { | |
// rest of the code omitted for the sake of brevity... | |
public generate(): string { | |
const results = this.database.queryResults(this.beginDate, this.endDate); | |
let pageText = "<html><head><title>" + | |
"Quarterly Report" + | |
"</title></head><body><table>"; | |
const producer = new QuarterlyReportTableHeaderProducer(); // <-- call to the sprouted class | |
pageText += producer.makeHeader(); | |
if (results.length !== 0) { | |
for (const result of results) { | |
pageText += "<tr>"; | |
pageText += `<td>${result.department}</td>`; | |
pageText += `<td>${result.manager}</td>`; | |
pageText += `<td>${result.netProfit}</td>`; | |
pageText += `<td>${result.operatingExpense}</td>`; | |
pageText += "</tr>"; | |
} | |
} else { | |
pageText += "No results for this period"; | |
} | |
pageText += "</table>"; | |
pageText += "</body>"; | |
pageText += "</html>"; | |
return pageText; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment