下面定义另外一个观察者:statistics_display
*Display statistics data
class statistics_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:
av_temp type f, "Average temperature
mx_temp type f, "Max temperature
mi_temp type f. "Min temperature
data: wd type ref to weather_data.
endclass.
class statistics_display implementation.
method constructor.
* Register observer
me->wd ?= wd.
call method me->wd->subject~register_observer
exporting o = me.
endmethod.
method observer~update.
* Local data definition
* Table record number
data: num type i.
* Local cal data
data:
tav_temp type f,
tmx_temp type f,
tmi_temp type f.
* Store all the temperature which setted
data:
begin of r_result,
temp type f,
end of r_result.
data: t_result like table of r_result.
* Get the temperature and add it to table
r_result-temp = temp.
append r_result to t_result.
describe table t_result lines num.
if num <> 0.
sort t_result ascending.
* Get min temp
read table t_result index 1 into r_result.
tmi_temp = r_result-temp.
* Get max temp
read table t_result index num into r_result.
tmx_temp = r_result-temp.
* Get avg temp
loop at t_result into r_result.
tav_temp = tav_temp + r_result-temp.
endloop.
tav_temp = tav_temp / num .
* update instance variants
mi_temp = tmi_temp.
mx_temp = tmx_temp.
av_temp = tav_temp.
endif.
* Display result
call method display_element~display.
endmethod.
method display_element~display.
skip.
write: / 'Statistics display:'.
write: / 'Average temp:', av_temp decimals 2 exponent 0.
write: / 'Max temp:', mx_temp decimals 2 exponent 0.
write: / 'Min temp:', mi_temp decimals 2 exponent 0.
endmethod.
endclass.
class statistics_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:
av_temp type f, "Average temperature
mx_temp type f, "Max temperature
mi_temp type f. "Min temperature
data: wd type ref to weather_data.
endclass.
class statistics_display implementation.
method constructor.
* Register observer
me->wd ?= wd.
call method me->wd->subject~register_observer
exporting o = me.
endmethod.
method observer~update.
* Local data definition
* Table record number
data: num type i.
* Local cal data
data:
tav_temp type f,
tmx_temp type f,
tmi_temp type f.
* Store all the temperature which setted
data:
begin of r_result,
temp type f,
end of r_result.
data: t_result like table of r_result.
* Get the temperature and add it to table
r_result-temp = temp.
append r_result to t_result.
describe table t_result lines num.
if num <> 0.
sort t_result ascending.
* Get min temp
read table t_result index 1 into r_result.
tmi_temp = r_result-temp.
* Get max temp
read table t_result index num into r_result.
tmx_temp = r_result-temp.
* Get avg temp
loop at t_result into r_result.
tav_temp = tav_temp + r_result-temp.
endloop.
tav_temp = tav_temp / num .
* update instance variants
mi_temp = tmi_temp.
mx_temp = tmx_temp.
av_temp = tav_temp.
endif.
* Display result
call method display_element~display.
endmethod.
method display_element~display.
skip.
write: / 'Statistics display:'.
write: / 'Average temp:', av_temp decimals 2 exponent 0.
write: / 'Max temp:', mx_temp decimals 2 exponent 0.
write: / 'Min temp:', mi_temp decimals 2 exponent 0.
endmethod.
endclass.