不确定是否使用 MVC 设计模式
我正在尝试在我的项目中使用 MVC 设计模式,但我有点不确定如何将我的程序解耦到类中。我的程序的第一部分是一个登录屏幕,要求用户输入用户名和密码以及一个检查详细信息的开始按钮,还有一个按钮可以转到可以添加新用户的页面。所以我正在考虑我的 MVC 设计:
LoginpanelView :只是带有文本框、标签、按钮等的 GUI
LoginpanelController: - 在这里实现开始按钮的动作监听器并引用方法 checkLogin - 在这里实现添加用户按钮的动作侦听器,并引用切换面板
登录模型的方法: - 定义检查登录 switchpanelModel 的实际方法
: - 定义一个创建卡片布局系统并切换面板的方法
我的理解是控制器只是对需要完成的事情进行非常一般的引用,即用户想要发生的事情,然后模型定义了如何执行的确切方法处理这个?有人介意验证/纠正我的理解吗?我已经阅读了很多有关此设计模式的内容,但不幸的是我仍然觉得我对它没有清晰的理解。任何帮助将不胜感激!
ps 抱歉!我忘了说我正在用 Java 编程
I'm trying to use the MVC design pattern for my project but I'm sort of unsure about how to decouple my program into the classes. The first part of my program is a Login screen which requires the user to enter a their username and password and a start button which checks the details, and there is a button to go to a page where you can add a new user. So I was thinking for my MVC design:
loginpanelView : Just the GUI with the text boxes, labels, buttons etc
loginpanelController:
- implement the actionlistener for the start button here and have a reference to the method checkLogin
- implement actionlistener for add user button here and have reference to a method which switches the panels
loginModel:
- defines the actual method which checks the login
switchpanelModel:
- defines a method which creates a cardlayout system and switches the panels
My understanding is that the controller just makes very general references to what needs to be done i.e. sort of what the user wants to happen, then the model defines the exact method of how to handle this? Would someone mind verifying/ correcting my understanding please? I've read a lot about this design pattern but unfortunately I still don't feel like I have a clear understanding of it. Any help would be much appreciated!
p.s. Sorry! I forgot to include that I'm programming in Java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有时从依赖关系的角度来思考 MVC 会有所帮助。
该模型代表了您的应用程序的功能。它不依赖任何东西。这就是您的应用程序的独特之处。
视图向用户显示信息。该信息来自模型。因此,视图对模型有依赖性。
控制器的功能是接受来自用户的输入,将该请求分派到适当的模型功能,并且(通常)接受返回值并将其提供给要呈现的视图。因此,控制器通常与其所服务的视图紧密耦合。它还对模型有依赖性。
在这种情况下,模型就是您的身份验证方案。 (实际上,这并不是一个模型,而是应用程序中的一个入口点,您的整体模型类似于“处理付款”、“生成报告”、“请求创建小部件”等)
您有两个视图,一个用于输入身份验证信息,另一个用于身份验证成功时的情况。第一个实际上没有任何模型信息,它只是收集输入(但是它的设计将基于身份验证模型所需的任何内容,因此这里仍然存在依赖性)。第二个无疑将显示您的应用程序提供的可用功能列表或显示登录页面等。
控制者有责任调解这些交互。因此,控制器接收第一个视图发送的信息,并将其分派给认证模型,认证成功或失败,然后控制器根据结果选择合适的视图进行渲染。
It sometimes helps to think of MVC in terms of dependencies.
The model repesents what your application does. It has no dependencies on anything. It is what makes your application unique.
The view displays information to the user. This information comes from the model. Therefore, the view has a dependency on the model.
The controller's function is to accept input from the user, dispatch that request to the appropriate model functionality, and (normally) accept the return value and supply it for a view to render. Thus, the controller is usually very tightly coupled to the view(s) that it serves. It also has dependencies on the model.
In this case, the model is your authentication scheme. (In reality, this is not all that much of a model but an entry point in your application, your overall model is something like "process payments", "generate report", "request to create widget", etc.)
You have two views, one to enter authentication information and a second for when an authentication succeeds. The first really does not have any model information, it is solely to collect input (however its design will be based on whatever the authentication model needs, so there is still a dependency here). The second will undoubtedly display a list of available features your application offers or display a landing page etc.
It is the controller's responsibility to mediate these interactions. Therefore, information sent from the first view is received by the controller, dispatched to the authentication model, authentication succeeds or fails, and then the controller chooses the appropriate view to render based on the result.
有了这样一个基本的“功能设计”,很难准确地帮助你,但你可能想更多地考虑一下你想要什么的大局。
方法
对象
页面视图/控制器当然可以分为几个子部分,但这可能不是一个糟糕的起点。
With such a basic "functional design" it's hard to help you exactly, but you might want to think more about the big picture about what you want.
method
object
The page view/controllers can be split up ofcourse in several sub-parts, but this might not be a bad place to start.
在我看来,LoginModel 和 SwitchPaneModel 根本不是模型。模型是你存储在某处的东西。所以你将拥有 UserModel 和 PaneModel。并且您的控制器将实现 switchPane 方法和登录方法。最好将此方法解耦到一些单独的类中,有很多方法可以执行此任务。但我强烈建议您找到现成的解决方案。不要发明自行车。
It seems to me that LoginModel and SwitchPaneModel are not models at all. Model is what you store somewhere. So you will have UserModel and PaneModel. And your controller will implement switchPane method and login method. It's good idea to decouple this method in some separate classes there are lots of methods to perform this task. But I strongly recommend you to find ready solution. Don't invent the bicycle.
此处是一个不错的起点。这是 MVC 的一个特例,称为被动视图。第一个重要的想法是视图和模型根本不相互通信。视图只告诉控制器有关事件的信息,控制器同时操作视图和模型。控制器甚至可以创建新的控制器和视图(例如复杂的模式对话框)。最后,模型不与任何人交流!
所以你有正确的想法:你的loginpanelController 监听来自loginpanelView 的按钮事件,然后调用模型中正确的方法来设置数据并验证它。
我认为您可能遇到问题的一个地方是 switchpanelModel。我认为你不需要这个。如果您的loginpanelView是其中包含卡片的视图,那么您的loginpanelController应该是切换卡片的视图。
我认为模型应该仅限于使用其自己的数据的方法,但不得在任何地方引用任何 GUI 元素。模型并不驱动程序;控制器这样做。
A good place to start is here. This is a special case of MVC called Passive View. The first important idea is that the view and the model do not communicate with each other at all. The view only tells the controller about events, and the controller manipulates both the view and the model. A controller can even create new controllers and views (such as for complex modal dialogs). And finally, the model does not communicate with anyone!
So you have the right idea: your loginpanelController listens for button events from the loginpanelView, and then calls the right methods in the model to set the data and validate it.
I think one place you may be having a problem with is switchpanelModel. I don't think you need this. If your loginpanelView is the view with the cards in it, then your loginpanelController should be the one switching the cards.
I think models should be restricted to methods working with its own data, but must have no reference to any GUI element anywhere. Models do not drive the program; controllers do.
与其考虑“定义”方法,也许更好地考虑是什么封装。
例如,松散,在 MVC 中,视图主要封装程序的用户界面(登录表单),模型封装域逻辑的某些部分(密码身份验证)控制器封装了连接视图和模型的逻辑(这取决于 MVC 架构的变化)。控制器通常在某种程度上耦合到视图(特别是如果您开始添加明显特定的 ActionListener 等),但是模型应该是相当可重用/可交换的(更改验证方式并不意味着您必须更改使用它的任何视图/控制器)
Rather then thinking in terms of 'defining' a method, perhaps it is better to think in terms of what is being encapsulated.
For example, loosely, in MVC a view encapsulates primarily the user interface of your program (a login form), a model encapsulates some part of your domain logic (password authentication) and a controller encapsulates the logic that connects a view with a model (it depends there are variation of MVC architecture). The controller is often to some extent coupled to a view (especially if you start adding overtly specific ActionListeners etc) however the model should be quite reusable/exchangable (changing how you validate should not mean you have to change any view/controller that uses it)