技术开发 频道

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

   定义component,在我们的例子中是超类drink,注意它是一个抽象类

    *----------------------------------------------------------------------*

    
* INCLUDE ZBOBO_DP_003_IF_CL *

    
*----------------------------------------------------------------------*

    
*For the Decorator pattern, normally using an abstract super class

    
*And the Decorator class inherite from the super class also as an

    
* abstract class

    
*Super abstract class with drink

    
class drink definition abstract.

    
public section.

    data: desc type string.

    methods:

    
* Get the drink's description

    getdesc returning value(de) type string,

    
* Because the cost must be calculate from every concrete material

    
* It should be an abstract method

    cost
abstract returning value(co) type f.

    endclass.

    
*Implement the drink class

    
class drink implementation.

    method getdesc.

    
* Return the description

    de
= desc.

    endmethod.

    endclass.

    定义concrete component,在我们的例子中,它是饮料类的一个子类darkroast

    *An concrete class for drink, as one need to be decorated

    
class darkroast definition inheriting from drink.

    
public section.

    methods:

    
* initialization

    constructor,

    
* The subclass should implement abstract method from super

    cost redefinition.

    endclass.

    
*Implement darkroast

    
class darkroast implementation.

    method constructor.

    call method
super->constructor.

    
* Give a new description

    desc
= 'Darkroast'.

    endmethod.

    method cost.

    
* Get the raw material cost

    co
= '1.99'.

    endmethod.

    endclass.
0
相关文章