ABAPデザインパターンの例-ブリッジモード

個人的な理解

鉛筆の色やモデルなどの2つのディメンションの場合、最初に一方のディメンションをインターフェイスとして抽象化して実装します。もう一方のディメンションの場合は、仮想クラスを設計し、最初のインターフェイスを(保護された属性として)関連付け、継承します。仮想クラス、別の次元を実現

バックグラウンド

ERP市場には、SAPやOracleなどの製品があります。各製品には、MM、PP、SDなどのモジュールが含まれています。製品はいつでも追加され、新しいモジュールは引き続き追加されます。

サンプルコード

コードは開閉の原則に準拠しています。新しいディメンションの場合は、実装クラスのみを追加する必要があります。
同時に、依存性逆転の原則、つまり、各実装クラスのインターフェイス指向プログラミングにも準拠しています。対応するインターフェースと仮想クラスがあります

"维度1接口
interface if_module.
  methods:get_module returning value(e_module_name) type string.
endinterface.

"维度1实现类1
class mm definition create public.
  public section.
    interfaces:if_module.
endclass.
class mm implementation.
  method if_module~get_module.
    e_module_name = 'MM'.
  endmethod.
endclass.

"维度1实现类2
class pp definition create public.
  public section.
    interfaces:if_module.
endclass.
class pp implementation.
  method if_module~get_module.
    e_module_name = 'PP'.
  endmethod.
endclass.

"维度2抽象类
class erp definition create public abstract.
  public section.
    methods: show_info,
      constructor importing io_module type ref to if_module.

  protected section.
  	**"关联维度1**
    data:mo_module type ref to if_module.
endclass.
class erp implementation.
  method show_info.

  endmethod.
  method constructor.
    mo_module = io_module.
  endmethod.
endclass.

"维度2实现类1
class sap definition inheriting from erp create public.
  public section.
    methods:show_info redefinition.
endclass.
class sap implementation.
  method show_info.
    write: / '系统SAP包含', mo_module->get_module( ), '模块'.
  endmethod.
endclass.

"维度2实现类2
class oracle definition inheriting from erp create public.
  public section.
    methods:show_info redefinition.
endclass.
class oracle implementation.
  method show_info.
    write: / '系统Oracle包含', mo_module->get_module( ), '模块'.
  endmethod.
endclass.

start-of-selection.
  data(lo_erp) = new sap( new pp( ) ).
  lo_erp->show_info( ).

  data(lo_erp2) = new oracle( new mm( ) ).
  lo_erp2->show_info( ).

おすすめ

転載: blog.csdn.net/u012232542/article/details/106645566