mardi 14 juin 2016

Observable pattern applied in massive record update

I'm trying to apply Observer pattern to a class that contains a set of records.

In Java pseudo code my class is:

public class MyClass<E> extends Observable
{
    private ArrayList<E> items = new ArrayList<E>();

    public MyClass()
    {
    }

    public void add(E e)
    {
        this.items.add(e);

        setChanged();
        notifyObservers();
    }

    public void remove(E e)
    {
        if(this.items.remove(e))
        {
            setChanged();
            notifyObservers();
        }
    }

    ...
}

My problem is: in some cases during program execution I have to do massive deletes and inserts of records in the structure and, for performances purposes, I would like that observer objects are notified only once at end of the whole operation.

Of course I can add some flag variables for handling the calls to setChanged and notifyObservers, but I wonder what is the "best" way to handle this kind of problem.

So my question: What is the best way to design the code to notify observer objects only once after a massive modification of the observed object?

Aucun commentaire:

Enregistrer un commentaire