Event mechanism in java (continued)

java event mechanism (continued)
- custom events
 
First question before we start: Are you familiar with the java.util.EventObject and java.util.EventListener classes and their existing subclasses?
If you have been able to use the event listeners provided by jdk proficiently, and are familiar with the events prepared by jdk such as MouseEvent, KeyEvent, WindowEvent, etc., then you must have some understanding of the event mechanism of java. But maybe you still think that although there is no problem in using it, the principle is still a bit confusing, so let's further implement these events and listeners by ourselves. We call this a custom event.
 
In fact, custom events are very useful in java. Sometimes we want our program to generate an event, but we don't want (or can't) use input devices such as mouse and keyboard to operate. For example, if you write an application Program, in this program, once the mail is received, the mail will be processed. For the event of "received mail", there is no definition in jdk. For such events, and listeners for such events, we can only do it ourselves.
 
So let's start our "innovation" process with an example: First, we need to clarify the resources needed in jdk: the class EventObject is used as the parent class to generate our own event classes, and the interface EventListener is used to implement our own listeners; All that's left is how to register these events and test them.
Let's implement it step by step:
(1)       Create the DemoEvent class through the DemoEvent.java file, which inherits EventObject. The parameters of the constructor of this class pass the event source (such as various controls) that generated the event, and the method getSource is used to obtain a reference to the event source.
DemoEvent.java
package demo.listener;
 
import java.util.EventObject;
 
public class DemoEvent extends EventObject
{
        Object obj;
        public DemoEvent(Object source)
        {
               super(source);
               obj = source;
        }
        public Object getSource()
        {
               return obj;
        }
        public void say()
        {
               System.out.println("This is say method...");
        }
}
 
(2)       Define a new event listener interface, which inherits from EventListener; this interface contains the handler for the DemeEvent event:
DemoListener.java
package demo.listener;
 
import java.util.EventListener;
 
public interface DemoListener extends EventListener
{
       public void demoEvent(DemoEvent dm);
}
 
Through the above interface, we define the event listener classes, which specifically implement the monitoring function and event processing function. Recalling the four implementation methods above, aren't we using the third one here - the way of writing external classes?
Listener1.java
package demo.listener;
 
public class Listener1 implements DemoListener
{
       public void demoEvent(DemoEvent de)
       {
              System.out.println("Inside listener1...");
       }
}


Listener2.java
package demo.listener;
 
public class Listener2 implements DemoListener
{
       public void demoEvent(DemoEvent de)
       {
              System.out.println("Inside listener2...");
       }
}

Listener3.java
package demo.listener;
 
public class Listener3 implements DemoListener
{
       public void demoEvent(DemoEvent de)
       {
              System.out.println("Inside listener3...");
       }
}
 
(3)       通过DemeSource..ava文件创造一个事件源类,它用一个java.utile.Vector对象来存储所有的事件监听器对象,存储方式是通过addListener(..)这样的方法。notifyDemeEvent(..)是触发事件的方法,用来通知系统:事件发生了,你调用相应的处理函数(回调函数)吧。
DemoSource.java
 
package demo.listener;
import java.util.*;
 
public class DemoSource
{
       private Vector repository = new Vector();
       DemoListener dl;
       public DemoSource()
       {
 
       }
       public void addDemoListener(DemoListener dl)
       {
              repository.addElement(dl);
       }
       public void notifyDemoEvent()
       {
              Enumeration enum = repository.elements();
              while(enum.hasMoreElements())
              {
                    dl = (DemoListener)enum.nextElement();
                    dl.demoEvent(new DemoEvent(this));
              }
       }
}
 
 
             
(4)       好了,最后写一个测试程序测试一下我们自定义的事件吧,这段程序应该不难理解吧:)
TestDemo.java
 
package demo.listener;
 
public class TestDemo
{
       DemoSource ds;
 
       public TestDemo()
       {
              try{
                    ds = new DemoSource();
                    Listener1 l1 = new Listener1();
                    Listener2 l2 = new Listener2();
                    Listener3 l3 = new Listener3();
 
                    ds.addDemoListener(l1);
                    ds.addDemoListener(l2);
                    ds.addDemoListener(l3);
 
                    ds.notifyDemoEvent();
 
              }catch(Exception ex)
              {ex.printStackTrace();}
       }
 
       public static void main(String args[])
       {
              new TestDemo();
       }
}
 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325849633&siteId=291194637