技术开发 频道

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

     定义装饰者抽象类,注意,他只不过继承了drink类,并没有作什么,我们需要的只不过是一个接口,一个装饰者和被装饰者的交互接口。

   *Decorator definition, which will decorate the raw material

    
*The decorator should be as abstract class

    
*It is just for supply an interface, it won't implement

    
*any method of super class

    
*Or you could define new method here so that the subclass

    
*of decorator should have new method in it

    
class decorator definition abstract

    inheriting from drink.

    endclass.

    
class decorator implementation.

    endclass.
 

    定义具体的装饰者

    *Define the concrete decorator which will used to decorate

    
* the concrete drink object, for exp: darkroast

    
class mocha definition inheriting from decorator.

    
public section.

    
* Define the interface which will point to super class drink

    data:

    drink type ref to drink.

    methods:

    
* Ininitialization

    constructor

    importing dr type ref to drink,

    
* Redifine the getdesc method so that we can get the right name

    
* of the decorated drink

    getdesc redefinition,

    
* Redifine the cost method so that we can get the right price

    
* of the decorated drink

    cost redefinition.

    endclass.

    
class mocha implementation.

    method constructor.

    call method
super->constructor.

    
* Make drink instance variant point to decorator

    
* For example, if darkroast decorated with mocha

    
* the reference drink shoul be pointed to darkroast

    drink
= dr.

    endmethod.

    method getdesc.

    
* This method will show how many decorate material we used

    data: ls_mocha type string.

    ls_mocha
= drink->getdesc( ).

    concatenate ls_mocha
',Mocha' into de.

    endmethod.

    method cost.

    
* Calculate the total price of the new drink which be decorated

    
* by the decorator

    data: lf_mocha type f.

    lf_mocha
= drink->cost( ).

    co
= lf_mocha + '0.20'.

    endmethod.

    endclass.
0
相关文章