samedi 20 février 2016

Pattern to force consuming of queue

Currently I have a method to consume events of a Queue. Based on the type I call the appropriate method to handle it.

private void consumeQueue() {
    MyQueue myQueue = MyQueue.getInstance();
    Iterator mIterator = myQueue.iterator();
    while (mIterator.hasNext()) {

        Object mEvent = null;
        try {
            mEvent = mIterator.next();
            mIterator.remove();
        } catch (NoSuchElementException e) {
            return;
        }

        if (mEvent instanceof EventOne)
            handleOne((EventOne) mEvent);
        else if (mEvent instanceof EventTwo)
            handleTwo((EventTwo) mEvent);
        ...
    }
}

Let's suppose that elsewhere the developer enqueue a new type of event, let's call it EventThree. Doing it in this way I can't be sure that the developer will handle EventThree's consumption properly.

Is there any pattern so I can force (at compile time) the developer, to handle EventThree's consumption?

Aucun commentaire:

Enregistrer un commentaire