好久就想写出来,这段时间都不知道在忙什么,今天好点,写成文吧:)
开始看到java类中有的addListener()方法,感觉就是一个方法,很简单啊,后来自己想写事件,才发觉这里头还是有些学问的。
MyChangeEvent.java 事件类,继承了java.util.EventObject
MyChangeListener.java 监听接口,继承了java.util.EventListener
MyClass.java 实现了事件的处理机制
MyEventTest.java Junit的测试
/*
* Created on Nov 26, 2004 11:25:14 PM
*
* MyChangeEvent.java
**/
package org.yz21.study.event;
import java.util.EventObject;
/**
* @author violin
*
* Copyright www.yz21.org 2003-2004
*/
public class MyChangeEvent extends EventObject {
private String myname;
/**
* @param source
*/
public MyChangeEvent(Object source) {
super(source);
}
/*
* @param source myname
*/
public MyChangeEvent(Object source,String myname) {
super(source);
this.myname = myname;
}
/*
* @return String
*/
public String getMyname() {
return myname;
}
}
/*
* Created on Nov 26, 2004 11:27:55 PM
*
* MyChangeListener.java
**/
package org.yz21.study.event;
import java.util.EventListener;
/**
* @author violin
*
* Copyright www.yz21.org 2003-2004
*/
public interface MyChangeListener extends EventListener {
/*
* @param event
*/
public void MyChanged(MyChangeEvent event);
}
/*
* Created on Nov 26, 2004 11:32:01 PM
*
* MyClass.java
**/
package org.yz21.study.event;
import java.util.Iterator;
import java.util.Vector;
/**
* @author violin
*
* Copyright www.yz21.org 2003-2004
*/
public class MyClass {
private String name = "2004-9-27";
private static Vector myChangeListeners = new Vector();
/**
*
*/
public MyClass() {
super();
}
/*
* @param myname
*/
public void testMyEvent(String myname) {
this.name = myname;
notifyMyChangeListeners();
}
/*
* @param listener add a listener;
*/
public void addMyChangeListener(MyChangeListener listener) {
myChangeListeners.add(listener);
}
/*
* @param listener remove a listener;
*/
public void removeMyChangeListener(MyChangeListener listener) {
myChangeListeners.remove(listener);
}
/*
* notify all listeners;
*/
private void notifyMyChangeListeners() {
MyChangeEvent event = new MyChangeEvent(MyClass.class, name);
Iterator iterator = ((Vector) myChangeListeners.clone()).iterator();
while (iterator.hasNext()) {
MyChangeListener listener = (MyChangeListener) iterator.next();
listener.MyChanged(event);
}
}
}
/*
* Created on Nov 26, 2004 11:47:04 PM
*
* MyEventTest.java
**/
package org.yz21.study.event;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* @author violin
*
* Copyright www.yz21.org 2003-2004
*/
public class MyEventTest extends TestCase implements MyChangeListener {
private MyClass myclass;
private String testname;
/**
* Constructor for MyEventTest.
* @param arg0
*/
public MyEventTest(String arg0) {
super(arg0);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(MyEventTest.class);
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
myclass = new MyClass();
myclass.addMyChangeListener(this);
this.testname = "yz21.org";
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
myclass = null;
}
/*
* @see TestCase#suite()
*/
public static Test suite() {
return new TestSuite(MyEventTest.class); //以类为参数
}
public void MyChanged(MyChangeEvent event) {
this.testname = event.getMyname();
}
public void testMyEvent() {
myclass.testMyEvent("www.yz21.org");
this.assertEquals("测试事件","www.yz21.org",testname);
}
}
参考资料:
1、java docs
2、Apache Jmeter
3、Junit docs