Short exercise for Memento and Command

The following scenario is abridged from Joel Spolsky, User Interface Design for Programmers, Apress, 2001, pages 105-106. (You are encouraged to read the full version in the lab's copy of the book; it has nothing to do with the Memento or Command patterns, but it is an amusing read.)

You discover that there's a real, unmet need for grilled marshmallows in the workplace. So you set out to create a marshmallow-grilling device that you hope will take the world by storm.

While you are trying to explain the whole concept to a manufacturer of business-grade heating elements, the guy erupts in laughter. "Grilled marshmallows? Without graham crackers? Who would eat one of those things without graham crackers?" Many of the people you talked to stated that they hate graham crackers, but a bit more research shows that there's a whole population of people that just will not eat their grilled marshmallows unless they have graham crackers.

The CEO you hired points out that you're now two thirds of the way to making s'mores, so you add a chocolate bar option.

The usability testing reveals that some people like their marshmallows almost raw, while others want them virtually charred with liquefied middles (mmmm). It's not just a matter of how long they cook: it's a matter of how quickly you turn them over the heat and how high the heat is.

"This is for the workplace," your head UI designer tells you. "It will be used by many different people. Those people aren't going to want to remember all their personal settings" So you add a Save Settings button with a little LCD panel, and a Load Settings button. Now you can file your personal marshmallow-grilling settings under your own name.

Below is code for the model portion of the software controlling the marshmallow griller. The personal settings feature is unimplemented. Implement this two ways: a. using the Memento pattern; b. using the Command pattern. You may change the parameter and return type of saveSettings() and restoreSettings(); for example, saveSettings() could return a Memento. Alternately, Mementos could be stored internally.

class MarshmallowSettings {

    private double heatLevel;   // 0 to 1
    private int rotisserieSpeed; // 1 to 5
    private int rotisserieTime;  // seconds
    private int numGrahamCrackers;  // 0, 1, or 2
    private boolean chocolate;

    public MarshmallowSettings() { clear(); }

    /**
     * Basic setting. One marshmallow, medium roast.
     */
    public void clear() {
        heatLevel = .5;
        rotisserieSpeed = 3;
        rotisserieTime = 30;
        numGrahamCrackers = 0;
        chocolate = false;
    }

    public void setHeat(double heatLevel) { this.heatLevel = heatLevel; }
    public void upSpeed() {  if (rotisserieSpeed < 5) rotisserieSpeed++; }
    public void downSpeed() { if (rotisserieSpeed > 1) rotisserieSpeed--; }
    public void upTime() { rotisserieTime++; }
    public void downTime() { rotisserieTime--; }
    public void addCracker() { if (numGrahamCrackers < 2) numGrahamCrackers++; }
    public void addChocolate() { chocolate = true; }

    public void saveSettings(String userName) { }
    public void restoreSettings(String userName) { }
}

Please turn this in by email by next Wednesday.


Last modified: Fri Feb 13 11:07:50 CST 2009