Note that there are some explanatory texts on larger screens.

plurals
  1. POUnderstanding the MVC Pattern
    text
    copied!<p>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.</p> <p>From what I understood, the <code>View</code>, is what the user sees. So it generally is the window/form. The <code>Controller</code> is inbetween the <code>View</code> and the <code>Model</code>. 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 <code>Controller</code>'s responsability to ensure they are made in the correct order, etc). The <code>Model,</code> is where the core of my application logic lives.</p> <p>Is this view correct?</p> <p>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.</p> <hr> <p>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):</p> <pre><code>class MyNumbers { private IList&lt;int&gt; listOfNumbers = new List&lt;int&gt; { 1, 3, 5, 7, 9 }; public IList&lt;int&gt; GetNumbers() { return new ReadOnlyCollection&lt;int&gt;(listOfNumbers); } } </code></pre> <p>Now it's time to make my <code>Controller</code>:</p> <pre><code>class Controller { private MyNumbers myNumbers = new MyNumbers(); public IList&lt;int&gt; GetNumbers() { return myNumbers.GetNumbers(); } } </code></pre> <p>The <code>View</code> should just have a <code>ListBox</code> that has as items all the numbers retrieved in <code>MyNumbers</code>.</p> <h2>Now, the first question arises:</h2> <p>Should the <code>Controller</code> be responsible for creating <code>MyNumbers</code>? In this simple case, I think its acceptable(as <code>MyNumbers</code> 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 <code>MyNumbers</code>. I would have to pass to this <code>Controller</code>(and all others that need it) that instance of <code>MyNumbers</code> that I want to use. Who is going to be responsible for that? In this WinForms examples, would that be the <code>View</code>? Or would that be the class that creates the <code>View</code>?</p> <p>Turning the question around: what is the order of instantiation of these 3 parts? What is the code that the "owner" of the <code>MVC</code> called to create it? Should the <code>Controller</code> create both the <code>View</code> and <code>Model</code>? Should the <code>View</code> instantiate the <code>Controller</code> and the <code>Controller</code> the <code>Model</code>?</p> <h2>Second question:</h2> <p>How is the <code>main</code> method supposed to look like, assuming I only want my application to have the <code>Use Case</code> this <code>Controller</code> portrays?</p> <h2>Third:</h2> <p>Why does in the following MVC diagram, the <code>View</code> have an arrow to the <code>Model</code>? Shouldn't the <code>Controller</code> be always the bridge between both <code>View</code> and <code>Model</code>?</p> <p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b5/ModelViewControllerDiagram2.svg/350px-ModelViewControllerDiagram2.svg.png" alt="alt text"></p> <hr> <p>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.</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload