【IT168 技术文章】
这章我们介绍观察者模式,下面先给出观察者模式的例子的类图:
首先给出测试程序的代码:
REPORT ZBOBO_DP_002_RE .
*Include file for the class and interface
include zbobo_dp_002_cl_if.
*Declare data
data:
* Weather data class reference object
wd type ref to weather_data,
* Define subject interface reference object
su type ref to subject,
* concrete observers object
* crrent condition object
cu type ref to current_condition_display,
* For statistics data object
sd type ref to statistics_display.
start-of-selection.
* Create weather data object.
create object wd.
* Widening cast,make the subject interface reference to
* weather data class
su ?= wd.
* Register concrete obsever to subject interface which
* point to weather data object
* For concrete observer current condition display
create object cu
exporting wd = su.
* For concrete observer statistics data
create object sd
exporting wd = su.
* Change the weather data,and if the data changed, the subject
* will inform all of its observers, including current_condition_display
* and statistics_display
call method wd->set_measurements
exporting tem = '80'
hum = '80'
pre = '80'.
call method wd->set_measurements
exporting tem = '81'
hum = '82.4'
pre = '80'.
call method wd->set_measurements
exporting tem = '79'
hum = '80'
pre = '80'.
*Include file for the class and interface
include zbobo_dp_002_cl_if.
*Declare data
data:
* Weather data class reference object
wd type ref to weather_data,
* Define subject interface reference object
su type ref to subject,
* concrete observers object
* crrent condition object
cu type ref to current_condition_display,
* For statistics data object
sd type ref to statistics_display.
start-of-selection.
* Create weather data object.
create object wd.
* Widening cast,make the subject interface reference to
* weather data class
su ?= wd.
* Register concrete obsever to subject interface which
* point to weather data object
* For concrete observer current condition display
create object cu
exporting wd = su.
* For concrete observer statistics data
create object sd
exporting wd = su.
* Change the weather data,and if the data changed, the subject
* will inform all of its observers, including current_condition_display
* and statistics_display
call method wd->set_measurements
exporting tem = '80'
hum = '80'
pre = '80'.
call method wd->set_measurements
exporting tem = '81'
hum = '82.4'
pre = '80'.
call method wd->set_measurements
exporting tem = '79'
hum = '80'
pre = '80'.