Mastering OO - MVC Exercise

1/6/00 - Go Back

The MVC Pattern is the granddaddy of all Design Patterns. Mastery of it is necessary to proceed to mastering other Design Patterns, as well as more deeply understanding OO and reuse.


The MVC Pattern

The MVC Pattern has a Model, View and Controller with these responsibilities:

Model - Holds the data to be displayed. Emits events to ModelListners when model data is changed.

View - Displays the data in the Model, by being a ModelListener. Emits events to ViewListeners when GUI events occur.

Controller - Controls the logic of the subsystem by being a ViewListener and handling ViewEvents appropropriately. Manually calls methods on the Model to change the data in the Model.

Everything else can vary, depending on your particular design needs. We have found it useful to add events, which requires a ViewListener and ModelListener interface, and a ViewEvent and ModelEvent class. To make it all more reusable, we use the Mediator Pattern and add a Mediator. The result is:

The methods shown are typical, not exact. Your design will have different methods, such as the ViewEvent may have setCommand(String command) and getCommand().

Notice the circular event flow. This is the hallmark of this pattern.

Study the pattern Class Diagram and trace the flow of events. How are the relationships established? Can the diagram support more that one View? More than one Model? More than one Controller?


The Three Door Game

Here's an intriguing game we will use to learn the MVC Pattern. Jim Worthington presented it in an OO exercise for the JSL in 1997. Steve Alexander found this link to it, where it's called "The Monty Hall Problem". Here's the game:

Imagine a Game Show type of game where there are three doors a contestent can choose from. Behind one of the doors is a super prize, such as a Car. Behind the other two doors is something useless, such as a Goat. The objective is to win the Car.

When the game starts, the Car is ramdomly placed behind one door, and the Goats behind the other doors. The contestent picks a door. The Game Show host then opens a door the contestent did not pick and reveals a goat. The contestent is then asked if they would like to change their door choice or stick with the door they frist selected. What strategy should the contestent follow to maximize their chance of winning the Car?

Basically the contestent can follow these strategies:

  1. Stick with their first choice.
  2. Choose the other door that is closed.

Here's a probablity theory question - What are the odds of winning with each strategy? That's just a thinker question, you don't need to answer it to proceed. But perhaps you can build a software system that can answer that question.... :-)


Requirements

Now that we've discussed the pattern and the game, your requirements are to design and implement a software system that plays the Three Door Game using the MVC Pattern and following the Mini Process. Here's a summary of the Mini Process and the Nutshell Vision and Narrative that, together with the above, serve as your complete requirements. These are a little fuzzy, which is real world. :-)


Mini Process

1. Nutshell Vision - Sentence or paragraph capturing the pure essence and goal.

2. Narrative - Describes what the system must do, key features, issues, etc.

3. Use Case List - A list of the Use Case names the system must satisfy.

4. Major Use Cases Stepped Out - List all steps for crucial or fuzzy Use Cases.

5. Modeling - Conceptual Model, Class Diagram or other types.

6. Implementation Plan - A series of steps to implement the model(s).

7. Implementation Iterations - Often with elaboration of above steps per iteration.

8. Automatic Regression Test - Per iteration, or whatever other type of test you prefer.


Nutshell Vision

A "Three Door Game" player that allows multiple games to be played simultaneously, by human or automated players, and shows the running score.


Narrative

The game player should present two visual "Game Windows", each of which is a complete game. A Game Window has three doors, a button to open each door, and a running score showing the total won, lost and percent won. When a game starts, all three doors are closed. The user then clicks on one door button, and that door is shown as "selected". The host then opens one of the other doors, revealing the goat. The user can then click on the other unopened door to choose it, or they can keep their first choice. The user then clicks on Show Game Result. All unopened doors open and the score is updated. The user can then click on Start New Game, and the whole thing starts over, except for the running score which is maintained.

The better strategy can be determined by using strategy A on one window, and B on the other. The running score will quickly show what strategy is best.

Our company has a high reuse policy. Build this system with only one Controller, one Model and One View class. In the actual system, use one Controller, two Model and two View instances. Hint - One Controller instance requires a way to tell what View was the event source, so that the correct Model instance can be mutated.


Exercise

1. Understand the requirements. Interview the customer, take notes, etc as needed.

2. Understand the MVC Pattern. Again, ask questions if needed, or even draw up a practice model.

3. Write your own Nutshell Vision and Narrative in your own words. Be unambiguous.

4. Proceed with the remainder of the Mini Process steps.


Give this a try on your own, and then see the Mini Process Steps.