在MVC中,模型为什么要通知视图,视图为什么要有模型? [英] In MVC, why should the model notify the views, and why should the view have the model?

查看:125
本文介绍了在MVC中,模型为什么要通知视图,视图为什么要有模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的世界中,模型仅通知订阅该模型事件的控制器.然后,控制器告诉视图该怎么做,例如将新行添加到列表中.

In my world, the model notifies only the controllers subscribed to the model's event. Then the controller tells the view what to do, for example adding a new row to a list.

与视图相同:视图通知控制器订阅了该视图的事件.然后,控制器根据需要修改模型,例如设置人的名字,然后在模型上调用Save()方法.

The same with the view: the view notifies the controller subscribed to the view's event. Then the controller modifies the model as needed, for example setting the name of a person, and call the Save() method on the model.

好的,我知道我错了,我不认为每一篇有关MVC的文章都是错的,因为我在想另一种方式. MVC中的重点是将UI与数据模型分开.当视图和模型相互到达时,这如何实现?他们为什么要这样做?

Okay, I know I'm wrong, I don't think every article about MVC is wrong because I'm thinking in another way. The point in MVC is to seperate the UI from the data model. How does this come true when the view and the model reach each other? Why should they do so?

感谢您的回答!

推荐答案

模型-视图-控制器被许多人以不同的方式看待,但我想将其视为其他几种模式的组合,而不是单个模式.这可能最初来自本说明

Model-View-Controller is seen different ways by many people, but I like to think of it as a combination of several other patterns rather than as a single pattern. This may come originally from this note

视图与模型的连接是一个观察者模式,模型会在视图更改时通知该视图.不需要控制器参与其中.

The connection of the view to the model is an Observer Pattern, with the model notifying the view when it has changed. There's no need for the controller to be involved in this.

这篇关于在MVC中,模型为什么要通知视图,视图为什么要有模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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