CPSC 329 Software Development Fall 2017

CPSC 329 Lab 11: Elements of MVC

This lab gives you some practice working with the key elements of the MVC pattern. In particular, you will refactor the maze solver code to better support the Single Responsibility Principle (and separate model from view) and will then implement the Observer pattern to decouple the maze solving from the user interface (and allow greater flexibility in user interfaces).

Objectives

Successful completion of this lab means that you have practice employing key elements of the MVC pattern:

Collaboration

Work in pairs (and one group of three) to complete this lab. Only one person needs to carry out the steps in the lab but everyone in the group should make sure they understand what is going on. There is no limit on collaboration with others, but you need to make sure that you understand for yourself how (and why) to do things.

Due Date and Deliverables

due Tue Nov 28 at the start of class (*)

(*) will be accepted up to 11:59pm without penalty

One handin per group.

To hand in the lab, create a handin tag as directed. Make sure the names of your group members are in the commit comment.


Setup

Create a project for this lab:

Provided Code and Files

The maze solver code is what you worked with in lab 10, refactored to encapsulate what varies using composition. You will only need to work with the solver and ui packages.


SRP

MazeSolverGUI has two responsibilities - it keeps track of the status (discovered, explored, etc) of each cell in the maze as the solver works, and it handles displaying that information on the screen.

The program should compile and run after you do this, though you won't see anything in the display as the solver runs because the GUI is no longer being updated. (You should, however, see the statistics printed out once the maze has been solved.)


Observer

Implementing Observer

The goal with the Observer pattern is to decouple the consumer of the information (the display) from the producer of the information (the maze solver) so that the consumer can change without affecting the producer. To implement the Observer pattern:

The main program now needs to create the two pieces (the solver and the GUI) and link them together by adding the GUI as an observer...except that the GUI should be observing the SolverData object (not MazeSolver) and that object is wrapped by MazeSolver and is only initialized after the maze has been set. Some bookkeeping:

Now the Observer pattern implementation can be completed:

(This design is a bit awkward - there are some robustness issues because a SolverData object is only created after the maze is set and the GUI and the solver must have the same maze object.)

Other Views

Decoupling the user interface from the solver makes it possible to add new ways of viewing the solver state:

The program should now compile and run, with the maze display and printed stats updated as the maze is solved.


If You Have Time

(this section is optional)

Address the awkwardness identified above: currently a SolverData object is only created after the maze is set, and the GUI and the solver must have the same maze object. Give some thought to the design so that your solution is as elegant as possible while maintaining the loose coupling between the solver and the GUI.


Handin


Valid HTML 4.01!