当前位置: 首页 > news >正文

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.

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

http://www.zskr.cn/news/111443.html

相关文章:

  • 【DevSecOps进阶之路】:企业Agent镜像签名的3种高阶策略
  • 实时云渲染与云桌面解析(三):核心异同点深度解析
  • LobeChat能否对接Nuclino?现代团队协作文档AI增强
  • 为什么顶尖团队都在用Dify做可视化流程编排?真相曝光
  • 17、多线程同步函数与安全接口详解
  • 逆向山姆小程序搜索接口
  • 视频字幕检索的Dify缓存周期全解密(企业级缓存设计实战)
  • 14、多线程编程中的信号处理与 I/O 操作
  • 揭秘Docker Compose中的Agent服务扩展:90%开发者忽略的关键配置
  • 肌营养不良新突破:固本培元生肌疗法
  • 揭秘Docker MCP 网关负载均衡机制:5步实现无缝流量分发
  • 【量子计算镜像构建缓存全解析】:掌握高效量子环境部署的5大核心技术
  • Dify工作流依赖检查实战(从入门到精通)
  • Tesseract在Dify中的批量任务崩溃?99%的人都忽略的资源控制策略
  • 300套伺服电机步进调速电机SolidWorks三维3D模型图结构库合集
  • 错过再等十年:IPCC级气候归因模型R实现全过程首次公开
  • 【高并发场景下的稳定性保障】:Dify混合检索缓存自动清理方案设计
  • 仅限内部分享:量子计算平台镜像最小化构建流程(限时公开)
  • Dify与Spring AI深度融合方案(企业级AI系统搭建指南)
  • 为什么你的检索结果不精准?Dify相关性评估避坑指南
  • Qwen3-VL-8B:轻量级多模态Embedding新选择
  • Dify工作流版本回滚避坑指南:90%团队忽略的3个关键细节
  • 为什么你的Shiny应用越来越慢?一文看懂多模态动态加载的核心原理
  • 变量太多反降效?,手把手教你用R语言做农业数据精简与优化
  • 逆变器DC-AC拓扑全解析:从基础到专用,实战选型指南
  • 混合检索的Dify响应时间优化全攻略(响应速度提升必看)
  • 前缀和+差分
  • 【环境监测数据同化实战指南】:掌握R语言高效融合多源观测数据的核心技术
  • 为什么你的临床模型总出错?可能是R语言缺失值处理没做好(附诊断清单)
  • 9 个专科生答辩PPT模板,AI工具推荐降重查重率