Skip to content

Strategy pattern for document type handling

The Strategy pattern for document type handling is a design implementation used to dynamically handle different document file types within a reporting system^[001-todo-code.md]. By defining a common interface and implementing specific strategies for each file type, the system can manage document generation and combination logic in a modular and extensible way^[001-todo-code.md].

Core Architecture

The implementation relies on the ReportDocumentService interface^[001-todo-code.md].

Service Interface

The ReportDocumentService interface defines the contract for all document handling strategies^[001-todo-code.md]. It requires implementations to provide methods for:

  • Validation: Checking if the implementation accepts a specific ReportDocumentType^[001-todo-code.md].
  • Generation: Creating a document byte array from titles and data rows^[001-todo-code.md].
  • Combination: Merging multiple document byte arrays into a single file^[001-todo-code.md].

Context Selection

The domain service (ReportDomainServiceImpl) acts as the context that utilizes these strategies^[001-todo-code.md]. It resolves the appropriate concrete implementation by iterating through an injected collection of services (Set<ReportDocumentService>)[[001-todo-code.md#L50-51],[001-todo-code.md#L70-74]]. The selection logic delegates to the accept method defined in the interface^[001-todo-code.md].

If no matching service is found for a given ReportDocumentType, the system throws an UnsupportedOperationException^[001-todo-code.md].

Supported Strategies

CSV Strategy

The CSVDocumentServiceImpl class handles the generation and merging of Comma Separated Value (CSV) files^[001-todo-code.md].

  • Validation: Accepts types matching ReportDocumentType.CSV^[001-todo-code.md].
  • Generation: It transforms a list of titles and rows into a CSV formatted byte array^[001-todo-code.md]. It handles specific data type formatting, such as OffsetDateTime, and validates that the titles match the row data structure^[001-todo-code.md].
  • Combination: It combines multiple CSV byte arrays. To prevent duplicate headers, it strips the title row from subsequent documents before appending them to the first one^[001-todo-code.md].

PDF Strategy

The PDFDocumentServiceImpl is a placeholder strategy for Portable Document Format (PDF) files^[001-todo-code.md].

  • Validation: Accepts types matching ReportDocumentType.PDF^[001-todo-code.md].
  • Generation: Currently throws an UnsupportedOperationException indicating support is not yet implemented^[001-todo-code.md].
  • Combination: Similarly throws an UnsupportedOperationException^[001-todo-code.md].

Document Type Enumeration

The valid document types are defined in the ReportDocumentType enum^[001-todo-code.md]. This enum encapsulates metadata required for file handling, such as:

  • Path: The storage path prefix (e.g., /doc/report/csv)^[001-todo-code.md].
  • Suffix: The file extension (e.g., .csv)^[001-todo-code.md].
  • [[Strategy pattern]]
  • [[Factory pattern]]
  • [[CSV]]
  • [[PDF]]
  • [[Document generation]]

Sources

^[001-todo-code.md]