Skip to content

Strategy Pattern for Report Document Types

The Strategy Pattern is employed within the report generation domain to handle different file formats dynamically. This allows the system to support multiple export types (e.g., CSV, PDF) through a common interface without modifying the core domain logic^[001-TODO__code.md].

Core Interface

The behavior of report document generation is defined by the ReportDocumentService interface^[001-TODO__code.md]. This interface enforces a contract for all concrete strategy implementations, ensuring that every document type can be created and combined using a standard API^[001-TODO__code.md].

The interface defines three key methods^[001-TODO__code.md]: * accept(ReportDocumentType): A predicate method used to determine if the specific implementation matches the requested document type^[001-TODO__code.md]. * makeDocument(List titles, List<List<?>> rows): Generates the raw byte content for a document based on provided titles and data rows^[001-TODO__code.md]. * combineDocument(List documents): Merges a list of byte arrays (representing document pages or chunks) into a single downloadable file^[001-TODO__code.md].

Domain Service Orchestration

The ReportDomainServiceImpl acts as the context class in this pattern^[001-TODO__code.md]. It utilizes a Set<ReportDocumentService> to hold all available strategy implementations^[001-TODO__code.md].

When the domain service needs to generate or combine a report, it calls the helper method getReportDocumentService(ReportDocumentType)^[001-TODO__code.md]. This method iterates through the available services and returns the first one where the accept method returns true for the given ReportDocumentType^[001-TODO__code.md].

If no matching strategy is found, the system explicitly throws an UnsupportedOperationException indicating that the specific type has not yet been implemented^[001-TODO__code.md].

Concrete Implementations

The source code provides concrete implementations for specific document types, demonstrating how the strategy pattern isolates format-specific logic.

CSV Strategy

The CSVDocumentServiceImpl implements the strategy for Comma Separated Values^[001-TODO__code.md]. * Logic: It converts lists of titles and rows into a comma-delimited string format^[001-TODO__code.md]. * Combination: When combining multiple CSV byte arrays, it specifically removes the header row from subsequent documents to prevent duplicate titles in the final output^[001-TODO__code.md].

PDF Strategy

The PDFDocumentServiceImpl acts as a placeholder for future functionality^[001-TODO__code.md]. * Status: While it implements the interface and accepts the PDF type, the actual methods currently throw an UnsupportedOperationException^[001-TODO__code.md]. This setup allows the system to recognize the type without requiring the implementation to be complete.

Document Type Enumeration

The strategies are selected based on the ReportDocumentType enumeration^[001-TODO__code.md]. This enum defines not only the type name (e.g., CSV, PDF) but also metadata required by the [[FileManageService]], such as the storage path prefix and the file suffix^[001-TODO__code.md].

  • [[Strategy Pattern]]
  • Design Patterns
  • [[Domain-Driven Design]]
  • [[Report Generation]]

Sources

^[001-TODO__code.md]