首 页 | 模 板 | 学 院 | 源 码 | 书 籍 | 壁 纸 | 字 体 | JS脚本 | FLASH源码 | 软 件 | 矢 量 | 服务器软件 | 素 材 | 其 它 |
设为主页
加入收藏
联系站长
平面设计 | 网页制作 | 程序编写 | 数 据 库 | 媒体动画 | 网络冲浪 | 服务器相关 | 站长乐园 | 业界动态 |
当前在线
实用:JAVA事件模式下PHP如何实现
2006-11-29 16:42:41  作者:模板天下收集整理  来源:未知 网友评论 0 条 论坛
  

本文介绍了JAVA事件模式的PHP实现。在我以前的文章里,我概括了系统事件定义和使用call_user_func()函数建立php 事件模块的例子。本文通过引入高级的基于sender/eventObject/listener的php事件模块对这个科目进行了扩展。

下面是一个JAVA 事件系统的例子。这个例子基于Apache小组 开发的,来源于Apache.org并进行了重新缩短和重顶格式后的ProtocolCommandSupport.java, ProtocolCommandListener.java and ProtocolCommandEvent.java文件。ProtocolCommandSupport.java文件包含了事件创建类,类的实例由系统创建,当系统被实例调用时,就就会创建该事件。事实上这个类看上去就像包含了一个监听器---类监听到事件创建并且在事件创建被通知时进行反应。注意“addProtocolCommandListener” and “removeProtocolCommandListener”方法,他们用于附加和分离监听。另外2个方法fireCommandSent” and “fireReplyReceived”,召唤了对象“__listeners”容器储存的方法。该容器实际上,只积累了ProtocolCommandListener 对象,因为只有该对象在调用addProtocolCommandListener” and “removeProtocolCommandListener”方法时能够被通过。

ProtocolCommandSupport.java

public class ProtocolCommandSupport implements Serializable {

private Object __source;
private ListenerList __listeners = new ListenerList();

public ProtocolCommandSupport(Object source) {
__source = source;
}

/***
* Fires a ProtocolCommandEvent signalling the sending of a command to all
* registered listeners.
***/
public void fireCommandSent(String command, String message) {
Enumeration en = __listeners.getListeners();
ProtocolCommandEvent event =
new ProtocolCommandEvent(__source, command, message);
ProtocolCommandListener listener;
while (en.hasMoreElements()) {
listener = (ProtocolCommandListener)en.nextElement();
listener.protocolCommandSent(event);
}
}

/***
* Fires a ProtocolCommandEvent signalling the reception of a command reply
* to all registered listeners.
***/
public void fireReplyReceived(int replyCode, String message) {
Enumeration en = __listeners.getListeners();
ProtocolCommandEvent event =
new ProtocolCommandEvent(__source, replyCode, message);
ProtocolCommandListener listener;
while (en.hasMoreElements()) {
listener = (ProtocolCommandListener)en.nextElement();
listener.protocolReplyReceived(event);
}
}

public void addProtocolCommandListener(ProtocolCommandListener listener) {
__listeners.addListener(listener);
}

public void removeProtocolCommandListener(ProtocolCommandListener listener) {
__listeners.removeListener(listener);
}

}

ProtocolCommandListener.java 包含了监听器接口。其后的含义是任何将要由ProtocolCommandSupport类认证的事件将要提供这个能够实现一个足够被事件认证接口。这就可能造成了一个新的种类,2个或更多不同的类被事件认证,因为一个类能够实现多重接口。
ProtocolCommandListener.java

public interface ProtocolCommandListener extends EventListener {

共分4页  [1] [2] [3] [4] 

>> 相关文章

关于网站 | 客服中心 | 服务条款 | 友情链接 | 广告联系 | 本站历程 | 网站导航

吉ICP备05000107号