理解MVC模式 [英] Understanding the MVC Pattern
问题描述
我在理解 MVC 模式时遇到了一些麻烦.我确实理解我们正在尝试将 GUI 与业务逻辑分离,尽管我在理解如何分离时遇到了问题.
I am having some trouble understanding the MVC Pattern. I do understand we are trying to decouple the GUI from the business logic, although I'm having problems understanding how.
据我所知,View
是用户看到的.所以它通常是窗口/窗体.Controller
位于 View
和 Model
之间.控制器将使数据在两个方向流动".它也会在需要时保持状态(如果我有一个有 5 个步骤的向导,Controller
负责确保它们按正确的顺序制作,等等).Model,
是我的应用程序逻辑的核心所在.
From what I understood, the View
, is what the user sees. So it generally is the window/form. The Controller
is inbetween the View
and the Model
. The Controller will make the data "flow" in both directions. It will also persist state when needed (if I have a wizard with 5 steps, it is the Controller
's responsability to ensure they are made in the correct order, etc). The Model,
is where the core of my application logic lives.
这个观点正确吗?
为了让它变得更有意义,我将尝试用 WinForms 勾勒出一个简单的示例(请不要使用 ASP.NET 或 WPF! - 对于 Java 人群,据我了解,Swing 可以工作以类似于 WinForms 的方式!),看看我是否做对了,我会提出我在做的时候经常遇到的问题.
To try to turn this into something more meaningful, I'll try to sketch out a simple example with WinForms(no ASP.NET or WPF, please! - to the java crowd, from what I come to understand, Swing works in a similar way to WinForms!), to see if I get it right, and I'll raise the questions I always come to while doing it.
假设我有一个只包含一个类的模型(只是为了使它更容易.我知道这会使示例看起来很笨,但这样更容易):
Let's assume I have a model that contains just a class (just to make it easier. I know it will make the example look dumb but its easier this way):
class MyNumbers {
private IList<int> listOfNumbers = new List<int> { 1, 3, 5, 7, 9 };
public IList<int> GetNumbers() {
return new ReadOnlyCollection<int>(listOfNumbers);
}
}
现在是制作我的Controller
的时候了:
Now it's time to make my Controller
:
class Controller
{
private MyNumbers myNumbers = new MyNumbers();
public IList<int> GetNumbers() {
return myNumbers.GetNumbers();
}
}
View
应该只有一个 ListBox
,它包含在 MyNumbers
中检索到的所有数字作为项目.
The View
should just have a ListBox
that has as items all the numbers retrieved in MyNumbers
.
Controller
是否应该负责创建MyNumbers
?在这个简单的例子中,我认为它是可以接受的(因为 MyNumbers
将完全相同,无论如何,并且没有关联的状态).但是让我们假设我想为所有不同的控制器使用我的应用程序具有相同的 MyNumbers
实例.我必须将我想要使用的 MyNumbers
实例传递给这个 Controller
(以及所有其他需要它的人).谁将为此负责?在这个 WinForms 示例中,那会是 View
吗?或者那会是创建 View
的类?
Should the Controller
be responsible for creating MyNumbers
? In this simple case, I think its acceptable(as MyNumbers
will do exactly the same, no matter what, and has no associated state). But let's assume I would want to use for all the different Controllers my app has the same instance of MyNumbers
. I would have to pass to this Controller
(and all others that need it) that instance of MyNumbers
that I want to use. Who is going to be responsible for that? In this WinForms examples, would that be the View
? Or would that be the class that creates the View
?
反过来问:这3个部分的实例化顺序是什么?MVC
的所有者"调用创建它的代码是什么?Controller
是否应该同时创建 View
和 Model
?View
是否应该实例化Controller
和Controller
实例化Model
?
Turning the question around: what is the order of instantiation of these 3 parts? What is the code that the "owner" of the MVC
called to create it?
Should the Controller
create both the View
and Model
? Should the View
instantiate the Controller
and the Controller
the Model
?
main
方法应该是什么样子的,假设我只希望我的应用程序具有这个 Controller
描绘的 Use Case
?
How is the main
method supposed to look like, assuming I only want my application to have the Use Case
this Controller
portrays?
为什么在下面的 MVC 图中,View
有一个指向 Model
的箭头?Controller
不应该一直是 View
和 Model
之间的桥梁吗?
Why does in the following MVC diagram, the View
have an arrow to the Model
? Shouldn't the Controller
be always the bridge between both View
and Model
?
我会再问一两个问题,但在我了解第一个细节后,这些问题可能会更有意义.或者也许在我理解了第一个问题之后,所有其他问题都会分开.
I'll have one or two more questions, but they probably will make more sense asked after I understand this first detail. Or maybe after I understand that first question all the others tear apart.
谢谢!
推荐答案
掌握 MVC 的最简单方法是在强制执行它的框架中使用它,这就是说..
The easiest way to get a handle on MVC is to use it in a framework that enforces it, that being said..
- 模型与数据源(DB 或其他)交互并让您可以访问您的数据.
- View 与外部世界交互,它接收来自某处的输入并将数据传递给 Controller,它还侦听 Controller 以确保其显示正确的数据.
- 控制器是所有魔法发生的地方;Controller 操作数据、推送事件并处理两个方向的更改(到/从视图和到/从模型).
这张图很有帮助(它比维基百科的更有意义):
This diagram is very helpful (it makes much more sense than Wikipedia's):
来源,以及一篇关于 MVC 的精彩文章!
Source, and a great article on MVC!
这篇关于理解MVC模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!