Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

how to customize the code in a method.

Former Member
0 Likes
1,085

I have created a wrapper class for ALV display to generalize the code for report programming,

I have used CL_SALV *   classes for report programming.

I have created a custom class ZCL_GB_EVENT_HANDLE  for interactive report programming,

where action like HANDLER_DOUBLE_CLK, HANDLER_LINK_CLK , HANDLER_ADDED_FUN  will be handled,

now i want to handle the these methods by deriving into some custom class,

where each user creates a new  custom class and write their own custom code in the derived class.

now my question is how to derive the ZCL_GB_EVENT_HANDLE into custom class for example ZCL_GB_EVENT_HANDLE_DATA,

please help who can we achevie this like abstraction or wide casting or any other options available ?

4 REPLIES 4
Read only

Former Member
0 Likes
855

Hello,

     To let others derive from your class into their classes and implement their own code for it , make your class an interface.

>    Define the interface, define the methods in it which you want other classes to implement.

>    After defining the interface, others can create their own classes and implement your interface with        their own coding.

e.g :     INTERFACE zif_gb_event_handle DEFINITION.

               METHODS :

                    handler_double_clk ,

                    ... ,

                    ... ,

           ENDINTERFACE.

Now others can use your interface and implement their own coding.

          CLASS  zcl_deriving_class DEFINITION.

               INTERFACES zif_gb_event_handle.

           ENDCLASS.

           CLASS zcl_deriving_class IMPLEMENTATION.

               METHOD zif_gb_event_handle~handler_double_clk.

                    custom code

                    .....

                    .....

                   

                ENDMETHOD.

           ENDCLASS.

Hope it will help you.

Read only

adam_krawczyk1
Contributor
0 Likes
855

Hi Hari,

Some options that I see:

1) Use facade design pattern:

- It is what Vipin mentioned

- You build new class ZCL_GB_EVENT_HANDLE_DATA that contains attribute of object ZCL_GB_EVENT_HANDLE

- You create any methods that you want in ZCL_GB_EVENT_HANDLE_DATA and just call ZCL_GB_EVENT_HANDLE when you need it inside

- In this way, you decide which methods from ZCL_GB_EVENT_HANDLE are exposed to user who uses object ZCL_GB_EVENT_HANDLE_DATA

2) Use decorator design pattern

- Create new class ZCL_GB_EVENT_HANDLE_DATA that inherits from class ZCL_GB_EVENT_HANDLE (this cannot be final)

CLASS ZCL_GB_EVENT_HANDLE_DATA INHERITING FROM ZCL_GB_EVENT_HANDLE.


- You mentioned that method ZCL_GB_EVENT_HANDLE already implements method like HANDLER_DOUBLE_CLK. If you want to do the same in new class and add something to behavior, then new class CL_GB_EVENT_HANDLE_DATA->HANDLER_DOUBLE_CLK implementation will first call parent method (CL_GB_EVENT_HANDLE->HANDLE_DOUBLE_CLK) then add implementation of own logic:

CL_GB_EVENT_HANDLE_DATA method implementation:

DEFINITION:

METHODS HANDLER_DOUBLE_CLK REDEFINITION.

IMPLEMENTATION:

METHOD HANDLER_DOUBLE_CLK.

     super->HANDLER_DOUBLE_CLK( ). " This will do what CL_GB_EVENT_HANDLE does normally, parent method call

     " Here add special own code just for CL_GB_EVENT_HANDLE_DATA class that will additionally decorate behavior

ENDMETHOD.


- In this approach as compared to point 1, we are exposing in new class all methods that CL_GB_EVENT_HANDLE contains. Even if you do not redefine method in ZCL_GB_EVENT_HANDLE_DATA, original will be used from CL_GB_EVENT_HANDLE.

- Decorator pattern can be also built with interfaces, as mentioned by Ashish.

Hope it helps

Regards,

Adam



Read only

Former Member
0 Likes
855

Hi,

As Adam said, one of the options is,

make your all custom classes to inherit from your class ZCL_GB_EVENT_HANDLE.

Then in every custom class REDEFINE the handler method.

CLASS zcl_gb_event_handle_data DEFINITION.

     METHODS handler_double_clk REDIFINITION.

ENDCLASS.

CLASS zcl_gb_event_handle_data IMPLEMENTATION.

     METHOD handler_double_clk.

          super->handler_double_clk( ).          "optional if you want implementation of super class also to get executed.

     ....                                                       "followed by the custom code for each class.

     ....

     ENDMETHOD.

ENDCLASS.

This way each custom class would be able to use the same methods with their own implementation.

Thank You.

Read only

Former Member
0 Likes
855

Hi Guys,

Thanks for your suggestions,

I have created a sub class for every custom implementation I want to implement from the generic class.

And I type casted from generic class to custom class - where each user can do their own implementation.