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
| // Clean: creation moved to a factory method | |
| public abstract class ReportTemplate { | |
| // Template Method — unchanged | |
| public final void generateReport() { | |
| Report report = createReport(); // Factory Method | |
| report.generateHeader(); | |
| report.generateBody(); | |
| report.generateFooter(); | |
| } | |
| // Factory Method (variation point) |
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
| // Smelly: Template Method indirectly creates concrete objects (bad!) | |
| public abstract class ReportTemplate { | |
| // Template Method — fixed algorithm | |
| public final void generateReport(String role) { | |
| // BAD: creation hidden inside Template Method through a private helper | |
| Report report = createReportWrong(role); | |
| report.generateHeader(); // fixed |