Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
This blog represents part 3 of the 6-part series Getting comfortable using the Object-Oriented design model with ABAP.

Part 1 – Transforming a simple procedural model into its equivalent OO design, focusing on the OO principles of Encapsulation and Abstraction.


Part 2 – Exploring Abstraction further by refactoring the program to transform selected static classes into instantiable classes.


Part 3 – Refactoring the program further to take advantage of the OO principle of Inheritance.


Part 4 – Adhering to the Single Responsibility Principle by refining the program to restrict each class to do only what it is intended to do.


Part 5 – Removing classes that no longer serve a purpose.


Part 6 – Introducing the Singleton OO design pattern, resulting in elimination of all static classes.


To recap from the preceding blog, we took a program having only static classes and changed it so that some of those classes could be instantiated. Here is the source code as we left it in the previous blog:
report.
interface data_exchangeable.
types : row_counter type n length 02.
types : email_recipient
type adr6-smtp_addr.
endinterface.
class excel_spreadsheet_manager definition
final.
public section.
methods : copy_table_to_excel_worksheet
importing
source_stack
type standard table
source_description
type string
raising
zcx_excel
, send_excel_via_email
importing
recipient
type data_exchangeable=>email_recipient
.
private section.
data : excel type ref to zcl_excel.
endclass.
class excel_spreadsheet_manager implementation.
method copy_table_to_excel_worksheet.
constants : first_column type char1 value 'A'
.
data : worksheet type ref to zcl_excel_worksheet
, worksheet_title
type zexcel_sheet_title
, table_settings type zexcel_s_table_settings
.
table_settings-table_style = zcl_excel_table=>builtinstyle_medium2.
table_settings-show_row_stripes
= abap_true.
table_settings-nofilters = abap_true.
table_settings-top_left_column
= first_column.
table_settings-top_left_row = 01.
if excel is not bound.
create object excel.
worksheet = excel->get_active_worksheet( ).
else.
worksheet = excel->add_new_worksheet( ).
endif.
worksheet_title = source_description.
worksheet->set_title( worksheet_title ).
worksheet->bind_table(
ip_table = source_stack
is_table_settings = table_settings
).
endmethod.
method send_excel_via_email.
constants : excel_file_type
type string value '.xlsx'
, file_name_parameter
type string value '&SO_FILENAME='
.
data : excel_writer type ref to zif_excel_writer
, excel_as_xstring
type xstring
, excel_as_xstring_bytecount
type i
, excel_as_solix_stack
type solix_tab
, mail_send_request
type ref to cl_bcs
, mail_message type ref to cl_document_bcs
, any_bcs_exception
type ref to cx_bcs
, diagnostic type string
, mail_title type so_obj_des
, mail_text_stack
type soli_tab
, mail_text_entry
like line
of mail_text_stack
, mail_attachment_subject
type sood-objdes
, mail_attachment_bytecount
type sood-objlen
, mail_attachment_header_stack
type soli_tab
, mail_attachment_header_entry
like line of mail_attachment_header_stack
, internet_email_recipient
type ref to if_recipient_bcs
, successful_send
type abap_bool
, file_name type string
.
" Much of the code here was lifted from method send_mail of
" class lcl_ouput, defined in object ZDEMO_EXCEL_OUTPUTOPT_INCL:
concatenate sy-repid " this report name
sy-datum " current date
sy-uzeit " current time
excel_file_type " excel file extension
into file_name.
mail_title = file_name.
mail_attachment_subject = file_name.
mail_text_entry = 'See attachment'.
append mail_text_entry
to mail_text_stack.
concatenate file_name_parameter
file_name
into mail_attachment_header_entry.
append mail_attachment_header_entry
to mail_attachment_header_stack.
create object excel_writer type zcl_excel_writer_2007.
excel_as_xstring = excel_writer->write_file( excel ).
excel_as_solix_stack = cl_bcs_convert=>xstring_to_solix( iv_xstring = excel_as_xstring ).
excel_as_xstring_bytecount = xstrlen( excel_as_xstring ).
mail_attachment_bytecount = excel_as_xstring_bytecount.
try.
mail_message = cl_document_bcs=>create_document(
i_type = 'RAW' "#EC NOTEXT
i_text = mail_text_stack
i_subject = mail_title
).
mail_message->add_attachment(
i_attachment_type = 'XLS' "#EC NOTEXT
i_attachment_subject = mail_attachment_subject
i_attachment_size = mail_attachment_bytecount
i_att_content_hex = excel_as_solix_stack
i_attachment_header = mail_attachment_header_stack
).
mail_send_request = cl_bcs=>create_persistent( ).
mail_send_request->set_document( mail_message ).
internet_email_recipient = cl_cam_address_bcs=>create_internet_address( recipient ).
mail_send_request->add_recipient( internet_email_recipient ).
successful_send = mail_send_request->send( ).
commit work.
if successful_send eq abap_false.
message i500(sbcoms) with recipient.
else.
message s022(so).
message 'Document ready to be sent - Check SOST' type 'I'.
endif.
catch cx_bcs into any_bcs_exception.
diagnostic = any_bcs_exception->if_message~get_text( ).
message diagnostic type 'I'.
endtry.
endmethod.
endclass.
class flight_report definition
final.
public section.
methods : produce_report
importing
row_count
type data_exchangeable=>row_counter
excel_spreadsheet_manager
type ref to excel_spreadsheet_manager
raising
zcx_excel
.
endclass.
class flight_report implementation.
method produce_report.
data : flight_stack type standard table of sflight
, alv_report type ref to cl_salv_table
.
try.
call method cl_salv_table=>factory
importing
r_salv_table = alv_report
changing
t_table = flight_stack.
catch cx_salv_msg.
return.
endtry.
select *
into table flight_stack
from sflight
up to row_count rows.
alv_report->display( ).
call method excel_spreadsheet_manager->copy_table_to_excel_worksheet
exporting
source_stack = flight_stack
source_description = 'Flights'.
endmethod.
endclass.
class carrier_report definition
final.
public section.
methods : produce_report
importing
row_count
type data_exchangeable=>row_counter
excel_spreadsheet_manager
type ref to excel_spreadsheet_manager
raising
zcx_excel
.
endclass.
class carrier_report implementation.
method produce_report.
data : carrier_stack type standard table of scarr
, alv_report type ref to cl_salv_table
.
try.
call method cl_salv_table=>factory
importing
r_salv_table = alv_report
changing
t_table = carrier_stack.
catch cx_salv_msg.
return.
endtry.
select *
into table carrier_stack
from scarr
up to row_count rows.
alv_report->display( ).
call method excel_spreadsheet_manager->copy_table_to_excel_worksheet
exporting
source_stack = carrier_stack
source_description = 'Carriers'.
endmethod.
endclass.
class booking_report definition
final.
public section.
methods : produce_report
importing
row_count
type data_exchangeable=>row_counter
excel_spreadsheet_manager
type ref to excel_spreadsheet_manager
raising
zcx_excel
.
endclass.
class booking_report implementation.
method produce_report.
data : booking_stack type standard table of sbook
, alv_report type ref to cl_salv_table
.
try.
call method cl_salv_table=>factory
importing
r_salv_table = alv_report
changing
t_table = booking_stack.
catch cx_salv_msg.
return.
endtry.
select *
into table booking_stack
from sbook
up to row_count rows.
alv_report->display( ).
call method excel_spreadsheet_manager->copy_table_to_excel_worksheet
exporting
source_stack = booking_stack
source_description = 'Bookings'.
endmethod.
endclass.
class process_driver definition
abstract
final.
public section.
class-methods: drive_process
importing
row_count
type data_exchangeable=>row_counter
recipient
type data_exchangeable=>email_recipient
.
endclass.
class process_driver implementation.
method drive_process.
data : flight_report type ref to flight_report
, carrier_report type ref to carrier_report
, booking_report type ref to booking_report
, excel_spreadsheet_manager
type ref to excel_spreadsheet_manager
.
create object: flight_report
, carrier_report
, booking_report
, excel_spreadsheet_manager
.
try.
call method flight_report->produce_report exporting row_count = row_count
excel_spreadsheet_manager = excel_spreadsheet_manager.
call method carrier_report->produce_report exporting row_count = row_count
excel_spreadsheet_manager = excel_spreadsheet_manager.
call method booking_report->produce_report exporting row_count = row_count
excel_spreadsheet_manager = excel_spreadsheet_manager.
catch zcx_excel ##NO_HANDLER.
endtry.
call method excel_spreadsheet_manager->send_excel_via_email exporting recipient = recipient.
endmethod.
endclass.
class email_address_resolver definition
abstract
final.
public section.
class-methods: resolve_email_address
importing
userid
type syuname
exporting
email_address
type data_exchangeable=>email_recipient
.
endclass.
class email_address_resolver implementation.
method resolve_email_address.
select single smtp_addr
into email_address
from adr6 ##WARN_OK
inner join
usr21 on usr21~persnumber eq adr6~persnumber
where usr21~bname eq userid.
endmethod.
endclass.
parameters : rowcount type data_exchangeable=>row_counter.
parameters : recipien type data_exchangeable=>email_recipient.
initialization.
call method email_address_resolver=>resolve_email_address
exporting
userid = sy-uname
importing
email_address = recipien.
start-of-selection.
call method process_driver=>drive_process
exporting
row_count = rowcount
recipient = recipien.

Open your favorite ABAP editor, make a copy of this ABAP program and follow along as we apply changes to reduce much of the code duplication by using Inheritance. For those who do not have ABAP2XLSX available at their site, replace class excel_spreadsheet_manager with the following source code:
class excel_spreadsheet_manager        definition
final.
public section.
methods : copy_table_to_excel_worksheet
importing
source_stack
type standard table
source_description
type string
raising
zcx_excel
, send_excel_via_email
importing
recipient
type data_exchangeable=>email_recipient
.
private section.
data : excel type string.
endclass.
class excel_spreadsheet_manager implementation.
method copy_table_to_excel_worksheet.
data : source_stack_lines type string.
describe table source_stack lines source_stack_lines.
concatenate excel
source_stack_lines
source_description
into excel separated by space.
endmethod.
method send_excel_via_email.
data : message type string.
concatenate excel
'would be sent to'
recipient
into message separated by space.
message message type 'I'.
endmethod.
endclass.

and include the following local exception class definition after the report statement:
class zcx_excel                        definition
inheriting from cx_static_check.
endclass.

Motivation

Currently the methods produce_report in methods flight_report, carrier_report and booking_report all contain the same code to create and present the report with the exception of the name of the variable holding the standard table of records to be displayed in the report. We can consolidate this processing into a single method that can be made available to all of these classes through inheritance.

Creating a new report class

First let’s create a new class called report from which classes flight_report, carrier_report and booking_report can inherit. It will contain an abstract produce_report method definition whose signature is identical to the produce_report of flight_report. In addition, it will have a protected method named present_report that can be used by each inheriting class. The skeleton of the new report class is shown here:
class report                           definition
abstract.
public section.
methods : produce_report abstract
importing
row_count
type data_exchangeable=>row_counter
excel_spreadsheet_manager
type ref to excel_spreadsheet_manager
raising
zcx_excel
.
protected section.
methods : present_report
changing
record_stack
type standard table
.
endclass.
class report implementation.
method present_report.
endmethod.
endclass.

Place this new report class ahead of the flight_report class. Once in place, copy the code from method produce_report of class flight_report into the method present_report, making the following changes:

  • remove the definition of field flight_stack

  • for formal parameter t_table, change the name of the actual parameter from flight_stack to record_stack

  • remove the select statement

  • remove the call to method copy_table_to_excel_spreadsheet


When completed, the code in method present_report should look like this:
  method present_report.
data : alv_report type ref to cl_salv_table
.
try.
call method cl_salv_table=>factory
importing
r_salv_table = alv_report
changing
t_table = record_stack.
catch cx_salv_msg.
return.
endtry.
alv_report->display( ).
endmethod.

At this point a syntax check should pass.

Inheriting from the new report class

Next, change class flight_report in the following way:

  • Include on its class definition statement the qualifier “inheriting from report”:


class flight_report                    definition
final
inheriting from report.


  • Replace the signature for its method produce_report with an indication that the method is redefined:


  public section.
methods : produce_report redefinition
.


  • In its implementation of method produce_report, discard the definition for field alv_report.

  • In its implementation of method produce_report, discard the try-endtry block and all code contained within it.

  • In its implementation of method produce_report, replace its call to method display with a call to method present_report it inherits from class report, as in:


    call method me->present_report
changing
record_stack = flight_stack.

At this point a syntax check should pass.

Next, make the same relative changes to classes carrier_report and booking_report as were made for class flight_report. Afterward a syntax check should pass.

Executing the program at this point should prove that it still works as before.

Summary

We’ve made enough changes for now and the final image of the code looks like this:
report.
interface data_exchangeable.
types : row_counter type n length 02.
types : email_recipient
type adr6-smtp_addr.
endinterface.
class excel_spreadsheet_manager definition
final.
public section.
methods : copy_table_to_excel_worksheet
importing
source_stack
type standard table
source_description
type string
raising
zcx_excel
, send_excel_via_email
importing
recipient
type data_exchangeable=>email_recipient
.
private section.
data : excel type ref to zcl_excel.
endclass.
class excel_spreadsheet_manager implementation.
method copy_table_to_excel_worksheet.
constants : first_column type char1 value 'A'
.
data : worksheet type ref to zcl_excel_worksheet
, worksheet_title
type zexcel_sheet_title
, table_settings type zexcel_s_table_settings
.
table_settings-table_style = zcl_excel_table=>builtinstyle_medium2.
table_settings-show_row_stripes
= abap_true.
table_settings-nofilters = abap_true.
table_settings-top_left_column
= first_column.
table_settings-top_left_row = 01.
if excel is not bound.
create object excel.
worksheet = excel->get_active_worksheet( ).
else.
worksheet = excel->add_new_worksheet( ).
endif.
worksheet_title = source_description.
worksheet->set_title( worksheet_title ).
worksheet->bind_table(
ip_table = source_stack
is_table_settings = table_settings
).
endmethod.
method send_excel_via_email.
constants : excel_file_type
type string value '.xlsx'
, file_name_parameter
type string value '&SO_FILENAME='
.
data : excel_writer type ref to zif_excel_writer
, excel_as_xstring
type xstring
, excel_as_xstring_bytecount
type i
, excel_as_solix_stack
type solix_tab
, mail_send_request
type ref to cl_bcs
, mail_message type ref to cl_document_bcs
, any_bcs_exception
type ref to cx_bcs
, diagnostic type string
, mail_title type so_obj_des
, mail_text_stack
type soli_tab
, mail_text_entry
like line
of mail_text_stack
, mail_attachment_subject
type sood-objdes
, mail_attachment_bytecount
type sood-objlen
, mail_attachment_header_stack
type soli_tab
, mail_attachment_header_entry
like line of mail_attachment_header_stack
, internet_email_recipient
type ref to if_recipient_bcs
, successful_send
type abap_bool
, file_name type string
.
" Much of the code here was lifted from method send_mail of
" class lcl_ouput, defined in object ZDEMO_EXCEL_OUTPUTOPT_INCL:
concatenate sy-repid " this report name
sy-datum " current date
sy-uzeit " current time
excel_file_type " excel file extension
into file_name.
mail_title = file_name.
mail_attachment_subject = file_name.
mail_text_entry = 'See attachment'.
append mail_text_entry
to mail_text_stack.
concatenate file_name_parameter
file_name
into mail_attachment_header_entry.
append mail_attachment_header_entry
to mail_attachment_header_stack.
create object excel_writer type zcl_excel_writer_2007.
excel_as_xstring = excel_writer->write_file( excel ).
excel_as_solix_stack = cl_bcs_convert=>xstring_to_solix( iv_xstring = excel_as_xstring ).
excel_as_xstring_bytecount = xstrlen( excel_as_xstring ).
mail_attachment_bytecount = excel_as_xstring_bytecount.
try.
mail_message = cl_document_bcs=>create_document(
i_type = 'RAW' "#EC NOTEXT
i_text = mail_text_stack
i_subject = mail_title
).
mail_message->add_attachment(
i_attachment_type = 'XLS' "#EC NOTEXT
i_attachment_subject = mail_attachment_subject
i_attachment_size = mail_attachment_bytecount
i_att_content_hex = excel_as_solix_stack
i_attachment_header = mail_attachment_header_stack
).
mail_send_request = cl_bcs=>create_persistent( ).
mail_send_request->set_document( mail_message ).
internet_email_recipient = cl_cam_address_bcs=>create_internet_address( recipient ).
mail_send_request->add_recipient( internet_email_recipient ).
successful_send = mail_send_request->send( ).
commit work.
if successful_send eq abap_false.
message i500(sbcoms) with recipient.
else.
message s022(so).
message 'Document ready to be sent - Check SOST' type 'I'.
endif.
catch cx_bcs into any_bcs_exception.
diagnostic = any_bcs_exception->if_message~get_text( ).
message diagnostic type 'I'.
endtry.
endmethod.
endclass.
class report definition
abstract.
public section.
methods : produce_report abstract
importing
row_count
type data_exchangeable=>row_counter
excel_spreadsheet_manager
type ref to excel_spreadsheet_manager
raising
zcx_excel
.
protected section.
methods : present_report
changing
record_stack
type standard table
.
endclass.
class report implementation.
method present_report.
data : alv_report type ref to cl_salv_table
.
try.
call method cl_salv_table=>factory
importing
r_salv_table = alv_report
changing
t_table = record_stack.
catch cx_salv_msg.
return.
endtry.
alv_report->display( ).
endmethod.
endclass.
class flight_report definition
final
inheriting from report.
public section.
methods : produce_report redefinition
.
endclass.
class flight_report implementation.
method produce_report.
data : flight_stack type standard table of sflight
.
select *
into table flight_stack
from sflight
up to row_count rows.
call method me->present_report
changing
record_stack = flight_stack.
call method excel_spreadsheet_manager->copy_table_to_excel_worksheet
exporting
source_stack = flight_stack
source_description = 'Flights'.
endmethod.
endclass.
class carrier_report definition
final
inheriting from report.
public section.
methods : produce_report redefinition
.
endclass.
class carrier_report implementation.
method produce_report.
data : carrier_stack type standard table of scarr
.
select *
into table carrier_stack
from scarr
up to row_count rows.
call method me->present_report
changing
record_stack = carrier_stack.
call method excel_spreadsheet_manager->copy_table_to_excel_worksheet
exporting
source_stack = carrier_stack
source_description = 'Carriers'.
endmethod.
endclass.
class booking_report definition
final
inheriting from report.
public section.
methods : produce_report redefinition
.
endclass.
class booking_report implementation.
method produce_report.
data : booking_stack type standard table of sbook
.
select *
into table booking_stack
from sbook
up to row_count rows.
call method me->present_report
changing
record_stack = booking_stack.
call method excel_spreadsheet_manager->copy_table_to_excel_worksheet
exporting
source_stack = booking_stack
source_description = 'Bookings'.
endmethod.
endclass.
class process_driver definition
abstract
final.
public section.
class-methods: drive_process
importing
row_count
type data_exchangeable=>row_counter
recipient
type data_exchangeable=>email_recipient
.
endclass.
class process_driver implementation.
method drive_process.
data : flight_report type ref to flight_report
, carrier_report type ref to carrier_report
, booking_report type ref to booking_report
, excel_spreadsheet_manager
type ref to excel_spreadsheet_manager
.
create object: flight_report
, carrier_report
, booking_report
, excel_spreadsheet_manager
.
try.
call method flight_report->produce_report exporting row_count = row_count
excel_spreadsheet_manager = excel_spreadsheet_manager.
call method carrier_report->produce_report exporting row_count = row_count
excel_spreadsheet_manager = excel_spreadsheet_manager.
call method booking_report->produce_report exporting row_count = row_count
excel_spreadsheet_manager = excel_spreadsheet_manager.
catch zcx_excel ##NO_HANDLER.
endtry.
call method excel_spreadsheet_manager->send_excel_via_email exporting recipient = recipient.
endmethod.
endclass.
class email_address_resolver definition
abstract
final.
public section.
class-methods: resolve_email_address
importing
userid
type syuname
exporting
email_address
type data_exchangeable=>email_recipient
.
endclass.
class email_address_resolver implementation.
method resolve_email_address.
select single smtp_addr
into email_address
from adr6 ##WARN_OK
inner join
usr21 on usr21~persnumber eq adr6~persnumber
where usr21~bname eq userid.
endmethod.
endclass.
parameters : rowcount type data_exchangeable=>row_counter.
parameters : recipien type data_exchangeable=>email_recipient.
initialization.
call method email_address_resolver=>resolve_email_address
exporting
userid = sy-uname
importing
email_address = recipien.
start-of-selection.
call method process_driver=>drive_process
exporting
row_count = rowcount
recipient = recipien.

We’ve identified duplicated code previously found in the produce_report methods of classes flight_report, carrier_report and booking_report and consolidated this into method present_report of new class report, a class from which classes flight_report, carrier_report and booking_report now inherit.

What’s next?

There remain other concepts of object-oriented programming for us to explore using this simple example program. Each of the three report classes gets the rows it is to present, then presents the rows in an ALV report, then arranges for those rows to be converted into an Excel worksheet. This means that each of these report classes has multiple responsibilities. The next step is to refactor the program so that these three classes conform to the Single Responsibility Principle. This will be covered in the blog representing part 4 of this 6-part series Getting comfortable using the Object-Oriented design model with ABAP.