Main business-oriented model.

Java Means Business
Business-oriented models for complex Swing apps
Lowell Kaplan
Listing 2. Main business-oriented model.


import java.util.*;
public class MasterDataModel
{
    private Vector listeners = new Vector();
    private Vector stockQuotes = new Vector();

    public void receivedQuote(StockQuote quote)
    {
        boolean add = false;
        int index = stockQuotes.indexOf(quote);
        if (index != -1)
        //if have an old quote for this symbol, update it
        {
            StockQuote oldQuote =
                        (StockQuote)stockQuotes.elementAt(index);
            oldQuote.updateData(quote);
        }
        else
        {
            stockQuotes.addElement(quote);
            add = true;
        }
        fireModelChanged(add);
    }

    public Vector getStockQuotes()
    {
        return stockQuotes;
    }

    public void addDataUpdateListener(DataUpdateListener l)
    {
        listeners.addElement(l);
    }

    public void removeDataUpdateListener(DataUpdateListener l)
    {
        listeners.removeElement(l);
    }

    public void fireModelChanged(boolean add)
    {
        for (int i=0;i<listeners.size();i++)
        {
            DataUpdateListener l =
                        (DataUpdateListener)listeners.elementAt(i);
            1.update(add);
        }
    }
}

public interface DataUpdateListener
{
    public void update(boolean add);
}