news 2026/8/20 16:57:47

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/8/20 21:14:33

2025互联网AI岗位爆发:开发/产品/运维核心技能冲突与CAIE认证指南

一、AI 岗位爆发背后的供需撕裂:2025 职场人必看现状​ 说实话,2025 年互联网 AI 岗位真的迎来了肉眼可见的爆发,行业第三方机构《2025 AI 人才趋势报告》显示,1-10 月 AI 岗位量同比攀升 543%,单月最高增幅超 11 倍&a…

作者头像 李华
网站建设 2026/8/20 17:54:32

**手机专业写小说软件2025推荐,提升创作效率与质量**

手机专业写小说软件2025推荐,提升创作效率与质量随着移动互联网技术的飞速发展,越来越多的作家和内容创作者开始使用手机进行创作。一款好的手机写小说软件不仅能提升创作效率,还能显著提高作品的质量。本文将为您推荐2025年最值得使用的手机…

作者头像 李华
网站建设 2026/8/20 11:21:45

你真的懂Agent参数安全吗?:从零构建Dify调用校验防御体系

第一章:你真的懂Agent参数安全吗?在分布式系统与自动化运维日益普及的今天,Agent作为连接控制端与目标主机的核心组件,其参数安全性常被低估。一个看似无害的启动参数,可能成为攻击者提权、注入恶意负载的入口。参数注…

作者头像 李华
网站建设 2026/8/19 15:03:54

各省排放清单调整

;cycle_asc_multiple_province_conbine.ncl ; 功能:用中国省级 shp 选出省格点,将省内的数据 *比例系数,其它所有区域保持与原始 ASC 完全相同;================= 1. 加载库 ================= load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl" load…

作者头像 李华
网站建设 2026/8/19 19:45:59

AI智能体:连接大语言模型与现实任务的核心架构解析

随着人工智能技术不断持续地演进,有一种架构范式被称作“智能体”,它正变成连接大语言模型与现实世界复杂任务的关键桥梁。被简称为智能体的AI智能体,不是一个全新概念,不过其内涵在通用人工智能技术取得突破之后,得到…

作者头像 李华
网站建设 2026/8/20 7:18:45

Agent与Dify集成深度解析(文档自动生成技术内幕)

第一章:Agent与Dify集成概述在现代AI应用开发中,智能代理(Agent)与低代码平台的融合正成为提升开发效率的重要路径。Dify作为一款支持可视化编排与Agent集成的开放平台,允许开发者将自定义逻辑的Agent无缝嵌入到工作流…

作者头像 李华