了解MVC模式 [英] Understanding the MVC Pattern

查看:181
本文介绍了了解MVC模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些无法理解的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.

这是我的理解,在查看,是用户看到的。因此它通常是窗口/形式。在控制器是插图中的查看模式。控制器将在两个方向上的数据流。在需要的时候它也将持续状态(如果我有5个步骤向导,它是控制器的responsability以确保他们以正确的顺序做,等等)。在模式,是我的应用程序逻辑生活​​的核心。

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.

时的这种观点是否正确?

Is this view correct?

要尽量把它变成更有意义的事情,我会尽力勾画出使用的WinForms(不ASP.NET或WPF,请一个简单的例子 - !到java人群,从我明白过来,摇摆方式类似工程的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);
    }
}

现在是时候让我的控制器

class Controller
{
    private MyNumbers myNumbers = new MyNumbers();

    public IList<int> GetNumbers() {
        return myNumbers.GetNumbers();
    }
}



查看应该只是有一个的ListBox 有作为项目>在通过myNumbers

The View should just have a ListBox that has as items all the numbers retrieved in MyNumbers.

如果控制器是负责创建通过myNumbers ?在这个简单的情况下,我认为其可以接受的(如通过myNumbers 会做完全一样的,不管是什么,并没有相关联的状态)。但是让我们假设我要使用所有不同的控制器我的应用程序有通过myNumbers 的同一个实例。我会传递给该控制器(这需要所有其他人)通过myNumbers 的该实例,我想使用。谁去负责呢?在这个例子的WinForms,会是这样的查看?还是那样是创建查看

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 的所有者调用来创建它的代码? $ B $乙方应在控制器同时创建查看模式?如果查看实例化控制器控制器的< ?code>模式

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?

是怎样的方法应该看起来像,假设我只希望我的应用程序有使用案例控制器描绘

How is the main method supposed to look like, assuming I only want my application to have the Use Case this Controller portrays?

为什么在下面的MVC图中,查看有一个箭头模式?应该不是控制器总是查看和两者之间的桥梁模式

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或其他),并允许您访问您的数据。

  • 视图与外部世界的互动,它接收输入的地方,并手离开数据控制器还监听器,以确保其显示正确的数据

  • 控制器是所有魔术发生。控制器操纵数据,推动事件,并处理在两个方向上的变化(从视图和/到/从模型)。

这个图是非常有帮助的(它更有道理比维基百科的):

This diagram is very helpful (it makes much more sense than Wikipedia's):

,并在MVC一个伟大的文章!

Source, and a great article on MVC!

这篇关于了解MVC模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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