调解者模式或责任过多

发布于 2024-07-19 15:02:42 字数 231 浏览 7 评论 0原文

在我的应用程序中,我有几个必须相互了解的组件,例如菜单栏和工具栏,它们都需要了解表以添加或删除作业,并找出选择了哪个作业。

因此,我创建了一个名为 guiMediator 的对象,我将其传递给每个对象,并且它们向该对象注册自己,以便可以使用该对象相互联系。 它还负责在添加新作业或后台工作人员完成其工作时触发事件。

由于它对系统了解很多,这种用法是否在一个地方承担了过多的责任,或者这是该模式的正确用法?

In my application, I have several components that have to know about each other, such as a menu bar and a tool bar that both need to know about the table to add or remove jobs and also to find out which job is selected.

So, I created a object called guiMediator that I pass to every object and they register themselves with it so that they can reach each other using that object. It is also responsible to fire events when new jobs are added or background workers finish their job.

Since it knows a lot about the system, is this type of usage too much responsibility in one place, or is this the correct usage of the pattern?

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

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

发布评论

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

评论(3

岁吢 2024-07-26 15:02:43

听起来比另一种更好……但是嘿,我娶了最不丑的妹妹;-)

Sounds better than the alternative... But hey, I married the least ugly sister ;-)

难忘№最初的完美 2024-07-26 15:02:42

通常我会使用命令模式来实现类似的功能:

  1. 用户单击菜单栏上的“Foo”按钮,该按钮执行 FooButtonClickedCommand。
  2. FooButtonClickedCommand 执行它应该执行的任何操作,然后适当地修改视图(菜单栏、表格等)。

因此,您的命令了解所有视图组件,但视图组件唯一需要知道的是当用户完成给定操作时要执行哪个命令。

Normally I would use the command pattern for something like that:

  1. User clicks the "Foo" button on menu bar, which executes the FooButtonClickedCommand.
  2. FooButtonClickedCommand does whatever it's supposed to do, then modifies the view (menu bar, tables, etc) appropriately.

So your commands know about all your view components, but the only thing your view components need to know is which command to execute when a given action is done by the user.

后eg是否自 2024-07-26 15:02:42

我会使用被动视图,您可以在此处阅读有关内容。

  • 您可以将每个表单放在一个界面后面。
  • 每个表单都会向一个或多个 UI 对象注册自己。UI
  • 对象应该自然组织,如设置、条目、显示等。文字处理器对于每个文档可能只有一个 UI 对象。 虽然机器控制器的每个屏幕可能有多个对象。
  • 该界面被实现为薄壳,将事件传递给 UI 对象并公开表示控件,将绘图表面传递给 UI 对象。
  • 然后 UIObject 获取输入并确定要执行哪个命令对象。
  • 命令对象将更新模型,然后告诉一个或多个 UI 对象更新视图。
  • UIObject 更新视图。

请注意,除了 UI 界面之外,没有任何地方知道按钮、复选框等。 您可以使用接口来抽象实际的实现。

这会给你带来几个好处。 首先,它将记录您的代码如何与 UI 交互,为您提供一个实现模拟对象以用于自动化测试的位置,最后在更改 UI 方面提供更多的自由度。

例如,用可点击面板代替命令按钮。 然后,表单将开始传递来自面板的单击事件而不是按钮。 表单可能仍然不知道每个小部件应该执行的实际命令。 UI 对象负责处理这个问题。

I would use a Passive View which you can read about here.

  • You would put each form behind an interface
  • Each form would register itself with one or more UI Objects
  • The UI Object should be naturally organized, like Setup, Entry, Display, etc. A word processor may only have one UI Object for each document. While a machine controller may have multiple object for each screen.
  • The interface are implemented as thin shells passing on events to the UI Objects and exposing presentation controls, drawing surfaces to the UI Object.
  • The UIObject then take the input and figures out which command object to execute
  • The Command Object will update the model, then tell one or more UI Objects to update the view.
  • The UIObjects update the views.

Note that nowhere does anything beyond the UI Interface knows about buttons, check boxes, and the like. You use the Interface to abstract the actual implementation away.

This will give you several advantage. First it will document how your code interacts with the UI, give you a place to implement mock objects to use for automated testing, and finally allow much more latitude in changing the UI.

For example substituting clickable panels instead of command buttons. The form will then just start passing the click events from the panels instead of the button. The form can remain ignorant of the actual command each widget is supposed to do. The UI Object takes care of that.

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