‎2008 Mar 18 12:38 PM
Dear Gurus,
I have created one interface zif_test and method is calc.
I have created two class in SE24- zcl_stf and zcl_wrk with interface zif_test. [ calculation varies in both classes ].
stf and wrk are two category.
Problem here:
How do i call the object based on category ?
Regards
Vijay
Point will be rewarded
‎2008 Apr 13 2:57 PM
Dear Vijay,
You question is not very clear. Can you please explain a bit?
In general, if you have an interface
if_xyz having method meth1
and two classes implement this interface
cl_one and cl_two.
Youu can call the method in interface statically is
cl_one->if_xyz~meth1 and
cl_two->if_xyz~meth1
If you want to have dynamic call, assign
if_xyz = cl_one or if_xyz = cl_two depending on your situation.
Then you can call if_xyz->meth1 and the implementation called will depend on which class is now assigned to if_xyz.
In general it is useful, when you call a method which takes a import paramater of type if_xyz and you pass cl_one or cl_two when you call it.
Regards, Rakesh
‎2008 Apr 13 5:08 PM
Hello Vijay
I would recommend to create a factory method or class which instantiates the appropriate class depending on your category.
The factory method or class could look like this (please note that I define it as local class for this purpose. You should create a global class):
CLASS lcl_factory DEFINITION.
PUBLIC SECTION.
CLASS-METHODS:
create
IMPORTING
id_category TYPE zcategory
RETURNING
value(ro_instance) TYPE REF TO zif_test. " your interface
ENDCLASS.
CLASS lcl_factory IMPLEMENTATION.
METHOD create.
CASE id_category.
WHEN 'STF'.
CREATE OBJECT ro_instance TYPE ( 'ZCL_STF' ).
WHEN 'WRK'.
CREATE OBJECT ro_instance TYPE ( 'ZCL_WRK' ).
WHEN OTHERS.
" perhaps you can define a default implementation
ENDCASE.
ENDMETHOD.
ENDCLASS.
The advantage of using a factory method is that in your main program you do not need to think about the required implementation but just call your CALC method.
DATA: go_test TYPE REF TO zif_test. " interface (!!!) variable
PARAMETERS:
p_catego TYPE zcategory DEFAULT 'STF'. " your category
START-OF-SELECTION.
" NOTE: the factory method returns the required implementation
go_test = lcl_factory=>create( p_catego ).
go_test->calc(...).
...
END-OF-SELECTION.
Regards
Uwe