清理建筑 - 应控制器聊到presenter? [英] Clean Architecture - should the controller talk to the presenter?

查看:142
本文介绍了清理建筑 - 应控制器聊到presenter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

清洁建筑通过罗伯特·马丁,让我们说我有这样的简化版本(不显示其他的东西,如型号,网关,边界):

In the Clean Architecture by Robert Martin, let's say I have this simplified version (not showing other stuff like Model, Gateway, Boundaries):

现在,让我们说我有一个查看,2个按钮,黑暗,即点击要改变视图的背景颜色并显示在屏幕上的一些文字时(文本的颜色应始终为蓝色)。
所以,虽然我的是这样的:

Now let's say I have a View with 2 buttons, Dark and Light, that when clicked should change the background color of the View and show some text on the screen (the text's color should always be blue). So I though of something like this:

这里假设我有在控制器的两个按钮(也许是因为它是一个形式或其他)使用一个方法,在按钮变量 buttonClicked(按钮)将载有关于是否黑暗按钮的信息为pressed。

Assuming here that I have to use one method in the controller for both buttons (maybe because it was a form or whatever), the button variable in buttonClicked(button) will contain the information on whether the Dark or Light button was pressed.

现在,在交互器在这种情况下,仅仅是为检索的文本要显示负责,但它并不需要了解的背景颜色任何东西。

Now, the Interactor in this case is only responsible for retrieving the text to be displayed, but it doesn't need to know anything about the background color.

所以,应该控制器告知这是选择的颜色(如按钮)presenter或者我应该传达这一信息交互器只是以便它可以通过它的 presenter ?请记住,在交互器甚至不会使用这些信息。

So, should the controller tell the presenter which color (i.e. button) was chosen or should I relay this information to the Interactor just so that it can pass it to the Presenter? Keeping in mind that the Interactor won't even use this information.

推荐答案

如果交互器有没有用的信息,这些信息只能在视图格式的目的。因此,你需要怎么看待你的意见(无法查看模型,不是你的presenters)来实现。您所选择的视图格式必须能够响应用户的输入,这将反过来影响格式化。

If the interactor has no use for the information, the information can only be for formatting purposes in the view. Therefore, you need to look at how your views (not view models, not your presenters) are implemented. Your chosen view format must be able to respond to user inputs, which will in turn affect the formatting.

例如,如果视图是HTML,在浏览器中查看,你只需使用JavaScript来监听按钮点击改变颜色。在presenter只需要一个响应,从您的使用情况转换成一个视图模型(在上面您的示例文本)。然后视图模型所使用的视图上呈现其选择的格式的数据,基

For example, if the view is HTML, viewed on a browser, you would just use javascript to listen for a button click and change the color. The presenter is only needed to convert a response from your use case into a view model (the text in your example above). The view model is then used by the view to render data, base on a format of its choosing.

你可能也想看看是为什么你提供不同的颜色。很可能,你想表明你的系统的一些州,通过颜色选择用户。在这种情况下,该按钮preSS可能是感兴趣的用例如也许一个按钮preSS系指运在电子商务网站中的项目被标记。

What you may want to also look at is WHY you are providing the different colors. It is likely that you are trying to indicate some state of your system to the user through color choice. In that case, the button press is likely to be of interest to the use case e.g. maybe a button press means an item on an ecommerce site is marked as shipped.

我这种情况下,你将不会被向前和向后传球颜色,但一些业务例如状态运= TRUE。

I this case, you wouldn't be passing colors backwards and forwards, but some business state e.g. shipped = true.

您的使用情况将随后返回到presenter你的文字和发运= TRUE。然后,presenter会集运=真在你的视图模型。那么该视图会改变取决于如果发货是在视图模型真或假的颜色。

Your use case would then return to the presenter your text and shipped = true. The presenter would then set shipped = true on your view model. The view would then change the color depending if shipped was true or false on the view model.

这篇关于清理建筑 - 应控制器聊到presenter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆