This blog is the second part of the blog series on Strategy Design Pattern:
So before we start using the Strategy Pattern let us revisit what we have learnt about Strategy Design Pattern!
Let us recapitulate what we’ve learnt so far about Strategy pattern:
Figure 1 - UML representation of Strategy Pattern
Before you start your coding, you may want to get your design approach validated by your peers. What better way to do it, than share your UML diagram!
Figure 2 - UML diagram for Flight Data Upload Program
Salient features of the design are:
METHOD set_upload_util.
DATA: lv_class_name TYPE seoclsname,
lo_upld_util TYPE REF TO zif_upload_util.
* Whenever a new file upload technique is to be added, we
* modify this method by adding an extra WHEN clause
* Set the upload util
CASE i_file_upload_type.
WHEN zif_upload_util=>c_pc_file.
lv_class_name = `ZCL_PC_FILE`. "For PC file
WHEN zif_upload_util=>c_as_file.
lv_class_name = `ZCL_APPL_SERVER_FILE`. "For appl server file
ENDCASE.
IF me->po_upld_util IS BOUND.
CLEAR me->po_upld_util. "Delete the reference
ENDIF.
* Create the new reference
CREATE OBJECT me->po_upld_util TYPE (lv_class_name).
ENDMETHOD.
METHOD set_log_util.
DATA: lo_log_util TYPE REF TO zif_log_util,
lv_class_name TYPE seoclsname.
* Whenever a new log writing technique is to be added, we
* enhance this method by adding an extra WHEN clause
* Set the upload util
CASE i_log_write_type.
WHEN zif_log_util=>c_classical.
lv_class_name = `ZCL_CLASSICAL_LOG`. "For classical log
WHEN zif_log_util=>c_appl_log.
lv_class_name = `ZCL_APPLLOG`. "For appl log
ENDCASE.
IF me->po_log_util IS BOUND.
CLEAR me->po_log_util. "Delete the old reference
ENDIF.
CREATE OBJECT me->po_log_util TYPE (lv_class_name)."Create the new reference
ENDMETHOD.
The class framework can be downloaded as a nugget here. For more information on how to upload the nugget visit SAPlink project page.
The program uses the client instance go_dataupld to process the data
DATA: go_dataupld TYPE REF TO zcl_flightdata_upload.
**********************************************************************
* Data Processing Event
**********************************************************************
START-OF-SELECTION.
* Create the flight data upload object
CREATE OBJECT go_dataupld.
* §1.1 Whenever a new file upload technique is to be added, we
* enhance this method by adding an extra WHEN clause
* Set the file upload attribute
CASE 'X'.
WHEN rb_pc."PC file upload
go_dataupld->set_upload_util( zif_upload_util=>c_pc_file ).
WHEN rb_app."Appl Server file upload
go_dataupld->set_upload_util( zif_upload_util=>c_as_file ).
ENDCASE.
* §1.2 Whenever a new log writing technique is to be added, we
* enhance this method by adding an extra WHEN clause
* Set the write log attribute
CASE 'X'.
WHEN rb_old."For Classical log
go_dataupld->set_log_util( zif_log_util=>c_classical ).
WHEN rb_applg."For Appl log
go_dataupld->set_log_util( zif_log_util=>c_appl_log ).
ENDCASE.
go_dataupld->upload_file_data( ). "Upload the file data
go_dataupld->upload_data_to_db( ). "Upload data to DB
go_dataupld->write_log( ). "Write Log
N.B.: Actual file upload & log writing techniques are outside the scope of this blog.
As mentioned in the previous blog the users want to add FTP functionality to the current program, which is when you started exploring the design patterns. Let us see how Strategy DP helps keep your code flexible.
In order to add the FTP functionality to the program, you follow these steps:
Now that you have a file upload strategy, you just have to add a new class which will upload the file via FTP (for e.g., ZCL_FTP_FILE) which will implement the interface ZIF_UPLOAD_UTIL.
Figure 4 - FTP Element added to Selection-Screen
* Set the upload util
CASE i_file_upload_type.
WHEN zif_upload_util=>c_pc_file.
lv_class_name = `ZCL_PC_FILE`. "For PC file
WHEN zif_upload_util=>c_as_file.
lv_class_name = `ZCL_APPL_SERVER_FILE`. "For appl server file
WHEN zif_upload_util=>c_ftp_file.
lv_class_name = `ZCL_FTP_FILE`. "For FTP file upload --> Add this line
ENDCASE.
Figure 5 - FTP Element added to Selection-Screen
CASE 'X'.
WHEN rb_pc."PC file upload
go_dataupld->set_upload_util( zif_upload_util=>c_pc_file ).
WHEN rb_app."Appl Server file upload
go_dataupld->set_upload_util( zif_upload_util=>c_as_file ).
WHEN rb_ftp.
go_dataupld->set_upload_util( zif_upload_util=>c_ftp_file ). "--> We just need to add this line
ENDCASE.
You can see that by implementing Strategy DP you’ve managed to loosely couple the “File Upload” functionality from the actual program. Your program needs a very minor fix to add this additional functionality.
If the users now want to write the log to an Application Server file, you know what to do.
Now that you know “why” & “how” to implement Strategy design pattern, you will find more places to apply it.
IMHO, although DPs can be helpful remember not to overuse them. You don’t want to complicate your “Hello World” application, do you? Do not complicate your programs unnecessarily, always K.I.S.S!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
4 | |
2 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 |