简体   繁体   English

调解器对象如何工作? 它背后的想法是什么?

[英]How does a mediator object work? What is the idea behind it?

I'm interested in the mediator object because it sounds useful, but deciphering code examples in order to learn how to interact with and build that object escapes me. 我对mediator对象感兴趣,因为它听起来很有用,但是为了学习如何与对象进行交互并构建该对象,我解读代码示例。 I love code examples if they come with some explanations, however short. 我喜欢代码示例,如果他们有一些解释,无论多么简短。 Would someone be able to just explain what I'm building when I build a mediator object? 当我构建一个中介对象时,有人能够解释我正在构建的内容吗?

Would a mediator object be a way to handle action events sent between classes? 调解器对象是否可以处理类之间发送的动作事件? or does the mediator object simply serve better for consolidating like-code into one handy place? 或者,调解器对象是否更适合将类似代码整合到一个方便的地方?

I don't know if it's practical for convenience or if it's practical because there is no other way to do what it does. 我不知道它是否实用方便或是否实用,因为没有其他方法可以做它的功能。 Any details, however "dumbed down", would be most excellent. 任何细节,无论如何“愚蠢”,都将是最优秀的。 Thanks in advance. 提前致谢。

The mediator object is intended to do nothing itself. 调解员对象的目的是什么都不做本身。 You should not move any logic that you already have into it, except maybe for some multiplexing/demultiplexing (when one object sends the same message to multiple other objects). 您不应该移动已经存在的任何逻辑,除非是某些多路复用/解复用(当一个对象将相同的消息发送到多个其他对象时)。 The mediator is just an external interface (if it simultaneously serves as a facade), and definitely a message passing channel between pre-existing objects. 介体只是一个外部接口(如果它同时用作外观),并且肯定是预先存在的对象之间的消息传递通道。

Likewise, a mediator should not be created until you are already perceiving the need for such a message passing channel. 同样,在您已经认识到需要这样的消息传递通道之前,不应该创建调解器。 How does such a need look like? 这样的需求怎么样? You already have a set of objects that start calling each other in increasingly complex ways. 您已经有一组对象以越来越复杂的方式开始相互调用。 Those objects are storing references to each other; 这些对象存储彼此的引用; the number of such references is already getting bigger than the number of such objects themselves. 这些引用的数量已经大于这些对象本身的数量。

So instead of each object talking to each object (with a quadratic number of references and complicated graph of interactions) you introduce a star topology to interactions; 因此,不是每个对象与每个对象交谈(具有二次数量的引用和复杂的交互图),而是为交互引入星形拓扑; everybody directly talks just to the mediator. 每个人都直接与调解员交谈。 It is then easier to instantiate, monitor, debug, extend, polymorphize... 然后更容易实例化,监视,调试,扩展,多态化......

Do not start introducing mediators too early or the overall complexity will grow instead of dropping. 不要太早开始介绍调解员,否则总体复杂性会增加而不是下降。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM