MVC 与 Netbeans 表单生成器
我在工作中开发了很多小应用程序。因此,我已经从在大多数地方使用 gridbag 转向使用 netbeans 表单生成器。我有时会使用网格布局、框布局和流布局。
我发现,我通常只需双击要添加功能的按钮,然后从那里添加对数据库适配器接口的调用,而不是开发单独的控制器对象。
这是错误的吗?
情况:
我有一个 Cat 类和一个 Cat herder 类,它们都存在于数据库中。我有一个视图,可以从数据库中获取最新的猫,并告诉我它属于哪个猫牧民。
该按钮应该与中间控制器通信还是触发 ActionPerformed 控制器的 ActionListener?
I develop a lot of small apps where I work. As a result, I've switched from using gridbag in most places to using the netbeans form builder. I sometimes use grid layout, box layout and flow layout.
I've found that instead of developing a seperate controller object that I often simply double click on the button that I want to add functionality to and then I add the call to the database adapter interface from there.
Is this wrong?
Situation:
I have a Cat class and a Cat herder class and they both exist in the database. I have a view that gets the most recent cat from the database and tells me which cat herder it belongs to.
Should this button talk to an intermediary controller or is the ActionListener that fire the ActionPerformed the controller?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,取决于您的应用程序正在执行多少业务逻辑。
我已经按照您描述的方式编写了一个应用程序。我在 Netbeans 中布置了所有面板,并编写了 SQL 语句以将数据直接提取到我的框架/面板类中。我双击创建的按钮以及需要发生的任何操作,还将代码写入框架/面板类中生成的方法中。
然而:这个应用程序只是从数据库中读取/写入内容,在屏幕上显示数据并允许对其进行编辑。代码中完全没有逻辑,甚至几乎没有任何验证。
如果需要发生任何类型的逻辑或流程(大多数应用程序都有),那么我将创建对象来执行这些操作,这些操作本身例如执行 SQL 语句或其他操作。首先,这使得代码更容易编写(逻辑和表示分离),其次更容易重用(例如,不同表单上的两个按钮执行相同的操作)。然而,这是一个更复杂的应用程序设计,只有在合理的情况下才应该引入复杂性。
I would say that depends on how much business logic your application is doing.
I have written an app exactly the way you describe. I laid out all the panels in Netbeans, and wrote SQL statements to fetch the data directly into my frame/panel classes. I double-clicked the buttons I created and any actions that needed to occur, I also wrote the code into the generated methods in my frame/panel classes.
However: This application was simply to read/write stuff from a database, display the data on the screen and allow it to be edited. There was absolutely no logic in the code, there was hardly even any validation.
If there is any kind of logic or processes which need to happen (which most apps have) then I would create objects to perform those actions, which themselves e.g. execute SQL statements or whatever. Firstly that makes the code easier to write (separation of logic and presentation) and secondly easier to re-use (e.g. two buttons on different forms which perform the same action). However, it is a more complex application design, and complexity should only be introduced if it is justified.