Assignment 1: Model-View-Controller (Java)

Combination Lock

Due Friday, May 23 at 5:00 PM


The average (non-zero) mark is 88.3%. The distribution of marks above 40% is:

CS349 A1 Marks


Some accumulated questions to date that seem worth spreading widely:
  • I've received many questions about keeping the views in sync without adding stuff to the model. I'm trying to avoid offending my object-oriented design sensibilities by putting stuff in an object called a "lock" that doesn't seem to be part of the idea of a lock (eg the combination that I'm about to try).

    I believe the cleanest solution lies in using the Singleton design pattern in the Factory class. Briefly, the first time a view is created, create a UI model to keep views synchronized on the current combination choice. Every time a view is created, pass that UI model (along with the application model) to the view. That allows you to do everything you need to do within the bounds of the interfaces provided and without even needing additional interfaces, etc.

    If you're unfamiliar with the Singleton design pattern, in this case it boils down to essentially:

        private MyUiModel uiModel = null;
        public IView getView1(ILock model) {
            if (this.uiModel == null) {
                // create it
            // create the view using both model and uiModel
  • The combination for the lock is set when the lock is created. It can be hard-coded right into the program. Some people mentioned setting it to the first combination tried; that's incorrect.

    I toyed with the idea of specifying it on the command line when the program is run, such as

    java comboLock.Main 23 39 85 10

    for a 4-number combination. Obviously, that idea didn't survive editing.

  • You may put reasonable limits on the length of the combination. You might, for example, say that combinations must have between 2 and 5 numbers, inclusive. You may not say that it has exactly n numbers. Document the upper and lower bounds in your Readme file. You must accomodate at least 3 and 4-number combinations.
In the provided code, the test testUnlockedStaysUnlocked should assert assertFalse rather than assertTrue.


Model-View-Controller (MVC) is the foundational design pattern for graphical user interfaces. In this very modest assignment you'll write a program that simulates a combination lock. It will stress the MVC pattern to help you get it right for future assignments.

Learning Goals


Write a program that simulates a combination lock.


It's expected that you have at least two views that allow for entering a new combination and that as the combination is entered in one view, the other view(s) reflect the changes in the first view.

One approach is to enhance the model to include additional state for the current setting of the combination with methods to set and get that state as well as notifying views, as appropriate. The problem with this approach is that it's adding UI-specific code to the model. It's imposing additional complexity on a nice, coherent model purely for the benefit of the UI. Not good.

There are a couple of alternatives. First, you could add a new model class that is entirely within the UI. Each time the combination changes in a view, tell the UI model about it. It, of course, has views registered on it and notifies them appropriately. There's nothing wrong with having a view registered with two different models.

Another alternative is to take inspiration from JTable and implement an abstract model that the application's model extends to provide the extra functionality.

It doesn't matter which of these approaches (or a possible third approach) you use. What does matter is that the views stay in synch and that your solution clearly builds on the MVC architecture. Document your approach in the README.txt file.


The sample code somes with a small set of tests that can be run using the Makefile. You are encouraged to add some additional tests to the test suite, ideally before you code them. An excellent test to include (hint, hint) is verifing that all views are notified when one of them changes the combination to be guessed.

Bonus Enhancement

Implement a custom component that shows the state of the lock (locked or unlocked) graphically. You'll need to extend JComponent and override the paintComponent method. See the free textbook for more background.


Check in to your personal SVN repository:


Note: This assignment is worth less than the remaining assignments.

Compiles and displays a frame
Program architecture: MVC
Appropriate use of layout managers
Other requirements
Pleasing interactions and appearance
Bonus enhancement