技术开发 频道

ABAP与设计模式之观察者模式

    下面定义一个具体的观察者:current_condition_display

    *Concrete observers

    
*Display current weather data

    
class current_condition_display definition.

    
public section.

    
* Implement two interfaces, observer and display_element

    interfaces:

    observer,

    display_element.

    methods:

    
* Get the initial concrete subject object

    constructor

    importing wd type ref to subject.

    
private section.

    data:

    tem type f,

    hum type f.

    
* Concrete subject

    data w_data type ref to weather_data.

    endclass.

    
class current_condition_display implementation.

    
* Update data

    method observer
~update.

    me
->tem = temp.

    me
->hum = hum.

    call method display_element
~display.

    endmethod.

    
* Display data

    method display_element
~display.

    write:
/ 'Current conditions:'.

    write:
/ me->tem decimals 2 exponent 0,

    
'F degrees.'.

    write:
/ me->hum decimals 2 exponent 0,

    
'% humidity.'.

    endmethod.

    method constructor.

    
* Widening cast because wd's type which import is interface subject

    
* and the me->w_data's type is weather_data

    
* and weather data is more specialized than interface subject

    me
->w_data ?= wd.

    
* Register current condition observer to subject

    call method w_data
->subject~register_observer

    exporting o
= me.

    endmethod.

    endclass.
0
相关文章