当前位置: 首页 > 图文教程 > 网络编程 > PHP > 实用比较:JAVA事件模式下PHP如何实现

PHP
让我们来编写一些PHP实用的脚本
七种缓存使用武器 为网站应用和访问加速
动态网页PHP中引用&的使用注意事项
在PHP中全面阻止SQL注入式攻击
PHP自带可以代替echo调试的unit函数
小结:PHP动态网页程序优化及高效提速问题
php对特殊语句查询结果进行数组排序
实例:用PHP技术解决网站URL格式过长的问题
小结:PHP动态网页程序两个有用的小技巧
动态网页中直接不让访问PHP程序文件
网页实例:详细介绍用PHP来编写网页记数器
菜鸟学习:动态网页PHP基础学习笔记
利用Apache实现禁止图片盗链
PHP编程中常用的三则技巧
PHP制作的网站意见在线反馈表
大型Web需求解决方案 PHP定位突出
PHP实例:精确到每一秒钟的在线人数显示代码
实用:动态网页制作技术PHP的十个应用技巧
常见php页面漏洞分析及相关问题解决
PHP和MYSQL制作动态网站开发经验之谈

实用比较:JAVA事件模式下PHP如何实现


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-03   浏览: 23 ::
收藏到网摘: n/a

    【PHPChina讯】本文介绍了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 {

 /***
  * This method is invoked by a ProtocolCommandEvent source after
  * sending a protocol command to a server.
  *
  * @param event The ProtocolCommandEvent fired.
  ***/
 public void protocolCommandSent(ProtocolCommandEvent event);

 /***
  * This method is invoked by a ProtocolCommandEvent source after
  * receiving a reply from a server.
  *
  * @param event The ProtocolCommandEvent fired.
  ***/
 public void protocolReplyReceived(ProtocolCommandEvent event);

}

    本文的最后一个文件是ProtocolCommandEvent.java。这个文件包含了一个EventO