观察者模式

利用观察者模式建立的事件监听系统,主要步骤是

EventObject 是所有事件的根类

EventListener 是所有监听器接口的根接口

Java事件监听模式:


1、事件 ( extends EventObject):定义事件,继承EventObject的属性和方法
2、监听器接口 ( extends EventListener):继承EventListener接口
3、注册器:实现注册器操作方法、事件广播方法,定义事件触发条件
4、事件监听器 (extends MyEventListenerInterface):注册监听器、定义事件处理方法


A new custom event must extends EventObject. Moreover, an event listener interface must be declared to allow objects to

receive the new custom event. All listeners must extend from EventListener.

1、事件MyEvent,继承EventObject事件

This example demonstrates all the steps necessary to create a new custom event.

// Declare the event. It must extend EventObject.

public class MyEvent extends EventObject {
public MyEvent(Object source) {
super(source);
}
}

2、监听接口MyEventListenerInterface
// Declare the listener class. It must extend EventListener.
// A class must implement this interface to get MyEvents.
public interface MyEventListener extends EventListener {
public void myEventOccurred(MyEvent evt);
}
3、注册器MyEventRegister

// Add the event registration and notification code to a class.
public class MyEventRegister {
protected Vector listenerList=new Vector();
private int value;
public void setValue(int newValue){
if(value!=newValue){
value=newValue;
MyEvent evt=new MyEvent(this,newValue);
fireMyEvent(evt); } }

public synchronized void addMyEventListener(MyEventListener listener){
listenerList.add(listener);
}


public synchronized void removedMyEventListener(MyEventListener listener){

listenerList.removeElement(listener);
}


public void fireMyEvent(MyEvent evt){
Vector currentListenerList=null;
synchronized(this){
currentListenerList=(Vector)listenerList.clone(); }
for(int i=0;i<currentListenerList.size();i++){
MyEventListener listener= (MyEventListener)currentListenerList.elementAt(i);
listener.myEventOccurred(evt);
}
}
}


4、事件监听器MyEventListener
public class MyEventListener implements MyEventListenerInterface{
public MyEventListener(MyEventRegister register){
register.addMyEventListener(this);
}

public void myEventOccurred(MyEvent evt){
System.out.println("事件发生了...");
}
}



自己创建事件监听系统时,主要有以下几步:
1.创建事件类,事件类必须继承EventObject类,例如:

package com.cxm;

import java.util.EventObject;

public class MyEvent extends EventObject {

public MyEvent(Object source) {
super(source);
}

/**
*
*/
private static final long serialVersionUID = 525828850067101202L;

}

2.创建监听器接口,该接口必须接触EventListener接口,例如:

package com.cxm;

import java.util.EventListener;

public interface NyEventListener extends EventListener{

//在监听器接口中定义某事件发生时,要调用的方法
public void myEventOccured(MyEvent me);

}


3.创建事件源,在事件源中当某事件发生时,必须手动抛出该事件(不像swing中的button,系统已经做好了,当事件发生时,该类自动会把事

件抛出),然后在调用相应的事件监听方法
public class MyEventSource {
protected Vector listenerList = new Vector();
private int value;

public void setValue(int newValue) {
if (value != newValue) {
value = newValue;
MyEvent evt = new MyEvent(newValue);
fireMyEvent(evt);
}
}

public synchronized void addMyEventListener(NyEventListener listener) {
listenerList.add(listener);
}

public synchronized void removedMyEventListener(NyEventListener listener) {

listenerList.removeElement(listener);
}

public void fireMyEvent(MyEvent evt) {
Vector currentListenerList = null;
synchronized (this) {
currentListenerList = (Vector) listenerList.clone();
}
for (int i = 0; i < currentListenerList.size(); i++) {
NyEventListener listener = (NyEventListener) currentListenerList
.elementAt(i);
//如果这样实现事件监听系统,就没有必要实现EventObject和EventListener
//实现这两个类主要不是为了功能上的监听,而是为了调用方法的统一
listener.myEventOccured(evt);
}
}
}


4.测试事件监听系统,如下:
public class MainEvent {

public static void main(String args[]){
MyEventSource mr=new MyEventSource ();
mr.addMyEventListener(new NyEventListener(){

public void myEventOccured(MyEvent me) {
// TODO Auto-generated method stub
System.out.println("sss");
}

});

mr.setValue(0);
mr.setValue(9);
mr.setValue(93);

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值