Python设计模式详解之16 —— 观察者模式

Python 中的 Observer(观察者)设计模式 是一种行为型设计模式,用于定义一种订阅机制,让多个观察者对象在某一对象的状态发生变化时自动接收到通知。该模式常用于事件驱动的架构,例如 GUI 系统、消息推送系统等。


Observer 模式的组成

  1. Subject(被观察者)

    • 持有观察者列表,并提供添加、移除观察者的方法。
    • 当状态变化时,通知所有观察者。
  2. Observer(观察者)

    • 定义接口,接收来自 Subject 的更新。
  3. ConcreteSubject 和 ConcreteObserver

    • ConcreteSubject 负责存储具体的状态并通知观察者。
    • ConcreteObserver 实现观察者接口,响应状态变化。

如何在 Python 中实现 Observer 模式

示例:股票价格通知系统
python 复制代码
# 观察者接口
class Observer:
    def update(self, subject):
        raise NotImplementedError("Subclasses must implement this method.")

# 被观察者基类
class Subject:
    def __init__(self):
        self._observers = []  # 存储观察者

    def attach(self, observer):
        if observer not in self._observers:
            self._observers.append(observer)

    def detach(self, observer):
        self._observers.remove(observer)

    def notify(self):
        for observer in self._observers:
            observer.update(self)

# 具体的被观察者
class Stock(Subject):
    def __init__(self, name, price):
        super().__init__()
        self.name = name
        self._price = price

    @property
    def price(self):
        return self._price

    @price.setter
    def price(self, new_price):
        if self._price != new_price:
            self._price = new_price
            self.notify()  # 通知观察者价格已变化

# 具体的观察者
class Investor(Observer):
    def __init__(self, name):
        self.name = name

    def update(self, subject):
        print(f"通知 {self.name}: {subject.name} 的价格已更新为 {subject.price}.")

# 使用 Observer 模式
if __name__ == "__main__":
    # 创建股票对象
    google_stock = Stock("Google", 1500)

    # 创建投资者对象
    investor_a = Investor("Alice")
    investor_b = Investor("Bob")

    # 添加观察者
    google_stock.attach(investor_a)
    google_stock.attach(investor_b)

    # 修改价格并触发通知
    google_stock.price = 1550
    google_stock.price = 1600

    # 移除观察者
    google_stock.detach(investor_a)
    google_stock.price = 1650
输出结果:
复制代码
通知 Alice: Google 的价格已更新为 1550.
通知 Bob: Google 的价格已更新为 1550.
通知 Alice: Google 的价格已更新为 1600.
通知 Bob: Google 的价格已更新为 1600.
通知 Bob: Google 的价格已更新为 1650.

使用 Python 内置的 weakref.WeakSet 优化

weakref.WeakSet 可以防止因为观察者未被移除而导致的内存泄漏:

python 复制代码
from weakref import WeakSet

class Subject:
    def __init__(self):
        self._observers = WeakSet()

    def attach(self, observer):
        self._observers.add(observer)

    def detach(self, observer):
        self._observers.discard(observer)

    def notify(self):
        for observer in self._observers:
            observer.update(self)

使用 Python 的内置库实现

Python 的 weakrefObservable 可用于更高级的实现,例如通过 abc 模块定义接口或利用事件机制。

使用 Observer Pattern 的替代方案
  • asyncio:实现异步事件通知。
  • pubsub(发布订阅库) :如 PyPubSub
  • GUI 框架自带的信号与槽:例如 PyQt。

适用场景

  1. 事件驱动系统:按钮点击事件、输入框监听。
  2. 实时通知系统:新闻订阅、股票更新。
  3. 解耦系统模块:通过观察者减少模块间的直接依赖。

总结

Observer 模式在 Python 中实现起来非常自然,且可以根据实际需求选择基本实现或利用更高级的特性(如弱引用、生成器等)优化。

相关推荐
chilavert3181 小时前
关于Python 实现接口安全防护:限流、熔断降级与认证授权的深度实践
python·网络安全
能来帮帮蒟蒻吗1 小时前
Python -将MP4文件转为GIF图片
开发语言·python·学习·视频
suoxiao7771 小时前
通过anaconda安装jupyter
ide·python·jupyter
百锦再1 小时前
MK米客方德SD NAND:无人机存储的高效解决方案
人工智能·python·django·sqlite·android studio·无人机·数据库开发
PacosonSWJTU2 小时前
python使用matplotlib画图
开发语言·python·matplotlib
伶俐角少儿编程2 小时前
2023年12月中国电子学会青少年软件编程(Python)等级考试试卷(六级)答案 + 解析
python·青少年编程·少儿编程·中国电子学会等级考试·中国电子学会
tangjunjun-owen2 小时前
Milvus 2.4 使用详解:从零构建向量数据库并实现搜索功能(Python 实战)
数据库·python·milvus·rag
CryptoRzz2 小时前
印度尼西亚数据源对接技术指南
开发语言·python·websocket·金融·区块链
敲代码的 蜡笔小新2 小时前
【行为型之策略模式】游戏开发实战——Unity灵活算法架构的核心实现策略
unity·设计模式·c#·策略模式
_yingty_2 小时前
Java设计模式-策略模式(行为型)
java·设计模式·策略模式