Hello I have read an example that I found on my school database about MVC in Java. there is this example where the buttons go into the Controller part. but isn't it correct that the buttons need to be in the VIEW because everything that the user can see must be in the view or am I understanding it wrong and is it different in Java?
您好我已经阅读了我在学校数据库中找到的关于Java中的MVC的示例。有一个例子,按钮进入控制器部分。但是按钮需要在VIEW中是不正确的,因为用户可以看到的所有内容都必须在视图中,或者我理解它是错误的并且在Java中是不同的?
3 个解决方案
#1
2
This image is a generalization of MVC from the Model-View-Controller page on Wikipedia:
此图像是来自*上的模型 - 视图 - 控制器页面的MVC的概括:
In general, if you're dealing with drawing/rendering UI elements, that belongs in the view. Actions/events that let the user interact with the button would belong in the controller (and may have functionality delegated to services as necessary).
通常,如果您正在处理绘图/渲染UI元素,那么它们属于视图。允许用户与按钮交互的动作/事件将属于控制器(并且可以根据需要将功能委托给服务)。
You don't want to put any code that draws/renders buttons in the controller - that tightly couples the controller to a specific view, which goes against the principle of using this design pattern.
您不希望在控制器中放置任何绘制/渲染按钮的代码 - 将控制器紧密耦合到特定视图,这违反了使用此设计模式的原则。
#2
2
You are correct in saying Buttons belong to the View. However, there may be specific cases where you have to define buttons in the controller. For example, in case of dynamic UI, buttons may need to be instantiated in the controller based on a condition.
你说Buttons属于View是正确的。但是,可能存在必须在控制器中定义按钮的特定情况。例如,在动态UI的情况下,可能需要基于条件在控制器中实例化按钮。
One main reason for the View/Controller separation, is to make the view "dumb", and be a UI representation that doesn't require testing. UI components are often heavy-weight, and must be abstracted out for testing. This is done by replacing it with light-weight mocks that mirror the original components' interface, and presenting those to the tests for the controller.
视图/控制器分离的一个主要原因是使视图“哑”,并且是不需要测试的UI表示。 UI组件通常很重,必须抽象出来进行测试。这是通过用轻量级模拟替换它来完成的,这些模拟镜像原始组件的界面,并将它们呈现给控制器的测试。
If conditions or business logic are involved, the code should be tested for the different scenarios, and hence belongs in the controller.
如果涉及条件或业务逻辑,则应针对不同场景测试代码,因此属于控制器。
#3
1
My advice to you - have a look at MVC implementation for Java by PureMVC Project. You'll find that it's an amazing thing! The infrastructure is implemented according to MVC architecture described here.
我给你的建议 - 看看PureMVC Project的Java MVC实现。你会发现这是一件了不起的事!基础架构根据此处描述的MVC架构实现。
#1
2
This image is a generalization of MVC from the Model-View-Controller page on Wikipedia:
此图像是来自*上的模型 - 视图 - 控制器页面的MVC的概括:
In general, if you're dealing with drawing/rendering UI elements, that belongs in the view. Actions/events that let the user interact with the button would belong in the controller (and may have functionality delegated to services as necessary).
通常,如果您正在处理绘图/渲染UI元素,那么它们属于视图。允许用户与按钮交互的动作/事件将属于控制器(并且可以根据需要将功能委托给服务)。
You don't want to put any code that draws/renders buttons in the controller - that tightly couples the controller to a specific view, which goes against the principle of using this design pattern.
您不希望在控制器中放置任何绘制/渲染按钮的代码 - 将控制器紧密耦合到特定视图,这违反了使用此设计模式的原则。
#2
2
You are correct in saying Buttons belong to the View. However, there may be specific cases where you have to define buttons in the controller. For example, in case of dynamic UI, buttons may need to be instantiated in the controller based on a condition.
你说Buttons属于View是正确的。但是,可能存在必须在控制器中定义按钮的特定情况。例如,在动态UI的情况下,可能需要基于条件在控制器中实例化按钮。
One main reason for the View/Controller separation, is to make the view "dumb", and be a UI representation that doesn't require testing. UI components are often heavy-weight, and must be abstracted out for testing. This is done by replacing it with light-weight mocks that mirror the original components' interface, and presenting those to the tests for the controller.
视图/控制器分离的一个主要原因是使视图“哑”,并且是不需要测试的UI表示。 UI组件通常很重,必须抽象出来进行测试。这是通过用轻量级模拟替换它来完成的,这些模拟镜像原始组件的界面,并将它们呈现给控制器的测试。
If conditions or business logic are involved, the code should be tested for the different scenarios, and hence belongs in the controller.
如果涉及条件或业务逻辑,则应针对不同场景测试代码,因此属于控制器。
#3
1
My advice to you - have a look at MVC implementation for Java by PureMVC Project. You'll find that it's an amazing thing! The infrastructure is implemented according to MVC architecture described here.
我给你的建议 - 看看PureMVC Project的Java MVC实现。你会发现这是一件了不起的事!基础架构根据此处描述的MVC架构实现。