Applying the proposed solution to the example.

Patterns in Java
Collections for States
Kevlin Henney
Listing 4. Applying the proposed solution to the example.


public abstract class Workpiece
{
    public abstract void save();
    ...
}

public class Application
{
    public void saveChanges()
    {
        Iterator workpiece = changed.iterator();
        while(workpiece.hasNext())
            ((Workpiece) workpiece.next()).save();
        saved.addAll(changed);
        changed.clear();
    }
    ...
    private Collection saved, changed;
}

About the Author

Kevlin Henney is a Principal Technologist with QA Training in the UK.