CLASS zcl_vendor DEFINITION
PUBLIC FINAL CREATE PRIVATE.
PUBLIC SECTION.
DATA gv_lifnr TYPE lifnr READ-ONLY
CLASS-METHODS get_instance
IMPORTING !iv_lifnr TYPE lifnr
RETURNUNG VALUE(ro_obj) TYPE REF TO zcl_vendor.
PRIVATE SECTION.
TYPES:
BEGIN OF t_multiton,
lifnr TYPE lifnr,
obj TYPE REF TO zcl_vendor,
END OF t_multiton,
tt_multiton TYPE HASHED TABLE OF t_multiton
WITH UNIQUE KEY primary_key COMPONENTS lifnr.
CLASS-DATA gt_multiton TYPE tt_multiton.
METHODS constructor
IMPORTING
!iv_lifnr TYPE lifnr.
PROTECTED SECTION.
ENDCLASS.
CLASS zcl_vendor IMPLEMENTATION.
METHOD constructor.
“ Check if IV_LIFNR exists in LFA1 and raise error if not
gv_lifnr = iv_lifnr.
ENDMETHOD.
METHOD get_instance.
ASSIGN gt_multiton[ KEY primary_key
COMPONENTS lifnr = iv_lifnr ]
TO FIELD-SYMBOL(<ls_multiton>).
IF sy-subrc NE 0.
“ Check if IV_LIFNR exists in LFA1 and raise error if not
DATA(ls_multiton) = VALUE t_multiton( lifnr = iv_lifnr ).
ls_multiton-obj = NEW #( iv_lifnr ).
INSERT ls_multiton
INTO TABLE gt_multiton
ASSIGNING <ls_multiton>.
ENDIF.
ro_obj = <ls_multiton>-obj.
ENDMETHOD.
ENDCLASS.
CLASS zcl_bc_multiton_demo DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
data:
gv_id type char15,
gv_erdat type erdat,
gv_ernam type ernam.
methods:
constructor importing iv_id type char15.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_bc_multiton_demo IMPLEMENTATION.
method constructor.
gv_id = iv_id.
gv_erdat = sy-datum.
gv_Ernam = sy-uname.
endmethod.
ENDCLASS.
interface ZIF_BC_MULTITON
public .
class-methods:
get_instance
importing
!iv_objectid type CDOBJECTV
returning
value(ro_obj) type ref to ZIF_BC_MULTITON
raising
CX_SY_CREATE_OBJECT_ERROR.
endinterface.
CLASS zcl_bc_multiton_demo DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
interfaces ZIF_BC_MULTITON.
data:
gv_id type char15,
gv_erdat type erdat,
gv_ernam type ernam.
methods:
constructor importing iv_id type char15.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_bc_multiton_demo IMPLEMENTATION.
method constructor.
gv_id = iv_id.
gv_erdat = sy-datum.
gv_Ernam = sy-uname.
endmethod.
METHOD zif_bc_multiton~get_instance.
ro_obj ?= new zcl_Bc_multiton_demo( conv #( iv_objectid ) ).
ENDMETHOD.
ENDCLASS.
CLASS zcl_bc_multiton DEFINITION
PUBLIC
FINAL
CREATE public .
PUBLIC SECTION.
class-METHODS:
get_obj
IMPORTING
!iv_clsname TYPE seoclsname
!iv_objectid TYPE cdobjectv
RETURNING
VALUE(ro_obj) TYPE REF TO zif_bc_multiton
RAISING
cx_sy_create_object_error.
PROTECTED SECTION.
PRIVATE SECTION.
TYPES:
BEGIN OF t_multiton,
clsname TYPE seoclsname,
objectid TYPE cdobjectv,
cx TYPE REF TO cx_sy_create_object_error,
obj TYPE REF TO zif_bc_multiton,
END OF t_multiton,
tt_multiton
TYPE HASHED TABLE OF t_multiton
WITH UNIQUE KEY primary_key COMPONENTS clsname objectid.
class-DATA:
gt_multiton TYPE tt_multiton.
ENDCLASS.
CLASS zcl_bc_multiton IMPLEMENTATION.
METHOD get_obj.
ASSIGN gt_multiton[
KEY primary_key COMPONENTS
clsname = iv_clsname
objectid = iv_objectid
] TO FIELD-SYMBOL(<ls_mt>).
IF sy-subrc NE 0.
DATA(ls_mt) = VALUE t_multiton(
clsname = iv_clsname
objectid = iv_objectid
).
TRY.
CALL METHOD (ls_mt-clsname)=>zif_bc_multiton~get_instance
EXPORTING
iv_objectid = ls_mt-objectid
RECEIVING
ro_obj = ls_mt-obj.
CATCH cx_sy_create_object_error INTO ls_mt-cx ##no_handler.
CATCH cx_root INTO DATA(lo_diaper).
ls_mt-cx = NEW #(
textid = cx_sy_create_object_error=>cx_sy_create_object_error
classname = CONV #( ls_mt-clsname )
previous = lo_diaper
).
ENDTRY.
INSERT ls_mt
INTO TABLE gt_multiton
ASSIGNING <ls_mt>.
ENDIF.
IF <ls_mt>-cx IS NOT INITIAL.
RAISE EXCEPTION <ls_mt>-cx.
ENDIF.
ro_obj = <ls_mt>-obj.
ENDMETHOD.
ENDCLASS.
DATA(lo_obj) = NEW zcl_bc_multiton_demo( 'DUMMY' ).
DATA(lo_obj) = CAST zcl_bc_multiton_demo(
zcl_bc_multiton=>get_obj(
iv_clsname = 'ZCL_BC_MULTITON_DEMO'
iv_objectid = 'DUMMY'
)
).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
3 | |
2 | |
2 | |
2 | |
2 | |
2 | |
1 | |
1 | |
1 |