java事件机制

来源:互联网 发布:淘宝买的东西不合适 编辑:程序博客网 时间:2024/06/03 19:00
          项目中的一个模块中使用了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();
    }
   
}

0 0
原创粉丝点击