当前位置: 首页 > 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.jsqmd.com/news/100690/

相关文章:

  • 为什么你的Dify回滚总是失败?深入剖析版本恢复的5大核心障碍
  • 文献学期末论文写作方法与实践研究:基于文献检索与分析的视角
  • 工业用自动反冲洗过滤器推荐厂家——降低运营成本的关键 - 速递信息
  • 基于 MATLAB 的光照不均匀图像增强
  • LobeChat能否用于生成问卷调查?市场调研工具包
  • 12月16日总结 - 作业----
  • 从“制造”到“智造”:Linux数控系统的核心优势
  • 强制退出MySQL CLI
  • LobeChat能否对接Nuclino?现代团队协作文档AI增强
  • Dify 1.7.0音频切片怎么配?揭秘专业级配置流程与避坑要点
  • mysql命令行手动导入csv数据到指定表
  • 高职510221信创系统技术应用专业产教协同育人解决方案
  • DDD领域驱动设计
  • 人工智能内容整理提纲
  • 路易波拿巴的雾月十八日 (马克思) _没记录
  • 2025互联网AI岗位爆发:开发/产品/运维核心技能冲突与CAIE认证指南
  • elysia
  • 如何在7天内掌握R语言代谢组分析?资深生信专家的进阶路线图曝光
  • 17、多线程同步函数与安全接口详解
  • 为什么顶尖团队都在用Dify做可视化流程编排?真相曝光
  • 逆向山姆小程序搜索接口
  • 节能又达标!基于Linux的污水自动控制方案
  • Clion+STM32配置环境-DESKTOP-65G5ROL
  • 14、多线程编程中的信号处理与 I/O 操作
  • 视频字幕检索的Dify缓存周期全解密(企业级缓存设计实战)
  • 2020-12-17-xtx的日常开发日记-DESKTOP-65G5ROL
  • 2025 - 2026年宁夏银川geo ai搜索优化公司客观深度评测排行最新发布
  • Agent工具如何赋能Dify?3个真实案例揭示扩展开发的巨大价值
  • 实时消息推送(Websocket/SSE)
  • (Dify Tesseract 更新机制终极指南):构建高可用AI应用的基石