MVC - 调用控制器方法
我的应用程序遵循 MVC 设计模式。我一直遇到的问题是需要从控制器类外部调用控制器类内部的方法(例如,视图类想要调用控制器方法,或者管理器类想要调用控制器方法)。 MVC 中允许以这种方式调用 Controller 方法吗?如果允许的话,正确的做法是什么?
根据我所关注的 MVC 版本(似乎有很多不同的版本),视图知道模型,控制器知道视图。这样做,我无法访问控制器。这是我找到的最好的网站,也是描述我正在关注的 MVC 版本的网站:http://leepoint.net/notes-java/GUI/struct/40mvc.html。主程序代码块真实地展示了它是如何工作的。
感谢您的任何答复。
My application is following the MVC design pattern. The problem I keep running into is needing to call methods inside a Controller class from outside that Controller class (ex. A View class wants to call a Controller method, or a Manager class wants to call a Controller method). Is calling Controller methods in this way allowed in MVC? If it's allowed, what's the proper way to do it?
According to the version of MVC that I am following (there seems to be so many different versions out there), the View knows of the Model, and the Controller knows of the View. Doing it this way, I can't access the controller. Here's the best site I've found and the one describing the version of MVC I'm following: http://leepoint.net/notes-java/GUI/structure/40mvc.html. The Main Program code block really shows how this works.
Thanks for any answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仔细看看您链接到的文章中的这一段:
查看
此视图不了解控制器,除了它提供了注册控制器侦听器的方法。其他组织也是可能的(例如,控制器的侦听器是可由视图引用的非私有变量,视图调用控制器来获取侦听器,视图调用控制器中的方法来处理操作,...)。
视图和控制器之间有观察者模式。 MVC 本身并不是单一模式,而是至少两种模式的组合。
管理视图/控制器通信的一种方法是使用事件。视图在某些用户操作上触发事件(不一定知道谁可能处理它们)。控制器处理这些事件并采取相应的操作。
Take a closer look at this paragraph from the article you linked to:
View
This View doesn't know about the Controller, except that it provides methods for registering a Controller's listeners. Other organizations are possible (eg, the Controller's listeners are non-private variables that can be referenced by the View, the View calls the Controller to get listeners, the View calls methods in the Controller to process actions, ...).
You have the observer pattern here between the View and the Controller. MVC is not a single pattern per se but at least two combined.
One way to get your head around managing the View/Controller communication is to use events. The View fires events on certain user actions (without knowing necessarily who might handle them.) The Controller processes these events and acts accordingly.