技术开发 频道

ABAP与设计模式之装饰者模式

    定义其他的装饰者,和上面的差不多

    *The below part is mostly the same as mocha, because all of them

    
*are decorators for the drink raw material

    
class soy definition inheriting from decorator.

    
public section.

    data:

    drink type ref to drink.

    methods:

    constructor

    importing dr type ref to drink,

    getdesc redefinition,

    cost redefinition.

    endclass.

    
class soy implementation.

    method constructor.

    call method
super->constructor.

    drink
= dr.

    endmethod.

    method getdesc.

    data: lssoy type string.

    lssoy
= drink->getdesc( ).

    concatenate lssoy
',Soy' into de.

    endmethod.

    method cost.

    data: lfsoy type f.

    lfsoy
= drink->cost( ).

    co
= lfsoy + '0.40'.

    endmethod.

    endclass.

    
class whip definition inheriting from decorator.

    
public section.

    data:

    drink type ref to drink.

    methods:

    constructor

    importing dr type ref to drink,

    getdesc redefinition,

    cost redefinition.

    endclass.

    
class whip implementation.

    method constructor.

    call method
super->constructor.

    drink
= dr.

    endmethod.

    method getdesc.

    data: lswhip type string.

    lswhip
= drink->getdesc( ).

    concatenate lswhip
',Whip' into de.

    endmethod.

    method cost.

    data: lfwhip type f.

    lfwhip
= drink->cost( ).

    co
= lfwhip + '0.60'.

    endmethod.

    endclass.

    这个程序比较难理解,有点像我们平时写的递归,下面我把getdesc方法的顺序画出来就比较容易理解了,说明如下:

    显示的图片不是很清楚地话,你可以用右键--〉图片另存为到本地就可以看清楚了。

0
相关文章