基本原理请参考相应书籍,这里直接给实例
观察者模式通过主题订阅的形式使得系统两个部分解耦。
基本说明本系统由两大部分组成:雷达(subject)和哨所(observer)
其中雷达分为: 空域雷达(RadarStationS) 和海域雷达(RadarStationB)
哨所分为:指挥本部(SentryO),一号哨所,二号哨所,三号哨所。
指挥本部订阅了这两种雷达信息
一号哨所、二号哨所 执行的是海域任务 ,订阅了海域雷达信息
三号哨所 执行的是空间任务 ,订阅了空间雷达信息
# -*- coding: GB18030 -*-
######################################################
#
# Observer.py
# Python implementation of the Class Observer
# Generated by Enterprise Architect
# Created on: 15-十二??2012 8:30:17
#
#######################################################
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from future_builtins import *
class Observer(object):
"""This class defines an updating interface for objects that should be notified of
changes in a subject.
"""
def __init__(self, name=None):
self.name=name
pass
def Update(self, radar):
str=self.name+'\n 收到情报:'+radar.GetState()+ " 情报来源:"+radar.name +'\n 执行:'+self.work
print(str)
pass
class SentryA(Observer):
"""This class defines an updating interface for objects that should be notified of
changes in a subject.
"""
def __init__(self, name='一号哨所'):
super(SentryA,self).__init__(name)
self.work='海监船马上跟进,执行海巡'
pass
class SentryB(Observer):
"""This class defines an updating interface for objects that should be notified of
changes in a subject.
"""
def __init__(self, name='二号哨所'):
super(SentryB,self).__init__(name)
self.work='护卫舰护航,进入戒备状态'
pass
class SentryC(Observer):
"""This class defines an updating interface for objects that should be notified of
changes in a subject.
"""
def __init__(self, name='三号哨所'):
super(SentryC,self).__init__(name)
self.work='马上执行空巡,进一步确定情况'
pass
class SentryO(Observer):
"""This class defines an updating interface for objects that should be notified of
changes in a subject.
"""
def __init__(self, name='指挥本部'):
super(SentryO,self).__init__(name)
self.work='按党中央统一部署执行任务'
pass
class Subject(object):
"""This class knows its observers and provides an interface for attaching and
detaching Observer objects.
"""
m_Observer= Observer()
def __init__(self, name):
"""m_Observer= Observer()
"""
self.name=name
self.ls=list()
self.state=None
pass
def Attach(self, observer):
self.ls.append(observer)
pass
def Detach(self, obsever):
if obsever in self.ls:
self.ls.remove(observer)
pass
def GetState(self):
return self.state
pass
def Notify(self):
for observer in self.ls:
observer.Update(self)
pass
pass
def SetState(self, state):
self.state=state
pass
class RadarStationA(Subject):
"""This class stores the state of interest to ConcreteObserver objects and sends
notification to its observers when its state changes.
"""
def __init__(self, name='空域雷达'):
super(RadarStationA,self).__init__(name)
pass
class RadarStationB(Subject):
"""This class stores the state of interest to ConcreteObserver objects and sends
notification to its observers when its state changes.
"""
def __init__(self, name='海域雷达'):
super(RadarStationB,self).__init__(name)
pass
pass
#客户端
if(__name__=="__main__"):
#观察者
m_SentryA=SentryA()
m_SentryB=SentryB()
m_SentryC=SentryC()
m_SentryO=SentryO()
#主题(监控雷达)
m_RadarStationA=RadarStationA()
m_RadarStationB=RadarStationB()
#订阅主题A(空间监控雷达)
m_RadarStationA.Attach(m_SentryO)
m_RadarStationA.Attach(m_SentryC)
#订阅主题B(海域监控雷达)
m_RadarStationB.Attach(m_SentryO)
m_RadarStationB.Attach(m_SentryA)
m_RadarStationB.Attach(m_SentryB)
#主题A监控到信息,并通知订阅??
m_RadarStationA.SetState('我领空发现不明飞行物')
m_RadarStationA.Notify()
print('\n')
#主题B监控到信息,并通知订阅??
m_RadarStationB.SetState('我海域发现不明船船只')
m_RadarStationB.Notify()
#运行结果: