java事件機制

          項目中的一個模塊中使用了java屬性事件機制,最近也在學習中。主要不是學習如何應用了,往深一層學習研究一下。
          java的事件(非屬性事件),我覺得主要關注事件的有和無,如按下一個按鈕,就產生一個事件。主要應用在GUI中,而在AWT,SWING中已經定義好很多事件,我們只要實現就可以了。而在非GUI中,使用事件機制感覺沒什麼太大意義。如監控一個系統,當系統發生一個故障,被監控到,這就產生一個事件,如果按時java的機制,就要定義事件監聽接口,實現接口,定義事件等好多步驟,實際上如下實現還比較直觀:
if (故障產生)
{
         processEorror();
}

沒有再繼續研究了,不過也自己試着寫一個例子,就發送和接收郵件事件,沒什麼高明之處。

事件監聽接口:
package cn.com.cathay.event;

import java.util.EventListener;

public interface MailListener extends EventListener
{
    public void doMailEvent(MailEvent event);
}


//事件對象
package cn.com.cathay.event;

import java.util.EventObject;

public class MailEvent extends EventObject
{
    /**
     * receive and send
     */
    private String action = null;
   
    public MailEvent(Object source, String action)
    {
        super(source);
        this.action = action;
    }
   
    public String getAction()
    {
        return this.action;
    }
   
    public void setAction(String action)
    {
        this.action = action;
    }
}

//實現監聽接口
package cn.com.cathay.event;

public class DoMailEvent implements MailListener
{

    public void doMailEvent(MailEvent event)
    {
        if (event == null || event.getAction() == null)
        {
            return;
        }
       
        if ("send".equalsIgnoreCase(event.getAction()))
        {
            System.out.println("send mail >>>");
           
        }
       
        if ("receive".equalsIgnoreCase(event.getAction()))
        {
            System.out.println("receive mail <<<");
        }
    }
}


//事件源,如一個Button。包括測試代碼。
package cn.com.cathay.event;

import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;

public class MailOperator
{
    protected Collection<MailListener> listenerList = null;
   
    //增加事件
    public void addMailListener(MailListener listener)
    {
        if (listenerList == null)
        {
            listenerList = new HashSet<MailListener>();
        }
       
        listenerList.add(listener);
    }
   
    //移除事件
    public void removeMailListener(MailListener listener)
    {
        if (listenerList == null)
        {
            return;
        }
        else
        {
            listenerList.remove(listener);
        }
    }
   
    //觸發發送郵件事件
    public void fireOnSendMail()
    {
        if (listenerList == null)
        {
            return;
        }
        else
        {
            MailEvent event = new MailEvent("MailOperator", "send");
            doMail(event);
        }
       
    }
   
    //觸發收取郵件事件
    public void fireOnReceiveMail()
    {
        if (listenerList == null)
        {
            return;
        }
        else
        {
            MailEvent event = new MailEvent("MailOperator", "receive");
            doMail(event);
        }
    }
   
    //處理郵件事件
    protected void doMail(MailEvent event)
    {
        Iterator<MailListener> it = listenerList.iterator();
       
        while (it.hasNext())
        {
            MailListener listerner = it.next();
            listerner.doMailEvent(event);
        }
    }
   
    //for test
    public static void main(String[] args)
    {
        MailOperator mail = new MailOperator();
        mail.addMailListener(new DoMailEvent());//註冊事件
        mail.fireOnReceiveMail();
        mail.fireOnSendMail();
    }
   
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章