2012-03-05 60 views
0

我有一個NotificationEvent的實例。無論何時創建此實例,我都已將此實例添加到隊列中。隊列的名稱應爲NotificationQueue。 NotificationEvent的如何在javaBeans中實現隊列

結構是這樣的:

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所需的結構?

回答

0

我建議不要重新發明輪子。已經在Java運行時庫中的接口Queue定義了隊列應具有的操作。這裏有一個brief tutorial for the Queue interfaceQueue JavaDoc。那麼,這裏也是一個example of using Queue implementations

你可以這樣創建一個通知隊列對象:

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接口實現,請參閱Queue JavaDoc以獲取Java運行時庫中已有的其他實現。當然,你也可以編寫自己的Queue接口實現。