ABAP与设计模式之策略模式

 

本文整合自两篇网文,略做加工,以与前篇保持统一风格.

 

原文地址: http://tech.it168.com/a2009/0731/616/000000616596.shtml / http://hi.baidu.com/oabap/item/396ef07827a99a10d0dcb3dd

 

首先介绍策略模式的基本内容
    1. 意图:
    定义一系列算法,把他们一个个的封装起来,并且使他们可以互相替换.本模式可以使得算法可以独立于使用他的客户而变化.
    2. 结构:
         ABAP与设计模式之策略模式_第1张图片

     3. 适用性:
         1).许多相关的类仅仅是行为有异.
         2).需要使用一个算法的不同变体.
         3).一个类定义了多种行为,并且这些行为在这个类的操作中以多个条件语句的形式出现.
   

    4. 参与者:
        1).Strategy
             定义所支持的算法的公共接口.context使用这个接口来调用某个concrete strategy所定义的算法.
        2).ConcreteStrategy
             以strategy接口实现具体的算法
        3).context
             用一个concretestrategy对象来配置维护一个strategy对象的引用,  可定义一个接口来让strategy访问它的数据
   

    5. 效果
       1).相关算法系列
       2).一个替代继承的方法
        3).消除一系列条件语句
   

    Note:
    对于效果,我想在以后的例子中就会有所体现,现在只是基本的定义以及结构,所以看上去有些晦涩难懂,不过不要着急,慢慢的就能理解了.
    策略模式的例子(UML类图):
    ABAP与设计模式之策略模式_第2张图片

     程序代码:

    

REPORT  zgary_t014.

INCLUDE zgary_t014_define.

START-OF-SELECTION.

  DATA: grf_mallard_duck TYPE REF TO lcl_mallard_duck.
  CREATE OBJECT grf_mallard_duck.

  CALL METHOD grf_mallard_duck->performquack.
  CALL METHOD grf_mallard_duck->performfly.
  CALL METHOD grf_mallard_duck->display.

*&---------------------------------------------------------------------*
*&  Include           ZGARY_T014_DEFINE
*&---------------------------------------------------------------------*

INTERFACE li_fly_behavior.

  METHODS fly.

ENDINTERFACE.                    "

*----------------------------------------------------------------------*
*        INTERFACE li_quack_behavior
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
INTERFACE li_quack_behavior.

  METHODS quack.

ENDINTERFACE.                    "

*----------------------------------------------------------------------*
*        CLASS lcl_Duck DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_duck DEFINITION ABSTRACT.

  PUBLIC SECTION.
    METHODS:
    performfly,
    performquack,
    display ABSTRACT.

  PROTECTED SECTION.
    DATA: fly_behavior    TYPE REF TO li_fly_behavior,
    quack_behavior  TYPE REF TO li_quack_behavior.


ENDCLASS.                    "lcl_Duck DEFINITION

*----------------------------------------------------------------------*
*        CLASS lcl_fly_with_wings DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_fly_with_wings DEFINITION .
  PUBLIC SECTION.
    INTERFACES: li_fly_behavior.

ENDCLASS.                    "lcl_fly_with_wings DEFINITION

*----------------------------------------------------------------------*
*        CLASS lcl_quack DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_quack DEFINITION.
  PUBLIC SECTION.
    INTERFACES: li_quack_behavior.
ENDCLASS.                    "lcl_quack DEFINITION

*----------------------------------------------------------------------*
*        CLASS lcl_Squeak DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_squeak DEFINITION.
  PUBLIC SECTION.
    INTERFACES: li_quack_behavior.
ENDCLASS.                    "lcl_Squeak DEFINITION

*----------------------------------------------------------------------*
*        CLASS lcl_mute_quack DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_mute_quack DEFINITION.
  PUBLIC SECTION.
    INTERFACES: li_quack_behavior.
ENDCLASS.                    "lcl_mute_quack DEFINITION

*----------------------------------------------------------------------*
*        CLASS lcl_mallard_duck DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_mallard_duck DEFINITION INHERITING FROM lcl_duck.

  PUBLIC SECTION.
    METHODS: constructor,
    display REDEFINITION.

ENDCLASS.                    "lcl_mallard_duck DEFINITION
*----------------------------------------------------------------------*
*        CLASS lcl_duck IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_duck IMPLEMENTATION.

  METHOD performfly.
    CALL METHOD fly_behavior->fly.
  ENDMETHOD.                    "performfly

  METHOD performquack.
    CALL METHOD quack_behavior->quack.
  ENDMETHOD.                    "performquack

ENDCLASS.                    "lcl_duck IMPLEMENTATION

*----------------------------------------------------------------------*
*        CLASS lcl_fly_with_wings IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_fly_with_wings IMPLEMENTATION.
  METHOD li_fly_behavior~fly.

    WRITE: / 'I''m flying'.

  ENDMETHOD.                    "li_fly_behavior~fly

ENDCLASS.                    "lcl_fly_with_wings IMPLEMENTATION

*----------------------------------------------------------------------*
*        CLASS lcl_quack IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_quack IMPLEMENTATION.
  METHOD li_quack_behavior~quack.
    WRITE: / 'quack'.
  ENDMETHOD.                    "li_quack_behavior~quack
ENDCLASS.                    "lcl_quack IMPLEMENTATION

*----------------------------------------------------------------------*
*        CLASS lcl_mute_quack IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_mute_quack IMPLEMENTATION.
  METHOD li_quack_behavior~quack.
    WRITE: / '<<Silence>>'.
  ENDMETHOD.                    "li_quack_behavior~quack
ENDCLASS.                    "lcl_mute_quack IMPLEMENTATION

*----------------------------------------------------------------------*
*        CLASS lcl_squeak IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_squeak IMPLEMENTATION.
  METHOD li_quack_behavior~quack.
    WRITE: / 'squeak'.
  ENDMETHOD.                    "li_quack_behavior~quack
ENDCLASS.                    "lcl_squeak IMPLEMENTATION

*----------------------------------------------------------------------*
*        CLASS lcl_mute_quack IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS lcl_mallard_duck IMPLEMENTATION.
  METHOD constructor.
    CALL METHOD super->constructor.
    DATA: lrf_quack_behavior TYPE REF TO lcl_quack,
    lrf_fly_behavior TYPE REF TO lcl_fly_with_wings.

    CREATE OBJECT: lrf_quack_behavior,
    lrf_fly_behavior.

    quack_behavior = lrf_quack_behavior.
    fly_behavior    = lrf_fly_behavior.

    CLEAR: lrf_quack_behavior,
    lrf_fly_behavior.
  ENDMETHOD.                    "constructor

  METHOD  display .
    WRITE: / 'I''m a real Mallard duck'.
  ENDMETHOD.                    "display
ENDCLASS.                    "lcl_mute_quack IMPLEMENTATIO


运行结果:

ABAP与设计模式之策略模式_第3张图片

 

注:代码部分来自 http://hi.baidu.com/oabap/item/396ef07827a99a10d0dcb3dd

    策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换,参考Headfirst 设计模式一书,使用abap OO实现

 

你可能感兴趣的:(ABAP与设计模式之策略模式)