news 2026/4/3 0:36:46

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/3/30 16:40:15

为什么你的智能Agent频繁被OOMKilled?资源限制配置误区大曝光

第一章:为什么你的智能Agent频繁被OOMKilled?当部署在 Kubernetes 或其他容器编排平台上的智能 Agent 频繁遭遇 OOMKilled(Out of Memory Killed)时,通常意味着容器内存使用超出了预设限制。这一现象不仅影响服务稳定性…

作者头像 李华
网站建设 2026/3/28 20:32:49

还在裸奔发布Agent?立即实施Docker签名以抵御供应链攻击

第一章:Agent发布安全的紧迫性与Docker签名的价值在现代持续交付体系中,Agent作为自动化任务执行的核心组件,其发布的安全性直接影响整个CI/CD链路的可信度。一旦恶意或被篡改的Agent镜像被部署,攻击者可能获得对构建环境、凭证系…

作者头像 李华
网站建设 2026/3/16 2:53:00

新手首次开标注意事项

给首次参与投标会议的新手一点建议~要带好被授权人的身份证检查好标书密封袋上的签字盖章,带上密封袋密封条胶棒,公章之类的东西带到现场,如果密封有问题,在投标截止时间之前是有机会补救的。要早点出发,尽量不要卡点到…

作者头像 李华
网站建设 2026/3/18 0:12:10

揭秘智能Agent日志难题:如何在Docker中实现精准日志收集与监控

第一章:智能Agent日志收集的挑战与演进随着分布式系统和微服务架构的广泛应用,智能Agent在日志收集中的角色愈发关键。传统的集中式日志采集方式已难以应对高并发、多节点、动态伸缩的现代应用环境,智能Agent需具备自适应、低延迟和高可靠的数…

作者头像 李华