Skip to main content

Refactoring Task

Overview

This task addresses a report generation process in which common logic (e.g., start/end processing)
and format-specific logic (PDF/CSV/HTML output) are tightly coupled and written together.
The goal is to improve reusability of shared steps and extensibility of format-specific behavior
by applying appropriate structural refactoring.

Initial Code

The following code generates reports in PDF, CSV, and HTML formats.
However, the format-specific logic is mixed directly with the shared steps (e.g., logging),
making the code hard to maintain and extend.

class ReportGenerator {
generatePdfReport(data: string) {
console.log("=== レポート開始 ===");
console.log(`[PDF形式] ${data}`);
console.log("=== レポート終了 ===");
}

generateCsvReport(data: string) {
console.log("=== レポート開始 ===");
console.log(`[CSV形式] ${data}`);
console.log("=== レポート終了 ===");
}

generateHtmlReport(data: string) {
console.log("=== レポート開始 ===");
console.log(`<html><body>${data}</body></html>`);
console.log("=== レポート終了 ===");
}
}

Question 1: What are the structural problems in this code?

Consider the following aspects when listing the issues:

  • Common logic is duplicated across multiple methods
  • Format-specific behavior is mixed with shared control flow
  • High modification cost when adding new formats
  • Hard to isolate and reuse shared steps for testing or maintenance

Question 2: How can this structure be improved for better flexibility and maintainability?

Outline your proposal considering these points:

  • How can shared logic (pre/post) be separated from variable logic?
  • How can the variable logic be localized, and common logic centralized?
  • What structure supports a template + plugin-style composition?

Example: Candidate Design Patterns

Pattern NamePurpose and Effect
Template MethodCentralizes shared behavior in a base class, allowing subclasses to override parts
StrategyDelegates format-specific logic to interchangeable objects
BridgeSeparates abstraction from implementation for flexible format-function combinations
FlyweightShares common state/data to reduce memory usage and instance cost
PrototypeAllows pre-configured output logic to be cloned and customized

Optional Extensions

  • How would the design scale if new formats like Markdown, JSON, or Excel are added?
  • If post-processing (e.g., save, compress, send) needs to be inserted,
    which structure is most adaptable for such extensions?

Suggested Output Format (for review or team discussion)

  • List of structural issues (at least 3)
  • Refactoring strategy and reasoning for selected patterns
  • Overview of the improved structure (abstraction of output flow, use of inheritance or delegation, etc.)