как реализовать очередь в javaBeans - PullRequest
0 голосов
/ 05 марта 2012

У меня есть экземпляр NotificationEvent. я добавляю этот экземпляр в очередь при каждом его создании. Имя очереди должно быть NotificationQueue.

структура NotificationEvent выглядит следующим образом:

public class NotificationEvent {

    private String sender;
    private String receiver;
    private String message;

    /**
     * @return the sender
     */
    public String getSender() {
        return sender;
    }

    /**
     * @param sender the sender to set
     */
    public void setSender(String sender) {
        this.sender = sender;
    }

    /**
     * @return the receiver
     */
    public String getReceiver() {
        return receiver;
    }

    /**
     * @param receiver the receiver to set
     */
    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    /**
     * @return the message
     */
    public String getMessage() {
        return message;
    }

    /**
     * @param message the message to set
     */
    public void setMessage(String message) {
        this.message = message;
    }

Какой должна быть необходимая структура NotificationQueue?

1 Ответ

0 голосов
/ 06 марта 2012

Предлагаю больше не изобретать велосипед.Интерфейс Queue, уже находящийся в библиотеке времени выполнения Java, определяет операции, которые должна выполнять очередь.Вот краткое руководство по для интерфейса Queue и JavaDoc Queue.Ну, вот также пример использования реализаций очереди .

Вы можете создать объект очереди уведомлений, например так:

Queue<NotificationEvent> eventQueue = new LinkedList<NotificationEvent>;

или, если вы настаиваете на том, чтобы вашсобственный тип для очереди:

public class extends LinkedList<NotificationEvent> {
    /**
     * Constructs an empty list.
     */
    public NotificationQueue() {
    }

    /**
     * Constructs a list containing the elements of the specified collection,
     * in the order they are returned by the
     * collection's iterator.
     * @param c the collection whose elements are to be placed into this list
     * @throws NullPointerException if the specified collection is null
     */
    public NotificationQueue(Collection<? extends NotificationEvent> c) {
        super(c);
    }
}

...

NotificationQueue eventQueue == new NotificationQueue();

Примечание:LinkedList - не единственная доступная реализация интерфейса Queue, посмотрите JavaDoc очереди для других реализаций, уже доступных в библиотеках времени выполнения Java.Конечно, вы также можете написать собственную реализацию интерфейса Queue.

...