你什么时候会使用中介者设计模式

发布于 2024-07-10 15:25:34 字数 119 浏览 11 评论 0原文

正如标题所述,您何时会推荐使用中介设计模式以及您在哪里看到它使用不当?

As the title states when would you recommend the use of the mediator design pattern and where do you see it used incorrectly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

后知后觉 2024-07-17 15:25:34

对象通信的复杂性开始阻碍对象的可重用性时,请使用中介器。 这种类型的复杂性经常出现在视图实例中,尽​​管它实际上可能存在于任何地方。

滥用中介者可能会导致中介者同事类的接口瘫痪。

谈论滥用模式似乎有点有趣。 如果您的实现遵循该模式,那么您就已经使用了该模式。 否则,你还没有。 换句话说,如果您的调解员正在做其他事情,那么它可能就不是调解员。 模式是由它们所做的事情以及它们实际上是什么来定义的。 事物的名称只是标签。

要问自己的真正问题是模式的实现是否满足模式对设计的承诺。 中介者模式旨在在复杂的对象间通信变得难以管理时对其进行封装。 如果它没有做到这一点,或者做得不好,你可以说调解者被滥用了。 在某些时候,它会成为一种价值判断。

Use a mediator when the complexity of object communication begins to hinder object reusability. This type of complexity often appears in view instances, though it could really be anywhere.

Misuse of a mediator can result in crippling the interfaces of the mediator's colleague classes.

It seems a little funny to talk about misusing a pattern. If your implementation follows the pattern, then you've used the pattern. Otherwise, you haven't. In other words, if your mediator is doing something else, then it probably isn't a mediator. Patterns are defined by what they do, what they in fact are. The names of things are simply labels.

The real question to ask yourself is whether your implementation of a pattern fulfills the pattern's promises for your design. The mediator pattern aims to encapsulate complex inter-object communication when it is becoming unmanageable. If it hasn't accomplished this, or hasn't done it very well, you could say that a mediator is being misused. At some point, it becomes a value judgement.

Saygoodbye 2024-07-17 15:25:34

我用它来处理 Swing 应用程序。

当我构建 GUI 时,我不喜欢每个控件彼此了解,因为这需要子类化。

相反,我有一个 Main 对象,其中包含侦听器和小部件,并让它在不同的控件、按钮、文本字段等之间进行调解。

I have used it to deal with swing apps.

When I'm building a GUI I don't like each control knowing each other because that would require subclassing.

Instead I have a Main object which contains the listener and the widgets and let it mediate between the different controls, buttons, textfields etc.

最近可好 2024-07-17 15:25:34

中介者基本上也相当于事件泵。 GUI 和游戏中非常常见的模式。

另外,我之前也使用过中介器在非常不同的系统和遗留框架之间进行通信。

The mediator is also basically what an event-pump is. A very common pattern in GUI's and Games.

Also I've used mediator before to communicate among very dispar systems, and legacy frameworks.

对你再特殊 2024-07-17 15:25:34

当您处理多个对象并且它们需要相互通信但您不希望它们直接相互了解时

When you are dealing with multiple objects and they need to communicate with each other but you want them not to know about each other directly

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文