简体   繁体   English

调解员模式是否适用于这种情况?

[英]Does Mediator Pattern work in this situation?

So for my current project, there are basically three main Java classes: 所以对于我目前的项目,基本上有三个主要的Java类:

  1. GUI GUI
  2. Instant Messaging 即时通讯
  3. Computation 计算

Essentially, there needs to be full communication, so we've decided to use the mediator approach rather than than allow the GUI to run the entire project. 基本上,需要进行完全通信,因此我们决定使用中介方法,而不是允许GUI运行整个项目。

Basically, the mediator is going to encapsulate the communication. 基本上,调解员将封装通信。 The problem we've run into is how to allow the GUI components to update without building a ton of methods for the mediator to call anytime something completes. 我们遇到的问题是如何允许GUI组件更新,而无需构建大量方法,以便中介在任何时候完成调用。

Ex. 防爆。 Say the GUI wants to log in the user, it goes through the mediator to create a thread and log in, but then the mediator has to relay the success/failure back to GUI as well as update a status message. 假设GUI想要登录用户,它通过介体创建一个线程并登录,但是介体必须将成功/失败转发回GUI并更新状态消息。

The other issue is things that need to update the GUI but do not need the moderator. 另一个问题是需要更新GUI但不需要主持人的事情。 Is it practical to just allow the GUI to create an instance of that class and run it or should everything go through the mediator? 允许GUI创建该类的实例并运行它或者是否所有内容都通过调解器是否切实可行?

Our original design just had the GUI managing everything, but it really killed reusability. 我们的原始设计只是让GUI管理所有内容,但它确实杀死了可重用性。 Is there a better design method to use in this case? 在这种情况下是否有更好的设计方法?

If you're finding Observer to bring too much overhead, Mediator may be the best way to go. 如果您发现Observer带来了太多的开销,Mediator可能是最好的方法。 I definitely think that you shouldn't have the GUI run the show. 我绝对认为你不应该让GUI运行这个节目。 If you're going to use the Mediator pattern, the mediator itself should be in charge. 如果您打算使用Mediator模式,调解员本身应该负责。 Something you might consider is a variant of the Command pattern. 您可能会考虑的是Command模式的变体。 If you were using Ruby, I might recommend passing function callbacks around as a means of avoiding having the mediator contact the GUI for every little thing. 如果您使用的是Ruby,我可能会建议传递函数回调,以避免让调解器为每个小东西联系GUI。 But since it's Java, some form of encapsulating an action in Command pattern style may help. 但由于它是Java,因此在Command模式样式中封装动作的某种形式可能有所帮助。

If you don't want the callback/notification to be triggerd by the mediator, you can inject the callback into the login function and have login call it when it finishes. 如果您不希望调解器触发回调/通知,您可以将回调注入登录功能,并在完成后登录调用。

I don't know how you would go about injecting the callback in Java, though. 不过,我不知道如何在Java中注入回调。 In a language where functions are first class citizens, you could just pass the function, but you're in Java so I guess you will have to use the command pattern as kmorris suggested. 在函数是一等公民的语言中,你可以只传递函数,但是你使用Java,所以我猜你必须使用命令模式作为kmorris建议。

You might also try having the GUI give the mediator a callback object that handles retrieving return values or setting whatever values you need (a version of the Command pattern). 您也可以尝试让GUI为调解器提供一个回调对象,该对象处理检索返回值或设置所需的任何值(Command模式的一个版本)。 There would then be one per call from the GUI to the mediator. 然后,从GUI到调解器的每次调用都会有一个。

Another thought is to group the methods the mediator calls into semantically related chunks. 另一个想法是将介体调用的方法分组为语义相关的块。 In particular if the mediator has sections where it tends to call several GUI methods in a row: 特别是如果介体具有可以连续调用多个GUI方法的部分:

   gui.a()
   gui.b()
   gui.c()

you can create a single method that handles the result of calling all three. 你可以创建一个方法来处理调用所有三个的结果。 The advantage of semantically grouped methods (ie setFileInformation over setFileMenu , setTab , etc.) is also then if you need to change the GUI, the contents of the methods might change, but the call the mediator makes may not. 语义分组方法(即setFileInformation over setFileMenusetTab等)的优点是,如果您需要更改GUI,方法的内容可能会更改,但调解器所做的调用可能不会。

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

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