模型,视图,控制器混乱 [英] Model, View, Controller confusion

查看:62
本文介绍了模型,视图,控制器混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
了解模型-视图-控制器

Possible Duplicate:
Understanding Model-View-Controller

我有一个关于如何划分代码的一般编程问题,通常是(我正试图摆脱这一点)我只是将它们全部编写在viewController中,在视图控制器中编写了很多代码.但是现在我已经查看了有关MVC的一些信息,但是我有一些疑问.

I have a general programming question about how to divide up my code, usually (and I'm trying to get away from this) I just write it all in the viewController, lots and lots of code in the view controller. But now i've reviewed some information on the MVC but I have some questions.

我的问题

主要是,如果我使用一个视图控制器(包含一个模型和一个视图),并且在模型中运行一个方法,该方法会为该视图计算一个数字显示,因此只需一个简单的int即可将视图取并显示在视图上屏幕.为了让我的模型告诉我的观点,我应该直接从模型---->视图出发.还是应该将数据返回给控制器,然后将其发送到视图?

Mainly, if I a view controller (holding a model and a view), and in the model I run a method figures out a number display for the view, so just a simple int which the view would take and display on the screen. In order for my model to tell my view to do this should I go straight from model ----> view. Or should I return the data back to the controller and then send it to the view?

最后,如果我需要将数据发送回控制器,该怎么办,因为我认为模型不应该了解控制器,反之亦然.

Lastly, if I need to send the data back to the controller, how might I do so, because I thought that the model should'nt know about the controller, only vice-versa.

很抱歉,这个冗长的问题.感谢你的帮助.

Sorry for the lengthy question. Thanks for all your help.

推荐答案

网络上有很多与此有关的信息.来自维基百科:

There's a lot of info about this in the net. From Wikipedia:

尽管MVC具有不同的风格,但控制流程通常与 如下:

Though MVC comes in different flavors, control flow is generally as follows:

  1. 用户以某种方式与用户界面进行交互(对于 例如,通过按下鼠标按钮).

  1. The user interacts with the user interface in some way (for example, by pressing a mouse button).

控制器可能会通知模型用户操作 导致模型状态发生变化. (例如, 控制器会更新用户的购物车.)

The controller notifies the model of the user action, possibly resulting in a change in the model's state. (For example, the controller updates the user's shopping cart.)

视图查询模型以生成适当的用户 界面(例如,视图列出了购物车的内容). 视图从模型中获取自己的数据.在某些实现中, 控制器可以向视图发出通用指令以进行渲染 本身.在其他情况下,该视图会由模型自动通知 状态更改(观察者),需要进行屏幕更新.

A view queries the model in order to generate an appropriate user interface (for example the view lists the shopping cart's contents). The view gets its own data from the model. In some implementations, the controller may issue a general instruction to the view to render itself. In others, the view is automatically notified by the model of changes in state (Observer) that require a screen update.

用户界面等待进一步的用户交互, 重新启动控制流循环.

The user interface waits for further user interactions, which restarts the control flow cycle.

MVC的目标是通过分离模型和视图来减少 复杂的建筑设计并增加灵活性和 代码的可维护性. MVC也已用于简化设计 自主和自我管理系统的设计

The goal of MVC is, by decoupling models and views, to reduce the complexity in architectural design and to increase flexibility and maintainability of code. MVC has also been used to simplify the design of Autonomic and Self-Managed systems

这篇关于模型,视图,控制器混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆