Downloading Data in Different Formats Using Factory Design Pattern

Estimated read time 15 min read

Downloading Data in Different Formats Using Factory Design Pattern

In real projects, users often need the ability to download business data (e.g., sales orders, customer data, or flight bookings) in different formats like TXT, PDF, DOC.

If we try to achieve this in a single report with IF/ELSE or CASE statements, the code quickly becomes messy and hard to maintain. Every new requirement (e.g., CSV, JSON, XML) would require changing the program logic.

This is where the Factory Design Pattern helps.

How the Factory Design Pattern Works Here–

Interface (ZIF_FILE_DOWNLOADER)Defines the common contract: one method DOWNLOAD_FILE.Ensures all file downloaders behave the same way from the outside.Concrete Classes (TXT, DOC, PDF, XLSX Downloaders)Each class implements the interface and contains logic specific to that file type.Example:TXT Uses GUI_DOWNLOAD with ASCII format.DOC Uses GUI_DOWNLOAD with linefeeds.PDF Uses CL_APOC_PDF_GENERATOR and binary download.XLSX Uses CL_FDT_XL_SPREADSHEET to generate Excel.Factory Class (ZCL_FILE_FACTORY)Acts as a central decision-maker.Based on user selection (TXT, DOC, PDF, XLSX), it instantiates the correct class.The main program never directly knows which class it is using — it only calls the common method.Report Program (ZDEMO6)Reads user input (p_type).Calls the factory to get the right downloader object.Invokes DOWNLOAD_FILE, which internally runs the format-specific logic.

Flow of Execution–

User selects file type on selection screen.Report calls factory to get the correct downloader.Factory returns the right class instance (TXT/PDF/DOC/XLSX).Report calls DOWNLOAD_FILE method (same for all).File gets saved on the user’s local system in the chosen format.

Benefits of Using Factory Pattern–

Scalability – Easy to add new file formats without modifying existing code.

Maintainability – Each file logic is isolated in its own class.

Reusability – The same design can be reused in multiple applications (ALV exports, reports, integration).

Clean Code – Avoids long IF/ELSE or CASE blocks.

REPORT zdemo6.

*———————————————————————-
* Interface for File Downloaders
*———————————————————————-

INTERFACE zif_file_downloader.
METHODS:
download_file
IMPORTING it_data TYPE STANDARD TABLE.
ENDINTERFACE.

CLASS zcl_txt_downloader DEFINITION.
PUBLIC SECTION.
INTERFACES zif_file_downloader.
ENDCLASS.

*———————————————————————-
* TXT File Downloader Implementation
*———————————————————————-

CLASS zcl_txt_downloader IMPLEMENTATION.
METHOD zif_file_downloader~download_file.
DATA: lv_filename TYPE string VALUE ‘C:tempNew folder1’.

” Download using classic FM GUI_DOWNLOAD
CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename = lv_filename ” Name of file
filetype = ‘ASC’ ” File Type (ASC or BIN)
write_field_separator = ‘X’ ” Separate Columns by Tabs in Case of ASCII Download
confirm_overwrite = ‘X’ ” Overwrite File Only After Confirmation
TABLES
data_tab = it_data ” Transfer table
EXCEPTIONS
file_write_error = 1 ” Cannot write to file
no_batch = 2 ” Front-End Function Cannot Be Executed in Background
gui_refuse_filetransfer = 3 ” Incorrect Front End
invalid_type = 4 ” Invalid value for parameter FILETYPE
no_authority = 5 ” No Download Authorization
unknown_error = 6
header_not_allowed = 7 ” Invalid header
separator_not_allowed = 8 ” Invalid separator
filesize_not_allowed = 9 ” Invalid file size
header_too_long = 10 ” The header information is limited to 1023 bytes at present
dp_error_create = 11 ” Cannot Create Data Provider
dp_error_send = 12 ” Error Sending Data with DataProvider
dp_error_write = 13 ” Error Writing Data with DataProvider
unknown_dp_error = 14 ” Error when calling data provider
access_denied = 15 ” Access to File Denied
dp_out_of_memory = 16 ” Not Enough Memory in DataProvider
disk_full = 17 ” Storage Medium Full
dp_timeout = 18 ” Timeout of DataProvider
file_not_found = 19 ” Could not find file
dataprovider_exception = 20 ” General Exception Error in Data Provider
control_flush_error = 21 ” Error in Control Framework
OTHERS = 22.
IF sy-subrc IS INITIAL.
WRITE: / ‘TXT file downloaded:’, lv_filename.
ENDIF.
ENDMETHOD.
ENDCLASS.

*———————————————————————-
* PDF File Downloader Implementation
*———————————————————————-

CLASS zcl_pdf_downloader DEFINITION.
PUBLIC SECTION.
INTERFACES zif_file_downloader. ” Implements interface
ENDCLASS.

CLASS zcl_pdf_downloader IMPLEMENTATION.
METHOD zif_file_downloader~download_file.

DATA: lv_filename TYPE string VALUE ‘C:tempoutput4.pdf’,
lt_pdf TYPE STANDARD TABLE OF x255.

” Generate PDF binary from given data using APOC PDF generator
DATA(lo_pdf) = NEW cl_apoc_pdf_generator( ).
lo_pdf->create_pdf(
EXPORTING
input = it_data
RECEIVING
pdf_binary_output = DATA(rv_bin)
).

” Convert XSTRING PDF to binary table
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = rv_bin
TABLES
binary_tab = lt_pdf.

” Save as binary file on local machine
cl_gui_frontend_services=>gui_download(
EXPORTING
filename = lv_filename ” Name of file
filetype = ‘BIN’ ” File type (ASCII, binary …)
confirm_overwrite = ‘X’ ” Overwrite File Only After Confirmation
CHANGING
data_tab = lt_pdf ” Transfer table
EXCEPTIONS
file_write_error = 1 ” Cannot write to file
no_batch = 2 ” Front-End Function Cannot Be Executed in Backgrnd
gui_refuse_filetransfer = 3 ” Incorrect Front End
invalid_type = 4 ” Invalid value for parameter FILETYPE
no_authority = 5 ” No Download Authorization
unknown_error = 6 ” Unknown error
header_not_allowed = 7 ” Invalid header
separator_not_allowed = 8 ” Invalid separator
filesize_not_allowed = 9 ” Invalid file size
header_too_long = 10 ” Header information currently restricted to 1023 bytes
dp_error_create = 11 ” Cannot create DataProvider
dp_error_send = 12 ” Error Sending Data with DataProvider
dp_error_write = 13 ” Error Writing Data with DataProvider
unknown_dp_error = 14 ” Error when calling data provider
access_denied = 15 ” Access to File Denied
dp_out_of_memory = 16 ” Not Enough Memory in DataProvider
disk_full = 17 ” Storage Medium full
dp_timeout = 18 ” Timeout of DataProvider
file_not_found = 19 ” Could not find file
dataprovider_exception = 20 ” General Exception Error in DataProvider
control_flush_error = 21 ” Error in Control Framework
not_supported_by_gui = 22 ” GUI does not support this
error_no_gui = 23 ” GUI not available
OTHERS = 24
).

IF sy-subrc IS INITIAL.
WRITE: / ‘PDF file downloaded:’, lv_filename.
ENDIF.

ENDMETHOD.
ENDCLASS.

*———————————————————————-
* DOC File Downloader Implementation
*———————————————————————-

CLASS zcl_doc_downloader DEFINITION.
PUBLIC SECTION.
INTERFACES zif_file_downloader.
ENDCLASS.

CLASS zcl_doc_downloader IMPLEMENTATION.
METHOD zif_file_downloader~download_file.
DATA: lv_filename TYPE string VALUE ‘C:tempoutput.doc’.

” Download using GUI_DOWNLOAD in ASCII mode
CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename = lv_filename ” Name of file
filetype = ‘ASC’ ” File Type (ASC or BIN)
write_lf_after_last_line = abap_true ” Writes LF even after last line
write_field_separator = ‘X’
confirm_overwrite = ‘X’
TABLES
data_tab = it_data ” Transfer table
EXCEPTIONS
file_write_error = 1 ” Cannot write to file
no_batch = 2 ” Front-End Function Cannot Be Executed in Background
gui_refuse_filetransfer = 3 ” Incorrect Front End
invalid_type = 4 ” Invalid value for parameter FILETYPE
no_authority = 5 ” No Download Authorization
unknown_error = 6
header_not_allowed = 7 ” Invalid header
separator_not_allowed = 8 ” Invalid separator
filesize_not_allowed = 9 ” Invalid file size
header_too_long = 10 ” The header information is limited to 1023 bytes at present
dp_error_create = 11 ” Cannot Create Data Provider
dp_error_send = 12 ” Error Sending Data with DataProvider
dp_error_write = 13 ” Error Writing Data with DataProvider
unknown_dp_error = 14 ” Error when calling data provider
access_denied = 15 ” Access to File Denied
dp_out_of_memory = 16 ” Not Enough Memory in DataProvider
disk_full = 17 ” Storage Medium Full
dp_timeout = 18 ” Timeout of DataProvider
file_not_found = 19 ” Could not find file
dataprovider_exception = 20 ” General Exception Error in Data Provider
control_flush_error = 21 ” Error in Control Framework
OTHERS = 22.
IF sy-subrc IS INITIAL.
WRITE: / ‘DOC file downloaded:’, lv_filename.
ENDIF.
ENDMETHOD.
ENDCLASS.

*———————————————————————-
* Factory Class – Returns Correct Downloader Object
*———————————————————————-

CLASS zcl_file_factory DEFINITION.
PUBLIC SECTION.
CLASS-METHODS get_downloader
IMPORTING iv_type TYPE string
RETURNING VALUE(ro_downloader) TYPE REF TO zif_file_downloader.
ENDCLASS.

CLASS zcl_file_factory IMPLEMENTATION.
METHOD get_downloader.
” Choose correct class based on file type
CASE iv_type.
WHEN ‘TXT’.
CREATE OBJECT ro_downloader TYPE zcl_txt_downloader.
WHEN ‘PDF’.
CREATE OBJECT ro_downloader TYPE zcl_pdf_downloader.
WHEN ‘DOC’.
CREATE OBJECT ro_downloader TYPE zcl_doc_downloader.
WHEN OTHERS.
MESSAGE ‘Invalid format!’ TYPE ‘E’.
ENDCASE.
ENDMETHOD.
ENDCLASS.

INITIALIZATION.

*———————————————————————-
* Selection Screen
*———————————————————————-

” Choose format dynamically
PARAMETERS: p_type TYPE string DEFAULT ‘TXT’. ” Options: TXT / PDF / DOC

*———————————————————————-
* Main Logic
*———————————————————————-

START-OF-SELECTION.

DATA: lo_down TYPE REF TO zif_file_downloader.

” Get some sample data from SFLIGHT table
SELECT FROM sflight
FIELDS * INTO TABLE (lt_data)
UP TO 10 ROWS.

” Use factory to get correct downloader object
lo_down = zcl_file_factory=>get_downloader( iv_type = p_type ).
lo_down->download_file( it_data = lt_data ).

For TEXT:-

 We can see in our folder.

For WORD–

We can see in our folder–

For PDF:-

We can see in our folder–

Conclusion:-

In this blog, we explored how the Factory Design Pattern can be effectively applied in ABAP to download data in different formats such as TEXT, WORD, PDF without tightly coupling the logic to a specific format. By introducing a factory class that decides which concrete formatter to instantiate, we achieve:

Flexibility – easily add new output formats in the future without modifying existing logic.Reusability – the same factory and interface can be used across multiple applications.Maintainability – reduces redundant code and keeps responsibilities clearly separated.Scalability – supports evolving business needs where multiple consumers may require different file formats.

The Factory Design Pattern not only promotes clean code principles but also aligns with object-oriented best practices in ABAP. By leveraging this approach, developers can build solutions that are extensible, testable, and future-proof.

Thank You.

 

​ Downloading Data in Different Formats Using Factory Design PatternIn real projects, users often need the ability to download business data (e.g., sales orders, customer data, or flight bookings) in different formats like TXT, PDF, DOC.If we try to achieve this in a single report with IF/ELSE or CASE statements, the code quickly becomes messy and hard to maintain. Every new requirement (e.g., CSV, JSON, XML) would require changing the program logic.This is where the Factory Design Pattern helps.How the Factory Design Pattern Works Here–Interface (ZIF_FILE_DOWNLOADER)Defines the common contract: one method DOWNLOAD_FILE.Ensures all file downloaders behave the same way from the outside.Concrete Classes (TXT, DOC, PDF, XLSX Downloaders)Each class implements the interface and contains logic specific to that file type.Example:TXT → Uses GUI_DOWNLOAD with ASCII format.DOC → Uses GUI_DOWNLOAD with linefeeds.PDF → Uses CL_APOC_PDF_GENERATOR and binary download.XLSX → Uses CL_FDT_XL_SPREADSHEET to generate Excel.Factory Class (ZCL_FILE_FACTORY)Acts as a central decision-maker.Based on user selection (TXT, DOC, PDF, XLSX), it instantiates the correct class.The main program never directly knows which class it is using — it only calls the common method.Report Program (ZDEMO6)Reads user input (p_type).Calls the factory to get the right downloader object.Invokes DOWNLOAD_FILE, which internally runs the format-specific logic.Flow of Execution–User selects file type on selection screen.Report calls factory to get the correct downloader.Factory returns the right class instance (TXT/PDF/DOC/XLSX).Report calls DOWNLOAD_FILE method (same for all).File gets saved on the user’s local system in the chosen format.Benefits of Using Factory Pattern–Scalability – Easy to add new file formats without modifying existing code.Maintainability – Each file logic is isolated in its own class.Reusability – The same design can be reused in multiple applications (ALV exports, reports, integration).Clean Code – Avoids long IF/ELSE or CASE blocks.REPORT zdemo6.

*———————————————————————-
* Interface for File Downloaders
*———————————————————————-

INTERFACE zif_file_downloader.
METHODS:
download_file
IMPORTING it_data TYPE STANDARD TABLE.
ENDINTERFACE.

CLASS zcl_txt_downloader DEFINITION.
PUBLIC SECTION.
INTERFACES zif_file_downloader.
ENDCLASS.

*———————————————————————-
* TXT File Downloader Implementation
*———————————————————————-

CLASS zcl_txt_downloader IMPLEMENTATION.
METHOD zif_file_downloader~download_file.
DATA: lv_filename TYPE string VALUE ‘C:tempNew folder1’.

” Download using classic FM GUI_DOWNLOAD
CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename = lv_filename ” Name of file
filetype = ‘ASC’ ” File Type (ASC or BIN)
write_field_separator = ‘X’ ” Separate Columns by Tabs in Case of ASCII Download
confirm_overwrite = ‘X’ ” Overwrite File Only After Confirmation
TABLES
data_tab = it_data ” Transfer table
EXCEPTIONS
file_write_error = 1 ” Cannot write to file
no_batch = 2 ” Front-End Function Cannot Be Executed in Background
gui_refuse_filetransfer = 3 ” Incorrect Front End
invalid_type = 4 ” Invalid value for parameter FILETYPE
no_authority = 5 ” No Download Authorization
unknown_error = 6
header_not_allowed = 7 ” Invalid header
separator_not_allowed = 8 ” Invalid separator
filesize_not_allowed = 9 ” Invalid file size
header_too_long = 10 ” The header information is limited to 1023 bytes at present
dp_error_create = 11 ” Cannot Create Data Provider
dp_error_send = 12 ” Error Sending Data with DataProvider
dp_error_write = 13 ” Error Writing Data with DataProvider
unknown_dp_error = 14 ” Error when calling data provider
access_denied = 15 ” Access to File Denied
dp_out_of_memory = 16 ” Not Enough Memory in DataProvider
disk_full = 17 ” Storage Medium Full
dp_timeout = 18 ” Timeout of DataProvider
file_not_found = 19 ” Could not find file
dataprovider_exception = 20 ” General Exception Error in Data Provider
control_flush_error = 21 ” Error in Control Framework
OTHERS = 22.
IF sy-subrc IS INITIAL.
WRITE: / ‘TXT file downloaded:’, lv_filename.
ENDIF.
ENDMETHOD.
ENDCLASS.

*———————————————————————-
* PDF File Downloader Implementation
*———————————————————————-

CLASS zcl_pdf_downloader DEFINITION.
PUBLIC SECTION.
INTERFACES zif_file_downloader. ” Implements interface
ENDCLASS.

CLASS zcl_pdf_downloader IMPLEMENTATION.
METHOD zif_file_downloader~download_file.

DATA: lv_filename TYPE string VALUE ‘C:tempoutput4.pdf’,
lt_pdf TYPE STANDARD TABLE OF x255.

” Generate PDF binary from given data using APOC PDF generator
DATA(lo_pdf) = NEW cl_apoc_pdf_generator( ).
lo_pdf->create_pdf(
EXPORTING
input = it_data
RECEIVING
pdf_binary_output = DATA(rv_bin)
).

” Convert XSTRING PDF to binary table
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = rv_bin
TABLES
binary_tab = lt_pdf.

” Save as binary file on local machine
cl_gui_frontend_services=>gui_download(
EXPORTING
filename = lv_filename ” Name of file
filetype = ‘BIN’ ” File type (ASCII, binary …)
confirm_overwrite = ‘X’ ” Overwrite File Only After Confirmation
CHANGING
data_tab = lt_pdf ” Transfer table
EXCEPTIONS
file_write_error = 1 ” Cannot write to file
no_batch = 2 ” Front-End Function Cannot Be Executed in Backgrnd
gui_refuse_filetransfer = 3 ” Incorrect Front End
invalid_type = 4 ” Invalid value for parameter FILETYPE
no_authority = 5 ” No Download Authorization
unknown_error = 6 ” Unknown error
header_not_allowed = 7 ” Invalid header
separator_not_allowed = 8 ” Invalid separator
filesize_not_allowed = 9 ” Invalid file size
header_too_long = 10 ” Header information currently restricted to 1023 bytes
dp_error_create = 11 ” Cannot create DataProvider
dp_error_send = 12 ” Error Sending Data with DataProvider
dp_error_write = 13 ” Error Writing Data with DataProvider
unknown_dp_error = 14 ” Error when calling data provider
access_denied = 15 ” Access to File Denied
dp_out_of_memory = 16 ” Not Enough Memory in DataProvider
disk_full = 17 ” Storage Medium full
dp_timeout = 18 ” Timeout of DataProvider
file_not_found = 19 ” Could not find file
dataprovider_exception = 20 ” General Exception Error in DataProvider
control_flush_error = 21 ” Error in Control Framework
not_supported_by_gui = 22 ” GUI does not support this
error_no_gui = 23 ” GUI not available
OTHERS = 24
).

IF sy-subrc IS INITIAL.
WRITE: / ‘PDF file downloaded:’, lv_filename.
ENDIF.

ENDMETHOD.
ENDCLASS.

*———————————————————————-
* DOC File Downloader Implementation
*———————————————————————-

CLASS zcl_doc_downloader DEFINITION.
PUBLIC SECTION.
INTERFACES zif_file_downloader.
ENDCLASS.

CLASS zcl_doc_downloader IMPLEMENTATION.
METHOD zif_file_downloader~download_file.
DATA: lv_filename TYPE string VALUE ‘C:tempoutput.doc’.

” Download using GUI_DOWNLOAD in ASCII mode
CALL FUNCTION ‘GUI_DOWNLOAD’
EXPORTING
filename = lv_filename ” Name of file
filetype = ‘ASC’ ” File Type (ASC or BIN)
write_lf_after_last_line = abap_true ” Writes LF even after last line
write_field_separator = ‘X’
confirm_overwrite = ‘X’
TABLES
data_tab = it_data ” Transfer table
EXCEPTIONS
file_write_error = 1 ” Cannot write to file
no_batch = 2 ” Front-End Function Cannot Be Executed in Background
gui_refuse_filetransfer = 3 ” Incorrect Front End
invalid_type = 4 ” Invalid value for parameter FILETYPE
no_authority = 5 ” No Download Authorization
unknown_error = 6
header_not_allowed = 7 ” Invalid header
separator_not_allowed = 8 ” Invalid separator
filesize_not_allowed = 9 ” Invalid file size
header_too_long = 10 ” The header information is limited to 1023 bytes at present
dp_error_create = 11 ” Cannot Create Data Provider
dp_error_send = 12 ” Error Sending Data with DataProvider
dp_error_write = 13 ” Error Writing Data with DataProvider
unknown_dp_error = 14 ” Error when calling data provider
access_denied = 15 ” Access to File Denied
dp_out_of_memory = 16 ” Not Enough Memory in DataProvider
disk_full = 17 ” Storage Medium Full
dp_timeout = 18 ” Timeout of DataProvider
file_not_found = 19 ” Could not find file
dataprovider_exception = 20 ” General Exception Error in Data Provider
control_flush_error = 21 ” Error in Control Framework
OTHERS = 22.
IF sy-subrc IS INITIAL.
WRITE: / ‘DOC file downloaded:’, lv_filename.
ENDIF.
ENDMETHOD.
ENDCLASS.

*———————————————————————-
* Factory Class – Returns Correct Downloader Object
*———————————————————————-

CLASS zcl_file_factory DEFINITION.
PUBLIC SECTION.
CLASS-METHODS get_downloader
IMPORTING iv_type TYPE string
RETURNING VALUE(ro_downloader) TYPE REF TO zif_file_downloader.
ENDCLASS.

CLASS zcl_file_factory IMPLEMENTATION.
METHOD get_downloader.
” Choose correct class based on file type
CASE iv_type.
WHEN ‘TXT’.
CREATE OBJECT ro_downloader TYPE zcl_txt_downloader.
WHEN ‘PDF’.
CREATE OBJECT ro_downloader TYPE zcl_pdf_downloader.
WHEN ‘DOC’.
CREATE OBJECT ro_downloader TYPE zcl_doc_downloader.
WHEN OTHERS.
MESSAGE ‘Invalid format!’ TYPE ‘E’.
ENDCASE.
ENDMETHOD.
ENDCLASS.

INITIALIZATION.

*———————————————————————-
* Selection Screen
*———————————————————————-

” Choose format dynamically
PARAMETERS: p_type TYPE string DEFAULT ‘TXT’. ” Options: TXT / PDF / DOC

*———————————————————————-
* Main Logic
*———————————————————————-

START-OF-SELECTION.

DATA: lo_down TYPE REF TO zif_file_downloader.

” Get some sample data from SFLIGHT table
SELECT FROM sflight
FIELDS * INTO TABLE (lt_data)
UP TO 10 ROWS.

” Use factory to get correct downloader object
lo_down = zcl_file_factory=>get_downloader( iv_type = p_type ).
lo_down->download_file( it_data = lt_data ).For TEXT:- We can see in our folder.For WORD–We can see in our folder–For PDF:-We can see in our folder–Conclusion:-In this blog, we explored how the Factory Design Pattern can be effectively applied in ABAP to download data in different formats such as TEXT, WORD, PDF without tightly coupling the logic to a specific format. By introducing a factory class that decides which concrete formatter to instantiate, we achieve:Flexibility – easily add new output formats in the future without modifying existing logic.Reusability – the same factory and interface can be used across multiple applications.Maintainability – reduces redundant code and keeps responsibilities clearly separated.Scalability – supports evolving business needs where multiple consumers may require different file formats.The Factory Design Pattern not only promotes clean code principles but also aligns with object-oriented best practices in ABAP. By leveraging this approach, developers can build solutions that are extensible, testable, and future-proof.Thank You.   Read More Technology Blog Posts by Members articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author