Memento (Chapter 19)

SOLUTION 19.1Storing a memento as an object assumes that the application will still be running when the user wants to restore the original object. Reasons that will force you to save a memento to persistent storage follow.
  • The ability to restore an object's state has to survive a system crash.

  • You anticipate that the user will exit the system and will want to resume work later.

  • You need to reconstruct an object on another computer.

SOLUTION 19.2Here is an implementation of createMemento() for FactorySimulator:
public List createMemento()
{
    List list = new ArrayList();
    Iterator i = machines.iterator();
    while (i.hasNext())
    {
        MachineImage mi = (MachineImage) i.next();
        list.add(mi.clone());
    }
    return list;
}

When you write a

Get Design Patterns Java™ Workbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.