ABAP 디자인 모드 인 스 턴 스 - 어댑터 모드

18999 단어 OO디자인 모드
배경
어댑터 모드 는 두 가지 실현 방식 이 있 습 니 다. 클래스 어댑터 와 대상 어댑터 는 ABAP 가 다 중 계승 을 지원 하지 않 기 때문에 클래스 어댑터 는 실제 적 으로 하나의 Adaptee (어댑터) 만 맞 출 수 있 습 니 다.
기초 실현
클래스 어댑터
class adaptee definition create public.
  public section.
    methods: adaptee_request.
endclass.
class adaptee implementation.
  method adaptee_request.
    write:/ 'request from adaptee'.
  endmethod.
endclass.

interface if_target.
  methods: request.
endinterface.

class class_adapter definition  inheriting from adaptee create public.
  public section.
    interfaces:if_target.
endclass.

class class_adapter implementation.
  method if_target~request.
    me->adaptee_request( ).
  endmethod.
endclass.

start-of-selection.

  data(lo_class_adapter) = new class_adapter( ).
  lo_class_adapter->if_target~request( ).

대상 어댑터
class adaptee definition create public.
  public section.
    methods: adaptee_request.
endclass.
class adaptee implementation.
  method adaptee_request.
    write:/ 'request from adaptee'.
  endmethod.
endclass.

interface if_target.
  methods: request.
endinterface.

class object_adapter definition create public.
  public section.
    interfaces:if_target.
    methods:constructor
      importing io_adaptee type ref to adaptee.

  private section.
    data:mo_adaptee type ref to adaptee.
endclass.

class object_adapter implementation.
  method constructor.
    me->mo_adaptee = io_adaptee.
  endmethod.
  method if_target~request.
    mo_adaptee->adaptee_request( ).
  endmethod.
endclass.

start-of-selection.

  data(lo_adaptee) = new adaptee( ).
  data(lo_object_adapter) = new object_adapter( lo_adaptee ).
  lo_object_adapter->if_target~request( ).

예 를 들다
두 가지 다른 방법 으로 데 이 터 를 보 여 줍 니 다 (demo & SALV)
"  demo  
class demo_output definition create public.
  public section.
    methods: display_demo importing it_data type index table.
endclass.
class demo_output implementation.
  method display_demo.
    cl_demo_output=>display_data( it_data ).
  endmethod.
endclass.

"SALV  
class salv_output definition create public.
  public section.
    methods:display_salv importing it_data type index table.
endclass.
class salv_output implementation.
  method display_salv.

    data:lr_data type ref to data.
    field-symbols:<fs_data> type index table.

    create data lr_data like it_data.
    assign lr_data->* to <fs_data>.
    <fs_data> = corresponding #( it_data ).

    cl_salv_table=>factory(
    exporting
      list_display = if_salv_c_bool_sap=>true
      importing
        r_salv_table = data(lo_salv)
     changing
      t_table = <fs_data>
    ).

    lo_salv->display( ).
  endmethod.
endclass.

"     
class abstract_output definition abstract create public.
  public section.
    methods:display_data importing it_data type index table.
endclass.
class abstract_output implementation.
  method display_data.
    message '       ' type 'E'.
  endmethod.
endclass.

"DEMO   
class demo_adapter definition inheriting from abstract_output create public final.
  public section.
    methods: display_data redefinition.

  private section.
    data:mo_output type  ref to demo_output.
endclass.
class demo_adapter implementation.
  method display_data.
    mo_output = new demo_output( ).
    mo_output->display_demo( it_data ).
  endmethod.
endclass.

"SALV   
class salv_adapter definition inheriting from abstract_output create public final.
  public section.
    methods: display_data redefinition.

  private section.
    data:mo_output type  ref to salv_output.
endclass.
class salv_adapter implementation.
  method display_data.
    mo_output = new salv_output( ).
    mo_output->display_salv( it_data = it_data ).
  endmethod.
endclass.

"      
class lcl_main definition create public.
  public section.
    class-methods:class_constructor,
      excute.

  private section.
    class-data:mt_data   type table of zlookup,
               mo_adapter type ref to abstract_output.
endclass.
class lcl_main implementation.
  method class_constructor.

    select *
      into table @mt_data
      up to 10 rows
      from zlookup.

  endmethod.
  method excute.
    "demo display
    mo_adapter = new demo_adapter( ).
    mo_adapter->display_data( mt_data ).

    "alv display
    mo_adapter = new salv_adapter( ).
    mo_adapter->display_data( mt_data ).

  endmethod.
endclass.

start-of-selection.

  lcl_main=>excute( ).

좋은 웹페이지 즐겨찾기