2012-03-05 57 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接口实现。