news 2026/4/18 3:25:02

2025-简单点-python设计模式之中介者模式

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
2025-简单点-python设计模式之中介者模式

中介者是一种行为设计模式, 让程序组件通过特殊的中介者对象进行间接沟通, 达到减少组件之间依赖关系的目的。

中介者能使得程序更易于修改和扩展, 而且能更方便地对独立的组件进行复用, 因为它们不再依赖于很多其他的类。

使用示例: 中介者模式在 Python 代码中最常用于帮助程序 GUI 组件之间的通信。 在 MVC 模式中, 控制器是中介者的同义词。

from__future__importannotationsfromabcimportABCclassMediator(ABC):""" The Mediator interface declares a method used by components to notify the mediator about various events. The Mediator may react to these events and pass the execution to other components. """defnotify(self,sender:object,event:str)->None:passclassConcreteMediator(Mediator):def__init__(self,component1:Component1,component2:Component2)->None:self._component1=component1 self._component1.mediator=self self._component2=component2 self._component2.mediator=selfdefnotify(self,sender:object,event:str)->None:ifevent=="A":print("Mediator reacts on A and triggers following operations:")self._component2.do_c()elifevent=="D":print("Mediator reacts on D and triggers following operations:")self._component1.do_b()self._component2.do_c()classBaseComponent:""" The Base Component provides the basic functionality of storing a mediator's instance inside component objects. """def__init__(self,mediator:Mediator=None)->None:self._mediator=mediator@propertydefmediator(self)->Mediator:returnself._mediator@mediator.setterdefmediator(self,mediator:Mediator)->None:self._mediator=mediator""" Concrete Components implement various functionality. They don't depend on other components. They also don't depend on any concrete mediator classes. """classComponent1(BaseComponent):defdo_a(self)->None:print("Component 1 does A.")self.mediator.notify(self,"A")defdo_b(self)->None:print("Component 1 does B.")self.mediator.notify(self,"B")classComponent2(BaseComponent):defdo_c(self)->None:print("Component 2 does C.")self.mediator.notify(self,"C")defdo_d(self)->None:print("Component 2 does D.")self.mediator.notify(self,"D")if__name__=="__main__":# The client code.c1=Component1()c2=Component2()mediator=ConcreteMediator(c1,c2)print("Client triggers operation A.")c1.do_a()print("\n",end="")print("Client triggers operation D.")c2.do_d()

输出:

Client triggers operation A. Component 1 does A. Mediator reacts on A and triggers following operations: Component 2 does C. Client triggers operation D. Component 2 does D. Mediator reacts on D and triggers following operations: Component 1 does B. Component 2 does C.

可以看出是让中介去写触发之后的逻辑链条。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/16 12:38:50

Docker MCP 网关负载均衡优化全攻略(企业级部署必备技术手册)

第一章:Docker MCP 网关负载均衡概述在现代微服务架构中,Docker 容器化技术被广泛用于实现服务的快速部署与弹性伸缩。随着容器实例数量的动态变化,如何高效地将外部请求分发到多个容器实例成为关键问题。Docker MCP(Microservice…

作者头像 李华
网站建设 2026/4/16 16:05:59

mysql命令行手动导入csv数据到指定表

问题 需要通过本地csv文件将数据导入到mysql表中。 步骤 登录数据库 首先使用mysql cli登录到数据库,注意,使用cli登录数据库的时候,需要启用--local-infile权限参数。类似如下: mysql -h xxxx.cn -u root --ssl --local-inf…

作者头像 李华
网站建设 2026/4/16 18:24:35

逆向山姆小程序搜索接口

断断续续弄了2-3次,一直跑不通。 今天良辰吉日,直接跑出数据了。 自己改一下pagesize,很方便。 接口比较复杂,小程序会把json改成protobuf上传网关,然后网关发包访问具体接口。然后再下发加密数据到小程序。

作者头像 李华
网站建设 2026/4/8 4:35:35

紧急修复OCR识别异常:Dify环境下Tesseract字体配置的4个关键点

第一章:Dify Tesseract 的字体适配在使用 Dify 集成 Tesseract 进行 OCR 文本识别时,字体适配是影响识别准确率的关键因素之一。Tesseract 默认训练模型基于标准字体(如 Times New Roman、Arial),当输入图像包含非常规…

作者头像 李华
网站建设 2026/4/14 17:10:46

【Dify 1.7.0音频处理终极指南】:掌握高效音频切片配置的5大核心技巧

第一章:Dify 1.7.0音频切片处理的核心架构Dify 1.7.0在音频处理能力上实现了重要升级,尤其在音频切片模块引入了高并发、低延迟的流式处理架构。该架构基于微服务设计,将音频输入、分段检测、元数据提取与存储解耦,提升了系统的可…

作者头像 李华