The workpiece assumes responsibility for saving or not, depending on its state.

Patterns in Java
Collections for States
Kevlin Henney
Listing 3. The workpiece assumes responsibility for saving or not, depending on its state.


public abstract class Workpiece
{
    public void save()
    {
        if(changed)
        {
            saveState();
            changed = false;
        }
    }
    ...
}

public class Application
{
    public void saveChanges()
    {
        Iterator workpiece = changed.iterator();
        while(workpiece.hasNext())
            ((Workpiece) workpiece.next()).save();
    }
    ...
}

About the Author

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