MVC Explained

Still struggling with MVC? Perhaps an alternate explanation will help…

Stanford University’s CS193P class’s Lecture 1 slides 11-37 provide a nicely illustrated overview of MVC: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/system/files/lectures/Lecture%201_1.pdf

Open those slides up, then we’ll go through them and relate their elements to elements of HW02 and HW03.

  1. Adapters a component in some implementations of the MVC design pattern. Adapters are not required in order for MVC to exist.
  2. An implementation of “Target Actions” can be found in Java’s some_button.addActionListener( some_listener ); In this case, some_button is the view, and some_listener is the controller. In Homeworks 2 and 3, we don’t use this directly. Instead we either use an adapter from the Controller as the some_listener. Target Actions let data flow from the View to the Controller.
  3. An implementation of “Delegation” is the “Strategies” that were introduced in Lecture 09. In Homework 2, you may wish to have the View as a delegate of the Model, so that the model can ask what size the screen is. This is not allowed in MVC (as the View and Model cannot talk to each other), so you should instead give the Model an  Adapter created by the Controller, that allows access to the view’s dimensions. In Homework 3, balls will have one or more IStrategy objects that they will call some method on which will modify the parameters used to paint, and then once the IStrategy(s) have finished, a ball will attempt to paint. These IStrategy objects could be given to a ball by a variety of sources. The balls are delegating the parameters of their painting to the IStrategy objecs. Delegation lets data flow into the View and Model from arbitrary sources.
  4. An implementation of the “Radio Station” can be found in the Observable-Observer design pattern. In Homework 2 and 3, balls want to know when they need to update themselves, so they “tune in” (as Observers) to the (Observable) model’s radio station. When the model announces that it has updated, everything that is tuned in is notified. You may also wish for the View to tune in to the Model so it can update… but MVC doesn’t allow this. However, the View can tune in to an Adapter provided by the Controller… Radio Stations let arbitrary quantities of objects become aware of state changes in other, important objects.
  5. “Target actions” are a one-to-one means of communication, while “radio stations” are a one-to-many means of communication. Delegation is a one-to-one means of communication. Any individual target action, adapter, radio station, or delegate is a one-way communication channel.
  6. In cases where data needs to flow from Model to View or View to Model (“time to update the display” or “the add ball button was clicked,” for example), the message must make a pit stop at the Controller, who will route it and/or translate it to the right place. This can be accomplished in one of two ways. Method 2 is better MVC.
    1. The Controller implements an IController interface that has all the methods that a View may need to call, and all the methods that a Model may need to call, implements the methods of that interface, then gives itself to the Model and View as the go-to guy for all tasks.
    2. The Controller creates Adapters that contain that same code, and hands them out to the Model and View as necessary. The Model and View must implement IModel and IView interfaces that specify setter methods for the adapters.