Strategy Pattern for Document Generation¶
The Strategy Pattern for Document Generation is an implementation approach that separates the logic for creating and combining different file formats—such as CSV and PDF—into distinct, interchangeable strategy classes^[001-TODO__code_copy.md]. This design allows the system to handle multiple document types through a common interface without modifying the core business logic^[001-TODO__code_copy.md].
Core Interface¶
The pattern relies on a ReportDocumentService interface which defines the contract for all document strategies^[001-TODO__code_copy.md]. This interface mandates three key capabilities for implementing classes^[001-TODO__code_copy.md]:
- Acceptance Check (
accept): Determines if the strategy implementation can handle the specificReportDocumentType(e.g., CSV or PDF)^[001-TODO__code_copy.md]. - Document Creation (
makeDocument): Generates the file content as a byte array from provided titles and data rows^[001-TODO__code_copy.md]. - Document Combination (
combineDocument): Merges a list of existing document byte arrays into a single file^[001-TODO__code_copy.md].
Concrete Implementations¶
CSV Strategy¶
The CSVDocumentServiceImpl class implements the strategy for CSV files^[001-TODO__code_copy.md]. Its responsibilities include:
- Validating input data and ensuring column titles match the data structure^[001-TODO__code_copy.md].
- Formatting data rows into comma-separated strings, including specific type handling for objects like
OffsetDateTime^[001-TODO__code_copy.md]. - Merging multiple CSV files by joining their content and removing duplicate headers from subsequent files^[001-TODO__code_copy.md].
PDF Strategy¶
The PDFDocumentServiceImpl acts as a placeholder for PDF generation^[001-TODO__code_copy.md]. Currently, the implementation throws an UnsupportedOperationException for both creation and combination methods, indicating that this feature is not yet supported^[001-TODO__code_copy.md].
Integration with File Storage¶
These strategies integrate with a file management service (FileManageService) to handle the storage and retrieval of generated documents^[001-TODO__code_copy.md]. This service manages paths based on the ReportDocumentType and interacts with the underlying storage system (e.g., Google Cloud Storage) to save the byte arrays produced by the strategies^[001-TODO__code_copy.md].
Related Concepts¶
- [[Strategy Pattern]]
- [[File Management Service]]
- [[Report Domain Service]]
Sources¶
001-TODO__code_copy.md