技术开发 频道

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

    【IT168 技术文档】本文介绍了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。这个文件包含了一个EventObject 类中的叫做ProtocolCommandEvent的子集。EventObject的子集用于通过从event producer到event listeners的事件数据。ProtocolCommandEvent 类增加了一个定义在EventObject中的getSource()方法,创建了creates the getMessage(), isReply(), isCommand(), getReplyCode() and getCommand()方法。这些方法只能返回给类一个只读字段,只能在构造函数中设置。
    ProtocolCommandEvent.java
    public class ProtocolCommandEvent extends EventObject {

    private int __replyCode;
    private boolean __isCommand;
    private String __message, __command;

    public ProtocolCommandEvent(Object source, String command, String message) {
    super(source);
    __replyCode = 0;
    __message = message;
    __isCommand = true;
    __command = command;
    }

    public ProtocolCommandEvent(Object source, int replyCode, String message) {
    super(source);
    __replyCode = replyCode;
    __message = message;
    __isCommand = false;
    __command = null;
    }

    public String getCommand() { return __command; }
    public int getReplyCode() { return __replyCode; }
    public boolean isCommand() { return __isCommand; }
    public boolean isReply() { return !isCommand(); }
    public String getMessage() { return __message; }

    }
    这3个文件组,只是JAVA事件系统的普通实现。也许把它应用于PHP程序是一个好的尝试。

    综上而言,如下的说明能够用于创建一个新的事件:

    1. 创建一个EventObject 类的子类,用于给事件数据提供监听。通常命名为包含事件名称和”event”结尾,就像ProtocolCommandEvent。这样你以后可以重新使用已经存在的类甚至事件对象本身。

    2. 创建或继承一个用于实现监听的接口。通常接口名称以“Listener”结尾并且包含1个或更多的被事件生成调用的函数,并且有一个符合对应的事件对象子集的自变量。

    3. 添加 “add<EventName>Listener(<EventName>Listener)” and “fire<EventName>()” 方法在事件生成器中. 任意的, 添加“remove<EventName>Listener(<EventName>Listener)”如果你想要移除监听。这些方法通过完善的训练,可以省略掉。

    4. 在你想要创建事件的代码中引入“fire<EventName>()”方法。这种方法可以被用于多种环境中例如,DatabaseError事件在数据库联接不能被创建或者查询失败的时候发生。

    The PHP event implementation requires two related classes to be defined and included into the PHP project: EventObject and ListenerList, as follows:

    PHP事件处理实现需要包含2个相关类在PHP工程里:EventObject and ListenerList就象下面的实例

    class EventObject {

    protected $source;

    function __construct($source) {
    $this->source = $source;
    }

    function getSource() {
    return $this->source;
    }

    }

    class ListenerList {

    protected $listeners = array();

    function add($listener) {
    $this->listeners[] = $listener;
    }

    function getRaw() {
    return $this->listeners;
    }

    }
    让我们来创建一个简单的用于产生事件并且同时进行监听的类。听起来满困难的,但是却是常被使用的技术
    interface MyEventListener {
    function onMyEvent(EventObject $event);
    }

    class MyClass implements MyEventListener {

    protected $listeners;

    function __construct() {
    $this->listeners = new ListenerList();
    $this->addMyEventListener($this);
    }

    function addMyEventListener(MyEventListener $listener) {
    $this->listeners->add($listener);
    }

    function work() {
    echo "Working!\n";
    $this->fireMyEvent();
    echo "Working!\n";
    $this->fireMyEvent();
    echo "Working!\n";
    }

    protected function fireMyEvent() {
    $event = new EventObject($this);
    foreach ($this->listeners->getRaw() as $listener) {
    $listener->onMyEvent($event);
    }
    }

    function onMyEvent(EventObject $event) {
    echo "Taking short break...\n";
    }

    }

    $object = new MyClass();
    $object->work();
    // Will print:
    // Working!
    // Taking short break...
    // Working!
    // Taking short break...
    // Working!

    使用事件描述模式,可以增进你的代码的稳定性,但是要注意的是他要在你的代码中添加很多接口和事件类,这样比没使用前执行要慢。

0
相关文章