MVC:为什么要分离模型,视图和控制器? [英] MVC: why the separation of model, view, and controller?

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

问题描述

除了它的哲学"方面,让我的控制器也成为我的模型不是一个好主意吗?

Other than the "philosophical" aspects of it, is it a bad idea to have my controller also be my model?

似乎节省了一些编程时间.因为是同一回事,所以我不必在控制器和模型之间创建逻辑.而且我可以直接与视图进行交互.

It seems to save some programming time. I don’t have to create logic between the controller and the model, since it’s the same thing. And I can directly interact with the view.

分离M和C有什么意义?模块化(即,将一种模型和控制器集交换为另一种模型和能力的能力)是分离它们的唯一原因吗?在我看来,交换"模块的发生比(例如)不必更新模型和控制器要少得多,因为模型中的某些内容正在发生变化.

What’s the point of separating the M and the C? Is modularity — that is, the ability to swap one model and controller set for another — the only reason to separate them? It seems to me that "swapping" modules out happens a lot less than (for example) having to update both the model and the controller because something in the model is changing.

根据MVC的概念,一个简单的计算器应该既具有控制器又具有其设置的视图(例如默认设置等),这似乎很奇怪.我知道这是一个简单的示例,但是它似乎适用于所有情况(也许除了框架).

It seems odd that a simple calculator, according to the MVC concept, should have both a controller and a view for its settings (like default settings, or something). I know this is a simple example, but it seems to apply to all cases (except maybe frameworks).

推荐答案

主要原因是代码的可重用性.如果您在职业生涯中只打算编写一个程序,那么也许没关系.如果您打算从事这项事业,那么拥有可重复使用的零件将很有价值.设计良好的模型,控制器和视图类很容易放入其他程序中.我一直都这样做.

The primary reason is for reusability of code. If you’re only ever going to write one program in your professional life, then perhaps it doesn’t matter. If you plan to make a career of it, having reusable pieces is valuable. Well-designed model, controller and view classes are very easy to drop into other programs. I do this all the time.

请考虑UITableViewController,它是一个控制器.现在,假设它是专门为处理音乐曲目(模型)而设计的,并且当您要处理其他内容时,您需要创建一个完全不同的表管理类.避免这种噩梦是可可豆中大量使用MVC的原因.

Consider UITableViewController, which is a Controller. Now imagine if it were designed exclusively to handle music tracks (the Model), and you needed to create a completely different table-management class when you wanted to handle something else. Avoiding this nightmare is why MVC is heavily used in Cocoa.

还有其他方法可以拆分内容.一些语言严重继承而不是委派.但是在可可中,拆分程序的主要方法是MVC,并且效果很好.

There are other ways to split things up. Some languages subclass heavily rather than delegating. But in Cocoa, the primary means of splitting up programs is MVC, and it works very well.

编辑:来自开发商业应用程序的其他一些原因.

Just some more reasons from the world of developing commercial apps.

  • 在MVC中,内存处理要容易得多.当它们离开屏幕时,您可以保留模型对象,并扔掉它们的视图对象(以及许多控制器对象).

  • Memory handling is much easier in MVC. You can hold on to your model objects and throw away your view objects (and many of your controller objects) when they go offscreen.

序列化不包含控制器和视图的模型对象更加容易,并且以多种方式显示相同数据也更加容易.即使在简单"文本编辑器中,您也可能希望能够进行分屏显示,或者具有显示同一文档的多个窗口.在MVC中,这非常简单.

It’s easier to serialize model objects that aren’t wrapped up with controllers and views, and it’s much easier to display the same data in multiple ways. Even in a "simple" text editor, you may want to be able to do split-screen, or have multiple windows showing the same document. In MVC that’s very easy.

如果现在或将来不需要灵活性,则不需要太多架构.但是,大多数实际项目并非如此简单. MVC源于Xerox编写大型程序的经验以及将所有东西放在一起时遇到的困难.

If you need no flexibility now or in the future, you don’t need much architecture. But most real projects aren’t so simple. MVC grew out of Xerox’s experience with writing large programs and the difficulties encountered when everything was thrown together.

我正在看您以前的根据MVC的概念,一个简单的计算器应该既具有控制器又具有其设置的视图(例如默认设置),这似乎很奇怪.设置等).

EDIT 2: I was looking at your earlier edit: "It seems odd that a simple calculator, according to the MVC concept, should have both a controller and a view for its settings (like default settings, or something)."

这正是MVC的原因.必须重新编码保存用于计算器应用程序的用户设置所需的所有内容,这似乎很疯狂.您想要一个与用户界面完全独立并且可以重复使用的通用请保存这些用户设置".在OS X上,它称为NSUserDefaults,而Calculator应用程序正是以这种方式存储其配置.

This is exactly the reason for MVC. It would seem crazy to have to re-code all of the things required for saving user settings specially for a Calculator app. You’d want a generic "please save these user settings" that was completely separate from the UI and that you could reuse. On OS X it’s called NSUserDefaults, and the Calculator app stores its configuration in exactly this way.

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

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