例子1:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
class
Service:
def
__init__(
self
, service_name, process_name, port, enable_monitor
=
None
):
self
.service_name
=
service_name
self
.process_name
=
process_name
self
.port
=
port
self
.mon
=
enable_monitor
self
._process_status
=
True
self
._port_status
=
True
@
property
def
process_status(
self
):
return
self
._process_status
@process_status.setter
def
process_status(
self
, status):
self
._process_status
=
status
self
.mon.start_mon()
@
property
def
port_status(
self
):
return
self
._port_status
@port_status.setter
def
port_status(
self
, status):
self
._port_status
=
status
self
.mon.start_mon()
class
Action:
@
classmethod
def
send_sms_alarm(
cls
, content):
print
(
"SMS Alarm: {}"
.
format
(content))
@
classmethod
def
send_email_alarm(
cls
, content):
print
(
"Email Alarm: {}"
.
format
(content))
class
Monitor:
def
__init__(
self
):
self
.services
=
[]
def
add_service(
self
, service):
self
.services.append(service)
def
start_mon(
self
):
for
ser
in
self
.services:
if
not
ser.process_status:
Action.send_email_alarm(
"Service: {0} Process: {1} Status: {2}"
.
format
(
ser.service_name, ser.process_name, ser.process_status))
if
not
ser.port_status:
Action.send_email_alarm(
"Service: {0} Process: {1} Status: {2}"
.
format
(
ser.service_name, ser.port, ser.port_status))
if
__name__
=
=
'__main__'
:
mon
=
Monitor()
http
=
Service(
"http"
,
"httpd"
,
80
, mon)
mysql
=
Service(
"mysql"
,
"mysqld"
,
3306
, mon)
zabbix
=
Service(
"zabbix"
,
"zabbixd"
,
1501
, mon)
mon.add_service(http)
mon.add_service(mysql)
mon.add_service(zabbix)
#mon.start_mon()
http.port_status
=
False
|
例子2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
#coding:utf-8
#Inventory类描述仓库对象
class
Inventory:
def
__init__(
self
):
self
.observers
=
[]
#此列表用于存储观察者对象
self
._product
=
None
#产品
self
._quantity
=
0
#数量
def
attach(
self
, observer):
#此方法用于将观察者对象添加进列表
self
.observers.append(observer)
@
property
#使用property装饰器修饰,使方法变成属性
def
product(
self
):
return
self
._product
@product.setter
#使用setter修饰product属性使其可以设置值
def
product(
self
, value):
self
._product
=
value
self
._update_observers()
#只要设置了产品的值,就调用 _update_observers方法
@
property
#对数量的设置
def
quantity(
self
):
return
self
._quantity
@quantity.setter
def
quantity(
self
, value):
self
._quantity
=
value
self
._update_observers()
#只要设置了数量的值,就调用 _update_observers方法
def
_update_observers(
self
):
for
observer
in
self
.observers:
#遍历观察者对象
observer()
#直接用()号调用观察者对象,之所以可以直接调用,是因为在ConsoleObserver类中实现了__call__方法
#ConsoleObserver类描述观察者对象
class
ConsoleObserver:
def
__init__(
self
, inventory):
self
.inventory
=
inventory
def
__call__(
self
):
#实现__call__方法后可直接可调用,这里的工作是print了产品和数量信息
print
(
"product: {0}, quantity: {1}"
.
format
(
self
.inventory.product,
self
.inventory.quantity))
if
__name__
=
=
'__main__'
:
i
=
Inventory()
#创建仓库对象
c
=
ConsoleObserver(i)
#创建一个观察者对象,并将仓库对象作为初始化参数
i.attach(c)
#将观察者对象添加到仓库对象中的observers列表里(两个对象之间的交互)
i.product
=
"Widget"
#仓库对象设置产品名称(有发生改动)
i.quantity
=
5
#仓库对象设置产品数量(有发生改动)
'''
工作流分析:
1、首先增加了一个产品名称,因此通知观察者打印,此时打印出了新添加的产品名称,数量默认为0
2、之后,又增加了数量5,那么又通知了观察者,此时打印出了之前添加的产品名称和这一次新增加的数量5
'''
|
本文转自 TtrToby 51CTO博客,原文链接:http://blog.51cto.com/freshair/2068926