IT貓撲網(wǎng):您身邊最放心的安全下載站! 最新更新|軟件分類|軟件專題|手機(jī)版|論壇轉(zhuǎn)貼|軟件發(fā)布

您當(dāng)前所在位置:首頁(yè)網(wǎng)絡(luò)編程PHP編程 → 實(shí)用:JAVA事件模式下PHP如何實(shí)現(xiàn)

實(shí)用:JAVA事件模式下PHP如何實(shí)現(xiàn)

時(shí)間:2015/6/28來(lái)源:IT貓撲網(wǎng)作者:網(wǎng)管聯(lián)盟我要評(píng)論(0)

本文介紹了java事件模式的php實(shí)現(xiàn)。在我以前的文章里,我概括了系統(tǒng)事件定義和使用call_user_func()函數(shù)建立php 事件模塊的例子。本文通過(guò)引入高級(jí)的基于sender/eventObject/listener的php事件模塊對(duì)這個(gè)科目進(jìn)行了擴(kuò)展。

下面是一個(gè)JAVA 事件系統(tǒng)的例子。這個(gè)例子基于Apache小組 開發(fā)的,來(lái)源于Apache.org并進(jìn)行了重新縮短和重頂格式后的ProtocolCommandSupport.java, ProtocolCommandListener.java and ProtocolCommandEvent.java文件。ProtocolCommandSupport.java文件包含了事件創(chuàng)建類,類的實(shí)例由系統(tǒng)創(chuàng)建,當(dāng)系統(tǒng)被實(shí)例調(diào)用時(shí),就就會(huì)創(chuàng)建該事件。事實(shí)上這個(gè)類看上去就像包含了一個(gè)監(jiān)聽(tīng)器---類監(jiān)聽(tīng)到事件創(chuàng)建并且在事件創(chuàng)建被通知時(shí)進(jìn)行反應(yīng)。注意"addProtocolCommandListener" and "removeProtocolCommandListener"方法,他們用于附加和分離監(jiān)聽(tīng)。另外2個(gè)方法fireCommandSent" and "fireReplyReceived",召喚了對(duì)象"__listeners"容器儲(chǔ)存的方法。該容器實(shí)際上,只積累了ProtocolCommandListener 對(duì)象,因?yàn)橹挥性搶?duì)象在調(diào)用addProtocolCommandListener" and "removeProtocolCommandListener"方法時(shí)能夠被通過(guò)。

以下為引用的內(nèi)容:

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 包含了監(jiān)聽(tīng)器接口。其后的含義是任何將要由ProtocolCommandSupport類認(rèn)證的事件將要提供這個(gè)能夠?qū)崿F(xiàn)一個(gè)足夠被事件認(rèn)證接口。這就可能造成了一個(gè)新的種類,2個(gè)或更多不同的類被事件認(rèn)證,因?yàn)橐粋(gè)類能夠?qū)崿F(xiàn)多重接口。
ProtocolCommandListener.java

public interface ProtocolCommandListener extends EventListener {

以下為引用的內(nèi)容:

/***

* 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);

}

本文的最后一個(gè)文件是ProtocolCommandEvent.java。這個(gè)文件包含了一個(gè)EventObject 類中的叫做ProtocolCommandEvent的子集。EventObject的子集用于通過(guò)從event producer到event listeners的事件數(shù)據(jù)。ProtocolCommandEvent 類增加了一個(gè)定義在EventObject中的getSource()方法,創(chuàng)建了creates the getMessage(), isReply(), isCommand(), getReplyCode() and getcommand()方法。這些方法只能返回給類一個(gè)只讀字段,只能在構(gòu)造函數(shù)中設(shè)置。

以下為引用的內(nèi)容:

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個(gè)文件組,只是JAVA事件系統(tǒng)的普通實(shí)現(xiàn)。也許把它應(yīng)用于PHP程序是一個(gè)好的嘗試。

綜上而言,如下的說(shuō)明能夠用于創(chuàng)建一個(gè)新的事件:

1. 創(chuàng)建一個(gè)EventObject 類的子類,用于給事件數(shù)據(jù)提供監(jiān)聽(tīng)。通常命名為包含事件名稱和"event"結(jié)尾,就像ProtocolCommandEvent。這樣你以后可以重新使用已經(jīng)存在的類甚至事件對(duì)象本身。

2. 創(chuàng)建或繼承一個(gè)用于實(shí)現(xiàn)監(jiān)聽(tīng)的接口。通常接口名稱以"Listener"結(jié)尾并且包含1個(gè)或更多的被事件生成調(diào)用

關(guān)鍵詞標(biāo)簽:Java,php

相關(guān)閱讀

文章評(píng)論
發(fā)表評(píng)論

熱門文章 plsql developer怎么連接數(shù)據(jù)庫(kù)-plsql deveplsql developer怎么連接數(shù)據(jù)庫(kù)-plsql deve2021年最好用的10款php開發(fā)工具推薦2021年最好用的10款php開發(fā)工具推薦在 PHP 中使用命令行工具在 PHP 中使用命令行工具php應(yīng)用程序安全防范技術(shù)研究php應(yīng)用程序安全防范技術(shù)研究

相關(guān)下載

人氣排行 詳解ucenter原理及第三方應(yīng)用程序整合思路、方法PHP中防止SQL注入攻擊PHP會(huì)話Session的具體使用方法解析PHP運(yùn)行出現(xiàn)Notice : Use of undefined constant 的解決辦法CakePHP程序員必須知道的21條技巧PHP如何清空mySQL數(shù)據(jù)庫(kù)PHP采集圖片實(shí)例(PHP采集)plsql developer怎么連接數(shù)據(jù)庫(kù)-plsql developer連接數(shù)據(jù)庫(kù)方法