使用 MVC,与自主外设的交互属于模型还是控制器?
使用具有观察者模式的 MVC,如果用户操作需要轮询设备(例如相机)以获取数据,轮询应该在控制器中完成并将结果传递给模型,还是应该将请求发送到模型和模型模型本身执行轮询。
这个问题是我试图将我正在阅读的所有吹捧“瘦控制器”格言的内容与我的直觉相协调的结果,即模型应该只对数据起作用而不是获取数据。
(注意:这个问题可能是主观的。我不完全确定这个问题是否有一个唯一正确的答案。如果没有,请随时重新标记,因为我很想听听对这个话题的看法。)
Using MVC with an observer pattern, if a user action requires polling a device (such as a camera) for data, should the polling be done in the Controller and the result passed off the Model or should a request be sent to the Model and the Model itself performs the polling.
This question is my attempt to reconcile everything I am reading that touts the "skinny Controllers" maxim with my gut intuition that the Model should only be acting on data not acquiring it.
(Note: This question might be subjective. I'm not entirely sure that there is a one-true-answer to this question. If not, feel free to retag as I will be very interested to hear opinions on the subject.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它属于控制器。 模型包含信息和业务规则,控制器本质上是除用户、信息或业务规则之外的所有内容的接口,视图处理用户交互。
有人可能会认为视图也可以控制这一点 - 相机模型和驱动程序可能由用户确定,因此属于该区域。
但我不希望该模型具有外围接口。
-亚当
It belongs in the controller. The model contains the information and business rules, the controller is essentially the interface to everything that is not the user, information, or a business rule, and the view deals with user interaction.
One might argue the view could control this as well - the camera model and drivers might be determined by the user, and thus fall under that area.
But I would not expect the model to have the peripheral interfaces.
-Adam
控制器应执行轮询。 模型是我脑海中状态的快照,因此相机状态应该从控制器传递到模型。
The Controller should perform the polling. The Model is a snapshot of state in my mind, so the camera states should be passed from the controller to the Model.
您可以在控制器下方和模型上方添加一个薄服务层,这使您能够将对外围代码的所有访问放在一个地方
you could add a thin service layer below controller and above model, this enables you to put all your access to periphery code in one place